[华为云在线课程][Linux基础入门和帮助][第二章Linux命令执行][学习笔记]
【摘要】 执行命令过程输入命令按下回车键,提醒Shell程序找到键入命令所对应的可执行程序或代码,并由其分析后提交给内核分配资源将其运行起来 Shell中可执行的两类命令内部命令:由Shell自带的,并且通过某命令形式提供外部命令:在文件系统路径下有对应的可执行程序文件如何区别指定的命令是内部命令还是外部命令?type COMMAND例子:查看是否存在对应内部命令和外部命令[15:59:39 roo...
执行命令过程
- 输入命令按下回车键,提醒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进程有效
如果想永久有效,要定义在配置文件中
- 仅对当前用户:~/.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
- 多个命令可以用";"符号分开
- 一个命令可以用\分成多行
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)