达内教育
最新版Linux Shell脚本全套,终于讲明白了!
丁明一老师
shell编程01_Shell基础

shell命令 需要shell命令解释器
常见的shell解释器
/bin/bash
/bin/sh
/bin/csh
/bin/tcsh
解释器:将用户的指令翻译为内核可以识别的指令
通过usermod、chsh可以更改登录shell
创建临时的用户的时候,如果我们没有指定指定解释器的时候,
默认的解释器为/bin/bash
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# useradd test1
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
- test1:x:1001:1001::/home/test1:/bin/sh
usermod chsh 来修改解释器
通过 /etc/shells
可以看到支持的shell解释器
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# cat /etc/shells
- # /etc/shells: valid login shells
- /bin/sh
- /bin/bash
- /usr/bin/bash
- /bin/rbash
- /usr/bin/rbash
- /bin/dash
- /usr/bin/dash
我们也可以安装更多的shell解释器
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# apt-get install tcsh
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following packages were automatically installed and are no longer required:
- linux-headers-5.11.0-27-generic linux-hwe-5.11-headers-5.11.0-27
- linux-image-5.11.0-27-generic linux-modules-5.11.0-27-generic
- linux-modules-extra-5.11.0-27-generic
- Use 'apt autoremove' to remove them.
- The following NEW packages will be installed:
- tcsh
- 0 upgraded, 1 newly installed, 0 to remove and 90 not upgraded.
- Need to get 427 kB of archives.
- After this operation, 1,363 kB of additional disk space will be used.
- Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 tcsh amd64 6.21.00-1 [427 kB]
- Fetched 427 kB in 7s (61.1 kB/s)
- Selecting previously unselected package tcsh.
- (Reading database ... 227919 files and directories currently installed.)
- Preparing to unpack .../tcsh_6.21.00-1_amd64.deb ...
- Unpacking tcsh (6.21.00-1) ...
- Setting up tcsh (6.21.00-1) ...
- update-alternatives: using /bin/tcsh to provide /bin/csh (csh) in auto mode
- Processing triggers for man-db (2.9.1-1) ...
再次查看支持的shell解释器
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# cat /etc/shells
- # /etc/shells: valid login shells
- /bin/sh
- /bin/bash
- /usr/bin/bash
- /bin/rbash
- /usr/bin/rbash
- /bin/dash
- /usr/bin/dash
- /bin/tcsh
- /usr/bin/tcsh
test1 用户默认shell解释器为/bin/bash
修改默认的shell解释器方法
usermod -s /bin/tcsh test
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# usermod -s /bin/tcsh test1
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
- test1:x:1001:1001::/home/test1:/bin/tcsh
可以使用chsh -s /bin/bash test1 修改用户默认shell解释器
chsh:change shell的意思
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# chsh -s /bin/bash test1
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
- test1:x:1001:1001::/home/test1:/bin/bash
为什么默认支持/bin/bash 呢?
因为Bash 很多功能和特性
快捷键 Tab键补齐
快捷键:
Ctrl + a 命令行的最前面
Ctrl + e 命令行的最后面
Ctrl + C 当前执行的命令撤销
Ctrl + l 清屏
Tab键补齐:命令补齐 选项都可以补齐
命令历史(history)
通过上下键翻阅历史
命令的别名(aliase)
ll= ls-l
标准输入与输出的重定向(>、 >>、2>、2>>、&>、 &>>)
> : 正确的信息重定向
2>: 错误的信息重定向
&>: 既要正确的信息又要错误的信息
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls
- 42fputcandwrite 84fatherandsoncommunication fl
- 82testpipe 85brotherscommunication shell
- 83mypipe 87fileforprocesscommunication test
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls > a.txt
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
- 42fputcandwrite
- 82testpipe
- 83mypipe
- 84fatherandsoncommunication
- 85brotherscommunication
- 87fileforprocesscommunication
- a.txt
- fl
- shell
- test
为什么还有a.txt自己呢?
ls > a.txt 命令是都是先创建 a.txt文件
然后将ls 的结果输入到a.txt中
如果使用两个大于号的时候,是表示追加
是在原有内容的基础上追加内容
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls >>a.txt
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
- 42fputcandwrite
- 82testpipe
- 83mypipe
- 84fatherandsoncommunication
- 85brotherscommunication
- 87fileforprocesscommunication
- a.txt
- fl
- shell
- test
- 42fputcandwrite
- 82testpipe
- 83mypipe
- 84fatherandsoncommunication
- 85brotherscommunication
- 87fileforprocesscommunication
- a.txt
- fl
- shell
- test
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin#
使用2>将错误信息导出到指定文件
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt
- ls: cannot access 'song.txt': No such file or directory
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin#
使用>只能输出正确的信息。
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt > a.txt
- ls: cannot access 'song.txt': No such file or directory
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
使用2>将错误信息导出到指定文件
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt 2> a.txt
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
- ls: cannot access 'song.txt': No such file or directory
也可以使用&> (也是重新创建文件,会覆盖)可以同时输出正确和错误的信息
单>: 都是重新创建输出的文件,所以会覆盖
双>:都是追加
管道(|)
可以将第一条命令的输出结果作为第二条命令的输入
搜索安装过的软件:
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# apt-cache search all|grep tcsh
- tcsh - TENEX C Shell, an enhanced version of Berkeley csh
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin#
apt-get install ksh
添加新的解释器
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat /etc/shells
- # /etc/shells: valid login shells
- /bin/sh
- /bin/bash
- /usr/bin/bash
- /bin/rbash
- /usr/bin/rbash
- /bin/dash
- /usr/bin/dash
- /bin/tcsh
- /usr/bin/tcsh
- /usr/bin/ksh2020
- /usr/bin/rksh2020
通过ksh 直接使用ksh解释器
ksh中只能通过clear 来清屏
无法通过Ctrl+l
ksh 无法使用方向键 所以默认使用/bin/bash
# ^[[C^[[C^[[C^[[C^[[C^[[D^[[D^[[D^[[D^[[A^[[A^[[A^[[B^[[B
ksh 通过exit返回上次默认的解释器
- # exit
- root@ubuntu-virtual-machine:/home/ubuntu/songweibin#
Shell执行命令的方式
1.交互式(命令行)
人工干预
逐条解释执行 效率低
2.非交互式(脚本)
需要提前设计
批量执行 效率高