linux Shell入门:掌握Linux,OS X,Unix的Shell环境

2019-09-23 09:27:24于丽

看看帖子"Linux下使用其他用户身份运行命令"更多地了解sudo,su和runuser命令。

shell别名

别名仅仅是命令的一个快捷方式。

列出所有的别名

输入下面的命令:

alias

示例输出:

alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias bc='bc -l'
alias cd..='cd ..'
alias chgrp='chgrp --preserve-root'
alias chmod='chmod --preserve-root'
alias chown='chown --preserve-root'
alias cp='cp -i'
alias dnstop='dnstop -l 5 eth1'
alias egrep='egrep --color=auto'
alias ethtool='ethtool eth1'

设定一个别名

bash/zsh语法:

alias c='clear'
alias down='sudo /sbin/shutdown -h now'

对于命令clear可以输入c别名,这样我们就可以输入c代替clear命令来清空屏幕:

c

或者输入down来关闭基于Linux的服务器:

down

你可以设定任意多的别名。看下"Linux/Unix/Mac OS X系统中的30个方便的bash shell别名"了解在类Unix系统中别名的实际应用。

shell函数

Bash/ksh/zsh函数允许你更进一步地配置shell环境。在这个例子中,我写了一个简单的名叫memcpu()的bash函数,用来显示前10个最占用CPU和内存的进程:

memcpu() { echo "*** Top 10 cpu eating process ***"; ps auxf | sort -nr -k 3 | head -10;
echo "*** Top 10 memory eating process ***"; ps auxf | sort -nr -k 4 | head -10; }

输入memcpu就可以在屏幕上看到下面的信息:

memcpu
 
*** Top 10 cpu eating process ***
nginx  39559 13.0 0.2 264020 35168 ?    S  04:26  0:00   _ /usr/bin/php-cgi
nginx  39545 6.6 0.1 216484 13088 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nginx  39471 6.2 0.6 273352 81704 ?    S  04:22  0:17   _ /usr/bin/php-cgi
nginx  39544 5.7 0.1 216484 13084 ?    S  04:25  0:03   _ /usr/bin/php-cgi
nginx  39540 5.5 0.1 221260 19296 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nginx  39542 5.4 0.1 216484 13152 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nixcraft 39543 5.3 0.1 216484 14096 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nixcraft 39538 5.2 0.1 221248 18608 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nixcraft 39539 5.0 0.1 216484 16272 ?    S  04:25  0:04   _ /usr/bin/php-cgi
nixcraft 39541 4.8 0.1 216484 14860 ?    S  04:25  0:04   _ /usr/bin/php-cgi
 
*** Top 10 memory eating process ***
498   63859 0.5 4.0 2429652 488084 ?   Ssl  2014 177:41 memcached -d -p 11211 -u memcached -m 2048 -c 18288 -P /var/run/memcached/memcached.pid -l 10.10.29.68 -L
mysql  64221 4.2 3.4 4653600 419868 ?   Sl  2014 1360:40 _ /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
nixcraft 39418 0.4 1.1 295312 138624 ?    S  04:17  0:02 |  _ /usr/bin/php-cgi
nixcraft 39419 0.5 0.9 290284 113036 ?    S  04:18  0:02 |  _ /usr/bin/php-cgi
nixcraft 39464 0.7 0.8 294356 99200 ?    S  04:20  0:02 |  _ /usr/bin/php-cgi
nixcraft 39469 0.3 0.7 288400 91256 ?    S  04:20  0:01 |  _ /usr/bin/php-cgi
nixcraft 39471 6.2 0.6 273352 81704 ?    S  04:22  0:17   _ /usr/bin/php-cgi
vivek  39261 2.2 0.6 253172 82812 ?    S  04:05  0:28   _ /usr/bin/php-cgi
squid   9995 0.0 0.5 175152 72396 ?    S   2014 27:00 _ (squid) -f /etc/squid/squid.conf
cybercit 3922 0.0 0.4 303380 56304 ?    S  Jan10  0:13 |  _ /usr/bin/php-cgi