shell脚本一些常用的基本特性(二)

bash的基本功能

1、历史命令
1)历史命令的查看

[root@localhost ~]# history [选项] [历史命令保存文件]

[root@bogon ~]# help history history: history [-c] [-d 偏移量] [n] 或
history -anrw [文件名] 或 history -ps 参数 [参数…]
显示或操纵历史列表。

带行号显示历史列表,将每个被修改的条目加上前缀 `*'。
参数 N 会仅列出最后的 N 个条目。

选项:
  -c	删除所有条目从而清空历史列表。
  -d 偏移量	从指定位置删除历史列表。
  -a	将当前绘画的历史行追加到历史文件中
  -n	从历史文件中读取所有未被读取的行
  -r	读取历史文件并将内容追加到历史列表中
  -w	将当前历史写入到历史文件中并追加到历史列表中
  -p	对每一个 ARG 参数展开历史并显示结果而不存储到历史列表中
  -s	以单条记录追加 ARG 到历史列表中

注意:
history 命令查看的历史命令和~/.bash_history 文件中保存的历史命令是不同的。

因为当前登录操作的命令并没有直接写入~/.bash_history 文件,
而是保存在缓存当中的。需要等当前用户注销之后,缓存中的命令才会写入~/.bash_history 文件。如果我们需要把内存中的命令直接写
入~/.bash_history 文件,而不等用户注销时再写入,就需要使用“-w”选项了。

2) 、历史命令的调用

如果想要使用原先的历史命令有这样几种方法:
使用上、下箭头调用以前的历史命令
使用“!n”重复执行第 n 条历史命令
使用“!!”重复执行上一条命令
使用“!字串”重复执行最后一条以该字串开头的命令
使用“!$”重复上一条命令的最后一个参数

2、命令与文件的补全

按tab键
Bash 命令补全增强软件包 bash-completion,这个包支持对 systemctl 命令服务名称的补全

3.命令别名

[root@localhost ~]# alias 别名='原命令'

[root@bogon ~]# alias tom='ls -l'
[root@bogon ~]# tom
总用量 16
-rw-r--r--. 1 root root 8580 10月 29 11:47 192.168.150.128.xsh
-rw-------. 1 root root 1131 10月 25 09:15 anaconda-ks.cfg

别名的优先级比命令高,那么命令执行时具体的顺序是:
1、 第一顺位执行用绝对路径或相对路径执行的命令。
2、 第二顺位执行别名。
3、第三顺位执行 Bash 的内部命令。
4 第四顺位执行按照$PATH 环境变量定义的目录查找顺序找到的第一个命令。
为了让这个别名永久生效,可以把别名写入环境变量配置“~/.bashrc”。

4、Bash 常用快捷键

ctrl+A 把光标移动到命令行开头
ctrl+E 把光标移动到命令行结尾
ctrl+C 强制终止当前的命令
ctrl+L 清屏,相当于clear 命令
ctrl+U 删除或剪切光标之前的命令
ctrl+K 删除或剪切光标之后的内容
ctrl+Y 粘贴 ctrl+U 或ctrl+K 剪切的内容
ctrl+R 在历史命令中搜索
ctrl+D 退出当前终端
ctrl+Z 暂停,并放入后台
ctrl+S暂停屏幕输出
ctrl+Q 恢复屏幕输出

5、输入输出重定向

1)bash标准的输出

设备          设备文件名          文件描述符         类型
键 盘         /dev/stdin           0             标准输入
显示器        /dev/stdout           1             标准输出
显示器        /dev/stderr           2             标准错误输出

2) 输入重定向
<
3) 输出重定向

(1)标准输出重定向: 
1.命令 > 文件 以覆盖的方式,把命令的正确输出输出到指定的文件或设备当中。
2.命令 >> 文件 以追加的方式,把命令的正确输出输出到指定的文件或设备当中。
(2)标准错误输出重定向:
1.错误命令 2>文件 以覆盖的方式,把命令的错误输出输出到指定的文件或设备当中。
2.错误命令 2>>文件以追加的方式,把命令的错误输出输出到指定的文件或设备当中。
(3)正确输出和错误输出同时保存:
1.命令 > 文件(2>&1)以覆盖的方式,把正确输出和错误输出都保存到同一个文件当中。
2.命令 >> 文件(2>&1)以追加的方式,把正确输出和错误输出都保存到同一个文件当中。

6、命令执行顺序

; --命令的顺序执行
[root@bogon ~]# ls -l ; date
总用量 16
-rw-r--r--. 1 root root 8580 10月 29 11:47 192.168.150.128.xsh
-rw-------. 1 root root 1131 10月 25 09:15 anaconda-ks.cfg
2021年 01月 04日 星期一 11:44:49 CST
[root@bogon ~]# date ; ls -l
2021年 01月 04日 星期一 11:45:02 CST
总用量 16
-rw-r--r--. 1 root root 8580 10月 29 11:47 192.168.150.128.xsh
-rw-------. 1 root root 1131 10月 25 09:15 anaconda-ks.cfg
&& --前面命令执行不成功,后面的命令不执行
[root@bogon ~]# ls -l && date
总用量 16
-rw-r--r--. 1 root root 8580 10月 29 11:47 192.168.150.128.xsh
-rw-------. 1 root root 1131 10月 25 09:15 anaconda-ks.cfg
2021年 01月 04日 星期一 11:45:56 CST
[root@bogon ~]# ls --l && date
ls: option '--l' is ambiguous; possibilities: '--literal' '--lcontext'
Try 'ls --help' for more information.
|| --如果前面命令成功,后面就不执行,如果前面不成功后面就执行
[root@bogon ~]# ls -l || date
总用量 16
-rw-r--r--. 1 root root 8580 10月 29 11:47 192.168.150.128.xsh
-rw-------. 1 root root 1131 10月 25 09:15 anaconda-ks.cfg
[root@bogon ~]# ls --l || date
ls: option '--l' is ambiguous; possibilities: '--literal' '--lcontext'
Try 'ls --help' for more information.
2021年 01月 04日 星期一 11:47:43 CST

7、管道

当在两个命令之间设置管道时,管道符|左边命令的输出就变成了右边命令的输入
只要第一个命令向标准输出写入,而第二个命令是从标准输入读取,那么这两个命令就可以形成一个管道。
[root@bogon ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

8、通配符

? 匹配一个任意字符
* 匹配 0 个或任意多个任意字符,也就是可以匹配任何内容
[] 匹配中括号中任意一个字符。
[-]匹配中括号中任意一个字符, -代表一个范围。
[^] 逻辑非,表示匹配不是中括号内的一个字符。

[[:class:]]:匹配一个属于指定字符类中的字符,[:class:]表示一种字符类,比如数字、大小写字母
等。

[:alnum:] :匹配任意一个字母或者数字 ,传统UNIX写法:a-zA-Z0-9
[:alpha:]:匹配任意一个字母,传统UNIX写法: a-zA-Z
[:digit:] :匹配任意一个数字,传统UNIX写法: 0-9
[:lower:]: 匹配任意一个小写字母,传统UNIX写法: a-z
[:upper:] : 匹配任意一个大写字母,传统UNIX写法:A-Z

1、*:匹配任意长度的任意字符,就是说“什么都可以”。
/etc/fs* #/etc中以fs开头的文件将被匹配
[root@bogon ~]# cat /etc/fs* | grep etc
# /etc/fstab
/tmp/my* #/tmp中以my开头的文件将被匹配
[root@bogon ~]# touch /tmp/my{1..10}
[root@bogon ~]# ls -l /tmp/my*
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my1
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my10
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my2
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my3
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my4
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my5
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my6
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my7
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my8
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my9

2、?:与任何单个字符匹配。
my? #文件名为my且后面跟任一单个字符的任何文件将被匹配
[root@bogon ~]# ls -l /tmp/my?
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my1
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my2
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my3
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my4
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my5
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my6
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my7
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my8
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my9

/tmp/notes?txt #如果/tmp/notes.txt和/tmp/notes_txt都存在的话他们都会被匹配
[root@bogon ~]# touch /tmp/notes.txt
[root@bogon ~]# touch /tmp/notes_txt
[root@bogon ~]# ls -l /tmp/notes?txt
-rw-r--r--. 1 root root 0 1月   4 11:58 /tmp/notes_txt
-rw-r--r--. 1 root root 0 1月   4 11:58 /tmp/notes.txt

3、[ ]:与?相似,可以匹配一个括号内的字符,也可以用“-”进行范围指定。
my[12] #将与my1和my2匹配
[root@bogon ~]# ls -l /tmp/my[12]
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my1
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my2
[root@bogon ~]# ls -l /tmp/my[1-5]
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my1
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my2
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my3
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my4
-rw-r--r--. 1 root root 0 1月   4 11:55 /tmp/my5

[Cc]hange[Ll]og #Changelog、ChangeLog、changeLog以及changelog都可匹配,与大写形式的
变形匹配时,使用括弧通配符很有用
ls /etc/[0-9] #将列出 /etc 中以数字开头的所有文件。
[root@bogon ~]# touch /tmp/{1..10}
[root@bogon ~]# ls -l /tmp/[0-9]
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/1
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/2
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/3
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/4
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/5
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/6
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/7
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/8
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/9
ls /tmp/[A-Za-z] #将列出/tmp中以大写字母或小写字母开头的所有文件。
[root@bogon ~]# ls -l /tmp/[a-z]
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/a
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/b
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/c
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/d
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/e
-rw-r--r--. 1 root root 0 1月   4 12:02 /tmp/f

4、[!]:括号内的“!”代表非的意思,即不与括弧中的字符匹配。
[root@bogon ~]# ls -l /tmp/[!0]
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/1
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/2
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/3
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/4
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/5
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/6
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/7
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/8
-rw-r--r--. 1 root root 0 1月   4 12:01 /tmp/9

rm myfile[!9] #删除除了myfile9之外的名为myfile加一个字符的所有文件
5、{}生成序列
touch file{1..9}.txt #当前路径生成file1.txt~file9.txt。{a..f}代表a-f,不连续的使用,分隔,比如f{1,3,5}.txt
[root@bogon ~]# touch f{1..9}
[root@bogon ~]# ls
192.168.150.128.xsh  anaconda-ks.cfg  f1  f2  f3  f4  f5  f6  f7  f8  f9
[root@bogon ~]# touch x{1,3,5}
[root@bogon ~]# ls x{1,3,5}
x1  x3  x5
6、使用{}备份
cp file1.txt{,.bak} #将fiel1.txt复制一份叫file1.txt.bak
cp file{2,22}.txt #复制file2.txt为file22.txt
[root@bogon ~]# touch helloword
[root@bogon ~]# echo helloword > helloword 
[root@bogon ~]# cat helloword 
helloword
[root@bogon ~]# cp helloword{,.bak}
[root@bogon ~]# cat helloword.bak 
helloword

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值