您的位置:首页 > 运维架构 > Shell

Shell基础应用

2019-06-12 09:00 1126 查看
版权声明:苏苏吖 https://blog.csdn.net/weixin_44774638/article/details/91489119

问题
本案例要求熟悉Linux Shell环境的特点,主要练习以下操作:
1)切换用户的Shell环境
2)练习命令历史、命令别名
3)重定向标准输入/输出/错误输出
4)管道操作实践
方案
步骤
实现此案例需要按照如下步骤进行。

步骤一:切换用户的Shell环境

若需要临时使用另一种Shell环境,可以直接执行对应的Shell解释器程序,比如只要执行zsh可以切换到zsh命令行环境。

[root@svr5 ~]# yum -y install zsh  			//若缺少zsh请先安装zsh包
.. ..
[root@svr5 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
/bin/zsh  									//确认当前系统已识别zsh

[root@svr5 ~]# zsh  							//进入zsh环境
[root@svr5]~# help help  						//尝试查看内部命令help的用法
zsh: command not found: help  					//失败,因为zsh未提供内部命令help
[root@svr5]~# exit  							//返回到切换前的bash环境

若希望修改用户的登录Shell,管理员可以直接通过usermod命令设置。比如,以下操作可将用户zhangsan的登录Shell改为/bin/tcsh:

[root@svr5 ~]# grep 'zhangsan' /etc/passwd
zhangsan:x:516:516::/home/zhangsan:/bin/bash  			//修改前
[root@svr5 ~]# usermod -s /bin/tcsh zhangsan  			//执行修改操作
[root@svr5 ~]# grep 'zhangsan' /etc/passwd
zhangsan:x:516:516::/home/zhangsan:/bin/tcsh  			//修改后

对于普通用户来说,可以使用chsh命令修改自己的登录Shell。比如,用户zhangsan可执行以下操作将自己的登录Shell重新改为/bin/bash:

[zhangsan@svr5 ~]$ grep 'zhangsan' /etc/passwd
zhangsan:x:516:516::/home/zhangsan:/bin/tcsh  			//修改前
[zhangsan@svr5 ~]$ chsh  								//执行修改操作
Changing shell for zhangsan.
口令:  										//验证用户zhangsan的密码
New shell [/bin/tcsh]: /bin/bash  			//指定新Shell程序的路径
Shell changed.
[zhangsan@svr5 ~]$ grep 'zhangsan' /etc/passwd
zhangsan:x:516:516::/home/zhangsan:/bin/bash  			//修改后

步骤二:练习命令历史

1)检查历史命令的容量。
默认记录1000条,通过全局变量HISTSIZE设置,对所有用户有效:

[root@svr5 ~]# grep HISTSIZE /etc/profile
HISTSIZE=1000
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
查看已为当前用户记录的历史命令条数:
[root@svr5 ~]# history | wc -l
1000

2)查看历史命令列表。
列出最近执行的10条历史命令:

[root@svr5 ~]# history | tail
1028  grep 'zhangsan' /etc/passwd
1029  cat /etc/redhat-release
1030  usermod -s /bin/tcsh zhangsan
1031  grep 'zhangsan' /etc/passwd
1032  su - zhangsan
1033  echo 1234567 | passwd --stdin zhangsan
1034  su - zhangsan
1035  grep HISTSIZE /etc/profile
1036  history | wc -l
1037  history | tail

3)调用指定的历史命令。
重新执行历史命令列表中的第1028条操作:

[root@svr5 ~]# !1028
grep 'zhangsan' /etc/passwd
zhangsan:x:516:516::/home/zhangsan:/bin/bash

重新执行最近一次以cat开头(根据实际情况变更)的历史命令操作:

[root@svr5 ~]# !cat
cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.9 (Tikanga)

