1、多命令顺序执行
这些执行符都要是英文的
多命令执行符 | 格式 | 作用 |
---|---|---|
; | 命令1;命令2 | 按照顺序一个一个执行。某个命令报错,会忽略,继续执行下面的命令 |
&& | 命令1&&命令2 | 当命令1正确执行时,才会执行命令2 。 |
|| | 命令1||命令2 | 当命令1执行错误时,会执行命令2。1正确则只执行1,不执行2 |
1.1.1 练习:执行符;
[root@catyuan ~]# date;ls;pwd
Wed Jun 5 07:42:39 CST 2019
anaconda-ks.cfg Documents hello.sh la.log Music Public test1.log test.log
Desktop Downloads initial-setup-ks.cfg ll.log Pictures Templates test2.log Videos
/root
1.1.2 练习:执行符&&
[root@catyuan ~]# da&&ls
bash: da: command not found...
[root@catyuan ~]# date&&ls
Wed Jun 5 07:44:12 CST 2019
anaconda-ks.cfg Documents hello.sh la.log Music Public test1.log test.log
Desktop Downloads initial-setup-ks.cfg ll.log Pictures Templates test2.log Videos
[root@catyuan ~]# date&&la
Wed Jun 5 07:44:21 CST 2019
bash: la: command not found...
1.1.3 练习:执行符||
[root@catyuan ~]# da||ls
bash: da: command not found...
anaconda-ks.cfg Documents hello.sh la.log Music Public test1.log test.log
Desktop Downloads initial-setup-ks.cfg ll.log Pictures Templates test2.log Videos
[root@catyuan ~]# date||ls
Wed Jun 5 07:46:04 CST 2019
[root@catyuan ~]# date||la
Wed Jun 5 07:46:43 CST 2019
1.1.4 练习:命令正确会打印yes,错误打印no
[root@catyuan ~]# ll && echo yes || echo no
total 32
-rw-------. 1 root root 2084 Nov 13 2018 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Nov 13 2018 Desktop
drwxr-xr-x. 2 root root 6 Nov 13 2018 Documents
drwxr-xr-x. 2 root root 6 Nov 13 2018 Downloads
-rw-r--r--. 1 root root 62 Jun 5 04:49 hello.sh
-rw-r--r--. 1 root root 2177 Nov 13 2018 initial-setup-ks.cfg
-rw-r--r--. 1 root root 31 Jun 5 06:36 la.log
-rw-r--r--. 1 root root 633 Jun 5 06:34 ll.log
drwxr-xr-x. 2 root root 6 Nov 13 2018 Music
drwxr-xr-x. 2 root root 6 Nov 13 2018 Pictures
drwxr-xr-x. 2 root root 6 Nov 13 2018 Public
drwxr-xr-x. 2 root root 6 Nov 13 2018 Templates
-rw-r--r--. 1 root root 837 Jun 5 07:13 test1.log
-rw-r--r--. 1 root root 31 Jun 5 07:11 test2.log
-rw-r--r--. 1 root root 764 Jun 5 07:06 test.log
drwxr-xr-x. 2 root root 6 Nov 13 2018 Videos
yes
2、管道符
命令1 | 命令2 命令1的正确输出作为命令2的操作对象
2.1.1 练习:过滤ll命令结果中有test的行
[root@catyuan ~]# ll | grep test
-rw-r--r--. 1 root root 837 Jun 5 07:13 test1.log
-rw-r--r--. 1 root root 31 Jun 5 07:11 test2.log
-rw-r--r--. 1 root root 764 Jun 5 07:06 test.log
[root@catyuan ~]# ll
total 32
-rw-------. 1 root root 2084 Nov 13 2018 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Nov 13 2018 Desktop
drwxr-xr-x. 2 root root 6 Nov 13 2018 Documents
drwxr-xr-x. 2 root root 6 Nov 13 2018 Downloads
-rw-r--r--. 1 root root 62 Jun 5 04:49 hello.sh
-rw-r--r--. 1 root root 2177 Nov 13 2018 initial-setup-ks.cfg
-rw-r--r--. 1 root root 31 Jun 5 06:36 la.log
-rw-r--r--. 1 root root 633 Jun 5 06:34 ll.log
drwxr-xr-x. 2 root root 6 Nov 13 2018 Music
drwxr-xr-x. 2 root root 6 Nov 13 2018 Pictures
drwxr-xr-x. 2 root root 6 Nov 13 2018 Public
drwxr-xr-x. 2 root root 6 Nov 13 2018 Templates
-rw-r--r--. 1 root root 837 Jun 5 07:13 test1.log
-rw-r--r--. 1 root root 31 Jun 5 07:11 test2.log
-rw-r--r--. 1 root root 764 Jun 5 07:06 test.log
drwxr-xr-x. 2 root root 6 Nov 13 2018 Videos
2.1.2 练习:ll命令结果中有test的行,统计有几行含有test
[root@catyuan ~]# ll | grep test | wc -l
3
2.1.3 练习:查看有多少个客户端连接了我们的服务器
[root@catyuan ~]# netstat -na | grep established |wc -l
2
3、通配符
主要用来匹配文件名或目录
通配符 | 作用 |
---|---|
? | 匹配一个任意字符 |
* | 匹配0或任意多个任意字符,也就是可以匹配任何内容 |
[] | 匹配中括号中任意一个字符 |
[-] | 匹配中括号中任意一个字符,-代表范围。如:[a-c] |
[^] | 逻辑非,表示匹配不是中括号内的字符 |
练习:使用通配符
第一步:创建一些文件来提供完美练习
[root@catyuan ~]# mkdir /test
[root@catyuan ~]# cd /test/
[root@catyuan test]# touch test1
[root@catyuan test]# touch test11
[root@catyuan test]# touch test113
[root@catyuan test]# touch test2
[root@catyuan test]# touch testabc
[root@catyuan test]# ls
test1 test11 test113 test2 testabc
第二步:练习*
[root@catyuan test]# ls test*
test1 test11 test113 test2 testabc
第三步:练习?
[root@catyuan test]# ls test?
test1 test2
[root@catyuan test]# ls test???
test113 testabc
第四步:练习[]
[root@catyuan test]# ls test[123]
test1 test2
第四步:练习[-]
[root@catyuan test]# ls test[0-9]
test1 test2
[root@catyuan test]# ls test[0-9][0-9]
test11
[root@catyuan test]# ls test[0-9][0-9][0-9]
test113
[root@catyuan test]# ls test[a-z][a-z][a-z]
testabc
第五步:练习[^]
[root@catyuan test]# ls test[^0-9][^0-9][^0-9]
testabc
4、bash中一些其他的特殊符号
符号 | 作用 |
---|---|
‘’ | 单引号。在单引号中的所有特殊符号都没有特殊含义 |
“” | 双引号。在双引号中的特殊符号都没有特殊含义,但是“$”、“·”和“\”是例外,拥有“调用变量”、‘’引用命令”、“转义符”的特殊含义 |
·· | 反引号。里面的内容是系统命令,在bash中会先执行它。和 ( ) 作 用 一 样 , 推 荐 ()作用一样,推荐 ()作用一样,推荐$(),反引号易出错 |
$() | 和反引号一样,用来引用系统命令 |
# | shell脚本中,#开头的行代表注释 |
$ | 用于调用变量的值 |
\ | 转义符 |
4.1.1 练习:$、单引号、双引号
[root@catyuan test]# a=1
[root@catyuan test]# echo $a
1
[root@catyuan test]# echo '$a'
$a
[root@catyuan test]# echo "$a"
1
4.1.2 练习:反引号、$()
[root@catyuan test]# b=`ls`
[root@catyuan test]# echo "$b"
test1
test11
test113
test2
testabc
[root@catyuan test]# b=$(ls)
[root@catyuan test]# echo $b
test1 test11 test113 test2 testabc
[root@catyuan test]# echo "$b"
test1
test11
test113
test2
testabc
4.1.3 练习:转义符
[root@catyuan test]# echo \$b
$b