bash命令排序与通配符(元字符)

1、命令排序:

1.1、 分号";",在同一行顺序执行多个命令,每个命令用;隔开;后面的命令不依赖前面命令是否执行成功
[root@centos6 test2]# ls; date; touch dd.txt
aa  bb.txt  cc.sh
2019年 11月 25日 星期一 17:02:54 CST
[root@centos6 test2]#
1.2、 && 与 ||
1.2.1、&& 连接两个命令,当第一个命令执行成功后,第二个命令执行,否则第二个命令不执行。
[root@centos6 test2]# cat dd.txt
111
root@centos6 test2]# ls dd.txt && cat dd.txt
dd.txt
111
[root@centos6 test2]# 
[root@centos6 test2]# ls ee.txt && cat dd.txt
ls: 无法访问ee.txt: 没有那个文件或目录
[root@centos6 test2]# 

1.2.2、 || 连接两个命令,当第一个命令执行失败后,第二个命令执行,否则第二个命令不执行。
[root@centos6 test2]# ll ee.txt || cat ee.txt
ls: 无法访问ee.txt: 没有那个文件或目录
cat: ee.txt: 没有那个文件或目录
[root@centos6 test2]# 
[root@centos6 test2]# ll ee.txt || > ee.txt
ls: 无法访问ee.txt: 没有那个文件或目录
[root@centos6 test2]# 
[root@centos6 test2]# ls ee*
ee.txt

1.2.3、 && 和 || 同时使用:如果第一个命令执行成功,则执行&&后的命令;如果第一命令执行失败,则执行 || 后的命令
##192.168.24.130能ping通
[root@centos6 test2]# ping -c1 192.168.24.130
PING 192.168.24.130 (192.168.24.130) 56(84) bytes of data.
64 bytes from 192.168.24.130: icmp_seq=1 ttl=64 time=0.286 ms

--- 192.168.24.130 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.286/0.286/0.286/0.000 ms
[root@centos6 test2]# 
##192.168.24.131不能ping通
[root@centos6 test2]# ping -c1 192.168.24.131
PING 192.168.24.131 (192.168.24.131) 56(84) bytes of data.
From 192.168.24.129 icmp_seq=1 Destination Host Unreachable

--- 192.168.24.131 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 3001ms

[root@centos6 test2]# ping -c1 192.168.24.130 &>/dev/null && echo "..ok" || echo "..fail"
..ok
[root@centos6 test2]# 
[root@centos6 test2]# ping -c1 192.168.24.131 &>/dev/null && echo "..ok" || echo "..fail"
..fail

2、通配符(元字符)

2.1、* 匹配任意多个字符
[root@centos6 test2]# ll *.txt
-rw-r--r--. 1 root root 0 11月 25 17:02 bb.txt
-rw-r--r--. 1 root root 4 11月 25 17:05 dd.txt
-rw-r--r--. 1 root root 0 11月 25 17:10 ee.txt
-rw-r--r--. 1 root root 0 11月 25 17:21 fff.txt
-rw-r--r--. 1 root root 0 11月 25 17:21 k.txt

2.2、? 匹配任意一个字符
[root@centos6 test2]# ll ??.txt
-rw-r--r--. 1 root root 0 11月 25 17:02 bb.txt
-rw-r--r--. 1 root root 4 11月 25 17:05 dd.txt
-rw-r--r--. 1 root root 0 11月 25 17:10 ee.txt
[root@centos6 test2]# 
[root@centos6 test2]# ll ?.txt
-rw-r--r--. 1 root root 0 11月 25 17:21 k.txt

2.3、[] 匹配括号中任意一个字符
[root@centos6 test2]# ll [b-e][b-e].txt
-rw-r--r--. 1 root root 0 11月 25 17:02 bb.txt
-rw-r--r--. 1 root root 4 11月 25 17:05 dd.txt
-rw-r--r--. 1 root root 0 11月 25 17:10 ee.txt

2.4、() 在子shell中执行
##在当前shell中执行,环境被改变
[root@centos6 test2]# cd /home/; ls
dog  duanyi  hack  jmz  qiaofeng  sx  test1.txt  test2.txt  testuser1  testuser2  tony
[root@centos6 home]# 
[root@centos6 home]# 
[root@centos6 home]# pwd
/home
[root@centos6 home]# 
[root@centos6 home]# cd -
/root/test2
[root@centos6 test2]# 
##在子shell中执行,当前环境不受影响
[root@centos6 test2]# (cd /home/;ls)
dog  duanyi  hack  jmz  qiaofeng  sx  test1.txt  test2.txt  testuser1  testuser2  tony
[root@centos6 test2]# 
[root@centos6 test2]# pwd
/root/test2
[root@centos6 test2]# 

2.5、{} 集合
[root@centos6 test2]# ll {fff,k}.txt
-rw-r--r--. 1 root root 0 11月 25 17:21 fff.txt
-rw-r--r--. 1 root root 0 11月 25 17:21 k.txt
[root@centos7 ddd]# touch test{1..10}.txt
[root@centos7 ddd]# 
[root@centos7 ddd]# ll
总用量 0
-rw-r--r--. 1 root root 0 12月 10 20:38 test10.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test1.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test2.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test3.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test4.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test5.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test6.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test7.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test8.txt
-rw-r--r--. 1 root root 0 12月 10 20:38 test9.txt
[root@centos7 ddd]#
2.6、\ 转义符,让特殊符号回归普通字符
## 特殊字符
[root@centos6 test2]# echo *
aa bb.txt cc.sh dd.txt ee.txt fff.txt k.txt
[root@centos6 test2]# 
##转义特殊字符
[root@centos6 test2]# echo \*
*
[root@centos6 test2]# 
##创建a,b两个文件
[root@centos6 test2]# touch a b
[root@centos6 test2]# ls
a  aa  b  bb.txt  cc.sh  dd.txt  ee.txt  fff.txt  k.txt
[root@centos6 test2]# 
[root@centos6 test2]# 
[root@centos6 test2]# ls a b
a  b
[root@centos6 test2]# 
[root@centos6 test2]# 
##转义空格,创建a b 一个文件
[root@centos6 test2]# touch a\ b
[root@centos6 test2]# 
[root@centos6 test2]# ll
总用量 4
-rw-r--r--. 1 root root 0 11月 25 17:26 a
-rw-r--r--. 1 root root 0 11月 25 17:02 aa
-rw-r--r--. 1 root root 0 11月 25 17:26 a b
-rw-r--r--. 1 root root 0 11月 25 17:26 b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值