4)清空已记录的历史命令。
[root@svr5 ~]# history -c //清空自己的历史命令
[root@svr5 ~]# > ~/.bash_history //清空记录文件
[root@svr5 ~]# history //再次检查历史命令列表
42 > ~/.bash_history
43 history

步骤三:练习命令别名

1)查看已经定义的命令别名列表。
当前的别名列表:

[root@svr5 ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

别名设置一般存放在用户的.bashrc文件内:

[root@svr5 ~]# grep '^alias' ~/.bashrc
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

2)自定义新的命令别名
添加一个名为lh的命令别名,实际执行的是“ls –lh –color=tty”:

[root@svr5 ~]# alias lh='ls -lh'  					//定义别名命令lh
[root@svr5 ~]# alias lh  							//确认定义结果
alias lh='ls -lh'

验证别名命令的效果:

[root@svr5 ~]# lh /etc/fstab  						//使用别名
-rw-r--r-- 1 root root 733 10-09 15:34 /etc/fstab
[root@svr5 ~]# ls -lh /etc/fstab  					//使用完整的命令
-rw-r--r-- 1 root root 733 10-09 15:34 /etc/fstab

3)取消别名
取消单个别名:

[root@svr5 ~]# unalias lh  						//取消名为lh的命令别名
[root@svr5 ~]# alias lh  						//查询时已没有lh
-bash: alias: lh: not found
取消所有别名:
[root@svr5 ~]# unalias -a  						//取消所有别名
[root@svr5 ~]# alias  							//查询时已无任何别名
[root@svr5 ~]#

步骤四:重定向标准输入/输出/错误输出

1)查看相关设备文件。
标准输入(stdin),描述号为0;标准输出(stdout),描述号为1;标准错误(stderr),描述号为2。三个都在/dev/目录下有链接文件,指向/proc文件系统下的运行文件。

[root@svr5 ~]# ls -l /dev/std*
lrwxrwxrwx 1 root root 15 12-05 13:32 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 12-05 13:32 /dev/stdout -> /proc/self/fd/1
lrwxrwxrwx 1 root root 15 12-05 13:32 /dev/stderr -> /proc/self/fd/2
[root@svr5 ~]# ls -l /proc/self/fd/[0-2]
lrwx------ 1 root root 64 12-10 13:58 /proc/self/fd/0 -> /dev/pts/0
lrwx------ 1 root root 64 12-10 13:58 /proc/self/fd/1 -> /dev/pts/0
lrwx------ 1 root root 64 12-10 13:58 /proc/self/fd/2 -> /dev/pts/0
[root@svr5 ~]# ls -l /dev/pts/0
crw--w---- 1 root tty 136, 0 12-10 13:58 /dev/pts/0

2)重定向标准输出。
使用 > 将命令执行的正常输出重定向到文件:

[root@svr5 ~]# ls -l /dev/pts/0  				//正常应输出到屏幕
crw--w---- 1 root tty 136, 0 12-10 14:04 /dev/pts/0
[root@svr5 ~]# ls -l /dev/pts/0 > stdout.txt  	//重定向到文件
[root@svr5 ~]# cat stdout.txt  					//确认重定向输出的结果
crw--w---- 1 root tty 136, 0 12-10 14:04 /dev/pts/0

操作会覆盖目标文件(先清空、再写入):

[root@svr5 ~]# echo "I am the king." > stdout.txt  		//覆盖目标文件
[root@svr5 ~]# cat stdout.txt  							//确认结果
I am the king.

改用 >> 可实现追加重定向输出:

[root@svr5 ~]# ls -l /dev/pts/0 >> stdout.txt  			//追加输出
[root@svr5 ~]# cat stdout.txt
I am the king.  											//原有内容还保留
crw--w---- 1 root tty 136, 0 12-10 14:08 /dev/pts/0  	//新加的内容

3)重定向标准错误。
对于命令执行出错的信息,使用 > 无法保存,仍然会输出到屏幕。比如,可使用ls命令同时查看两个对象(其中nofile并不存在),重定向输出:

