Shell basic application

Copyright: Susu acridine https://blog.csdn.net/weixin_44774638/article/details/91489119

Question
in this case require familiarity with the characteristics of Linux Shell environment, mainly to practice the following:
1) switch users Shell environment
2) exercise command history, command aliases
3) redirect the standard input / output / error output
4) pipeline operating practices
program
steps
to achieve this case requires the following steps.

Step a: a user switch environment Shell

If you need to use another temporary Shell environment, you can perform a corresponding Shell interpreter program directly, so long as such may be switched to zsh zsh execute the command line environment.

[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环境

If you wish to modify the user's login Shell, administrators can set up directly by the usermod command. For example, the following may be the user's login zhangsan to 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  			//修改后

For the average user, you can use the chsh command to change your login Shell. For example, the user zhangsan perform the following actions will be re-read their own login 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  			//修改后

Step two: Practice Command History

1) Check the history command capacity.
The default record 1000, HISTSIZE set by the global variable, valid for all users:

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

2) View history list of commands.
It lists the 10 most recently executed command history:

[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) invokes the specified commands.
Article 1028 re-execute the operation command history list:

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

Re-run last start with cat (change the actual situation) the history commands Action:

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

4) Clear history recorded commands.
[root @ svr5 ~] # history -c // erase your history command
[root @ svr5 ~] #> ~ / .bash_history // clear the log file
[root @ svr5 ~] # history // history command again to check the list of
42 > ~ / .bash_history
43 is History

Step three: exercise command aliases

1) Check command has defined a list of aliases.
The current list of aliases:

[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'

Alias ​​generally stored in the user's .bashrc file:

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

2) to define an alias from a new command
to add a command called lh alias, the actual implementation is "ls -lh -color = tty":

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

Alias ​​command to verify the effect of:

[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) cancel alias
cancel a single alias:

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

Step Four: redirect standard input / output / error output

1) view the device file.
Standard input (stdin), number 0 is described; standard output (stdout), as described in No. 1; standard error (stderr), No. 2 is described. Three are in the / dev / directory has linked files, run files point to the / proc file system.

[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) the standard output redirection.
Use> command executed properly redirect the output to a file:

[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

Operation will overwrite the destination file (to empty, and then write):

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

Additional use >> redirect the output can be achieved:

[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) redirect standard error.
For information command execution error, use> could not be saved, it will still be output to the screen. For example, the ls command to view simultaneously two objects (wherein nofile does not exist), redirect the output:

[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

Use 2> error message may be redirected, for example, perform an erroneous command:

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

Similarly, additional output 2 >> can be achieved:

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

If you want the normal output, error output redirected to the same file, you can use &>:

[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 script applications, the output of some operations may not require the user to see, do not need to do anything, then you can redirect the null device file / dev / null. After the execution, although not "see" any change, but the administrator can decide what action to make (the subsequent chapters learning) based on the value return to the state $? A. For example, using the command to check the user id pingping exists, only need to give the final "YES" or the answer "NO", so that the output can be masked 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) the standard input redirection.
<The use is not common, because it requires a command program supports itself, in fact, related commands directly to the target file as a parameter. For example, cat file and performs cat <file the effect is the same:

[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

Step Five: Pipeline Operation Practice

By means of the pipe symbol "|", may be the standard output of a command to another command processing, a plurality of pipes may be used sequentially in a command line.
1) (see long format attributes statistics are ordinary files in the / etc / directory - the beginning of the) number.
[root @ SVR5 ~] # LS -lR / etc | grep -c '^ -'
1465
2) lists the packages containing the name of Yum Curry cluster of.

[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) The man page for the ls command to export plain text files.

[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 ...

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91489119