[华为云在线课程][Linux基础入门和帮助][第二章Linux命令执行][学习笔记]

网友投稿 554 2022-05-29

执行命令过程

输入命令按下回车键,提醒Shell程序找到键入命令所对应的可执行程序或代码,并由其分析后提交给内核分配资源将其运行起来

Shell中可执行的两类命令

内部命令:由Shell自带的,并且通过某命令形式提供

外部命令:在文件系统路径下有对应的可执行程序文件

如何区别指定的命令是内部命令还是外部命令?

type COMMAND

例子:查看是否存在对应内部命令和外部命令

[15:59:39 root@centos7 ~]#type -a echo echo is a shell builtin echo is /usr/bin/echo

内部命令相关

help 内部命令列表

enable 管理内部命令

enable cmd 启用内部命令

enable -n cmd 禁用内部命令

enable -n 查看所有禁用的内部命令

[16:03:49 root@centos7 ~]#help GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star (*) next to a name means that the command is disabled. job_spec [&] history [-c] [-d offset] [n] or hist> (( expression )) if COMMANDS; then COMMANDS; [ elif C> . filename [arguments] jobs [-lnprs] [jobspec ...] or jobs > : kill [-s sigspec | -n signum | -sigs> [ arg... ] let arg [arg ...] [[ expression ]] local [option] name[=value] ... alias [-p] [name[=value] ... ] logout [n] bg [job_spec ...] mapfile [-n count] [-O origin] [-s c> bind [-lpvsPVS] [-m keymap] [-f filen> popd [-n] [+N | -N] break [n] printf [-v var] format [arguments] builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir] caller [expr] pwd [-LP] case WORD in [PATTERN [| PATTERN]...)> read [-ers] [-a array] [-d delim] [-> cd [-L|[-P [-e]]] [dir] readarray [-n count] [-O origin] [-s> command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] o> compgen [-abcdefgjksuv] [-o option] > return [n] complete [-abcdefgjksuv] [-pr] [-DE] > select NAME [in WORDS ... ;] do COMM> compopt [-o|+o option] [-DE] [name ..> set [-abefhkmnptuvxBCHP] [-o option-> continue [n] shift [n] coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...] declare [-aAfFgilrtux] [-p] [name[=va> source filename [arguments] dirs [-clpv] [+N] [-N] suspend [-f] disown [-h] [-ar] [jobspec ...] test [expr] echo [-neE] [arg ...] time [-p] pipeline enable [-a] [-dnps] [-f filename] [na> times eval [arg ...] trap [-lp] [[arg] signal_spec ...] exec [-cl] [-a name] [command [argume> true exit [n] type [-afptP] name [name ...] export [-fn] [name[=value] ...] or ex> typeset [-aAfFgilrtux] [-p] name[=va> false ulimit [-SHacdefilmnpqrstuvx] [limit> fc [-e ename] [-lnr] [first] [last] o> umask [-p] [-S] [mode] fg [job_spec] unalias [-a] name [name ...] for NAME [in WORDS ... ] ; do COMMAND> unset [-f] [-v] [name ...] for (( exp1; exp2; exp3 )); do COMMAN> until COMMANDS; do COMMANDS; done function name { COMMANDS ; } or name > variables - Names and meanings of so> getopts optstring name [arg] wait [id] hash [-lr] [-p pathname] [-dt] [name > while COMMANDS; do COMMANDS; done help [-dms] [pattern ...] { COMMANDS ; }

执行外部命令

查看外部命令路径:

[16:08:10 root@centos7 ~]#which java /usr/bin/java [16:09:07 root@centos7 ~]#whereis java java: /usr/bin/java /usr/lib/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz

Hash缓存表

系统初始hash表为空,当外部命令执行时,默认会从PATH路径下寻找该命令,找到后会将这条命令的路径记录到hash表中,当再次使用该命令时,Shell解释器首先会查看hash表,存在将执行,如果不存在,将会去PATH路径下寻找,利用hash缓存表可大大提高命令的调用速率

hash命令常见用法

hash,显示hash缓存

[16:09:31 root@centos7 ~]#hash hits command 1 /usr/bin/whereis 1 /usr/bin/clear

hash -l,显示hash缓存,可作为输入使用

[16:17:46 root@centos7 ~]#hash -l builtin hash -p /usr/bin/whereis whereis builtin hash -p /usr/bin/clear clear

hash -p path name,将命令全路径path起别名为name

[16:20:42 root@centos7 ~]#hash -p path name

hash -t name,打印缓存中name的路径

[16:20:52 root@centos7 ~]#hash -t name path

hash -d name,清除name缓存

hash -r,清除缓存

命令别名

对于经常执行的较长的命令,可以将其定义成较短的别名,以方便执行

显示当前Shell进程所有可用的命令别名

alias

定义别名NAME,其相当于执行命令VALUE

alias NAME='VALUE'

例子:扫描新加的磁盘

[16:44:15 root@centos7 ~]#alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan'

例子:持久化保存别名

[17:04:17 root@centos7 ~]#vi ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # New aliase alias sayhello='echo hello world' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi [17:06:31 root@centos7 ~]#source ~/.bashrc [17:06:55 root@centos7 ~]#sayhello hello world

撤销别名:unalias

unalias [-a] name [name ...] unalias -a #取消所有别名

注意:在命令行中定义的别名,仅对当前Shell进程有效

[华为云在线课程][Linux基础入门和帮助][第二章Linux命令执行][学习笔记]

如果想永久有效,要定义在配置文件中

仅对当前用户:~/.bashrc

对所有用户生效:/etc/bashrc

编辑配置给出的新配置不会立即生效,bash进程重新读取配置文件

source /path/to/config_file . /path/to/config_file

如果别名同原命令同名,如果要执行原命令,可使用

\ALIASNAME "ALIASNAME" 'ALIASNAME' command ALIASNAME /path/command #只适用于外部命令

命令格式

COMMAND [OPTIONS...] [ARGUMENTS...] COMMAND [COMMAND] [COMMAND] ...

选项:用于启用或关闭命令的某个或某些功能

短选项:UNIX风格选项, -c 例如:-l,-h

长选项:GNU风格选项, --word 例如:–all,–human

BSD风格选项:一个字母,例如:a,使用相对较少

参数:命令的作用对象,比如:文件名,用户名等

例子:

[17:20:13 root@centos7 ~]#id -u xxx id: xxx: no such user [17:20:27 root@centos7 ~]#ls -a . .bash_history .bashrc .cshrc .tcshrc .. .bash_logout .cache .dbus .viminfo anaconda-ks.cfg .bash_profile .config initial-setup-ks.cfg [17:20:37 root@centos7 ~]#ls --all . .bash_history .bashrc .cshrc .tcshrc .. .bash_logout .cache .dbus .viminfo anaconda-ks.cfg .bash_profile .config initial-setup-ks.cfg [17:20:46 root@centos7 ~]#free -h total used free shared buff/cache available Mem: 1.8G 512M 930M 14M 376M 1.1G Swap: 2.0G 0B 2.0G [17:20:51 root@centos7 ~]#free --human total used free shared buff/cache available Mem: 1.8G 512M 930M 14M 376M 1.1G Swap: 2.0G 0B 2.0G [17:21:01 root@centos7 ~]#ps a PID TTY STAT TIME COMMAND 1738 tty1 Ssl+ 0:00 /usr/bin/X :0 -background none -noreset -audit 4 -ve 2143 pts/0 Ss 0:00 -bash 3412 pts/0 R+ 0:00 ps a

注意:

多个选项以及多参数和命令之间使用空白字符分隔

取消和结束命令执行:Ctrl+c,Ctrl+d

多个命令可以用";"符号分开

一个命令可以用\分成多行

Linux Shell

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:进阶Python之图形界面篇(上)
下一篇:在MySQL中如何使用覆盖索引优化limit分页查询
相关文章