[root@svr5 ~]# ls -lh nofile.txt /etc/fstab > stderr.txt
ls: nofile.txt: 没有那个文件或目录  			//出错信息仍显示到屏幕
[root@svr5 ~]# cat stderr.txt  				//正常信息成功重定向到目标文件
-rw-r--r-- 1 root root 733 10-09 15:34 /etc/fstab

使用 2> 可重定向错误信息,比如,可执行一个错误的命令:

[root@svr5 ~]# ipconfig /all
-bash: ipconfig: command not found  		//正常情况下,错误显示到屏幕
[root@svr5 ~]# ipconfig /all 2> stderr.txt  //将错误信息重定向到目标文件
[root@svr5 ~]# cat stderr.txt  				//确认重定向结果
-bash: ipconfig
4000
: command not found

类似的,2>> 可实现追加输出:

[root@svr5 ~]# ls nofile 2>> stderr.txt
[root@svr5 ~]# cat stderr.txt
-bash: ipconfig: command not found
ls: nofile: 没有那个文件或目录

若希望将正常输出、错误输出重定向同一个文件,可使用 &> :

[root@svr5 ~]# ls -lh nofile.txt /etc/fstab &> stderr.txt
[root@svr5 ~]# cat stderr.txt
ls: nofile.txt: 没有那个文件或目录
-rw-r--r-- 1 root root 733 10-09 15:34 /etc/fstab

在Shell脚本应用中,有些操作的输出信息可能既不需要用户看到,也不需要做任何处理,此时可以将其重定向空设备文件/dev/null。虽然执行后“看”不到任何变化,但管理员可根据返回状态 $? 的值来决定作何种操作(后续章节学习)。比如,使用id命令检查用户pingping是否存在,最终只需要给出“YES”或“NO”的答案,所以可将id的输出信息屏蔽掉:

[root@svr5 ~]# id pingping  							//未屏蔽时
uid=515(pingping) gid=1201(nsd) groups=1201(nsd)
[root@svr5 ~]# id pingping &> /dev/null  			//屏蔽后
[root@svr5 ~]# [ $? -eq 0 ] && echo "YES" || echo "NO"
YES

4)重定向标准输入。
< 的使用并不常见,因为这要求命令程序本身的支持,而事实上,相关命令可以直接将目标文件作为参数。比如,执行cat file与cat < file的效果是相同的:

[root@svr5 ~]# cat stdout.txt
I am the king.
crw--w---- 1 root tty 136, 0 12-10 14:08 /dev/pts/0
[root@svr5 ~]# cat < stdout.txt
I am the king.
crw--w---- 1 root tty 136, 0 12-10 14:08 /dev/pts/0

步骤五:管道操作实践

借助于管道符“|”,可以将一条命令的标准输出交给另一条命令处理,在一条命令行内可依次使用多个管道。
1)统计/etc/目录下普通文件(长格式看属性是以-开头)的数量。
[root@svr5 ~]# ls -lR /etc | grep -c ‘^-’
1465
2)列出Yum库里名称中含cluster的软件包。

[root@svr5 ~]# yum list | grep cluster
cluster-cim.x86_64 				0.12.1-7.el5 	RHEL5-Cluster
cluster-snmp.x86_64 				0.12.1-7.el5 	RHEL5-Cluster
lvm2-cluster.x86_64 				2.02.88-9.el5 	RHEL5-ClusterStorage
modcluster.x86_64 				0.12.1-7.el5 	RHEL5-Cluster
system-config-cluster.noarch 	1.0.57-16 		RHEL5-Cluster

3)将ls命令的man手册页导出为普通文本文件。

[root@svr5 ~]# man ls | col -b > ls-man.txt  	//使用col过滤控制字符
[root@svr5 ~]# unix2dos ls-man.txt  			//转换为Windows文本格式
unix2dos: converting file ls-man.txt to DOS format ...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: