欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Shell基本命令

程序员文章站 2022-06-03 11:50:14
...

Shell基本命令

1:任务调度命令

fg:将后台暂停的命令调到前台运行(ctrl+z+z将一个命令发在后台运行,bg可以调到前台运行)

bg:展示后台有运行哪些命令

2:命令排序

1 ";"表示顺序执行冒号前后的两条命令

[[email protected]_0_5_centos etc]# date;echo "输出时间和文本"
2019年 12月 24日 星期二 20:25:27 CST
输出时间和文本

2:"&&“和”||"逻辑判断符号

1: “&&”连接2个命令,如果前一个命令运行失败 后一个命令则不执行

示例1

[[email protected]_0_5_centos etc]# mkdir /11/22/33 && echo "命令指令成功"
mkdir: 无法创建目录"/11/22/33": 没有那个文件或目录

示例2

[[email protected]_0_5_centos etc]# mkdir -p /11/22/33 && echo "命令指令成功"
命令指令成功

2 :"||"连接的2个命令如果前一个命令执行成功则不执行||后面的那个命令,如果前一个命令执行失败则执行||符号后面的命令

示例1:

[[email protected]_0_5_centos etc]# mkdir -p /55/22/33 || echo "命令指令失败"
[[email protected]_0_5_centos etc]# 

示例2:

[[email protected]_0_5_centos etc]# mkdir /77/22/33 || echo "命令指令失败"
mkdir: 无法创建目录"/77/22/33": 没有那个文件或目录
命令指令失败

3: “&&”和“||”的混合使用(/test/tmp目录不存在)

示例:

[[email protected]_0_5_centos etc]# mkdir /test/tmp && each "命令执行成功" || echo "命令指令失败"
mkdir: 无法创建目录"/test/tmp": 没有那个文件或目录
命令指令失败

3:通配符

1 * 匹配 0 或多个字符

a*b,a与b之间可以有任意长度的任意字符, 也可以一个也没有, 如 aabcb, axyzb, a012b, ab

示例1:

有如下数据

[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# ll
总用量 0
-rw------- 1 root root 0 12月 24 20:50 a012b
-rw------- 1 root root 0 12月 24 20:50 aabcb
-rw------- 1 root root 0 12月 24 20:50 ab
-rw------- 1 root root 0 12月 24 20:50 axyzb
[[email protected]_0_5_centos test]# 

执行结果

[[email protected]_0_5_centos test]# ll a*b
-rw------- 1 root root 0 12月 24 20:50 a012b
-rw------- 1 root root 0 12月 24 20:50 aabcb
-rw------- 1 root root 0 12月 24 20:50 ab
-rw------- 1 root root 0 12月 24 20:50 axyzb
[[email protected]_0_5_centos test]# 

2:? 匹配任意单个字符

a?b,a与b之间有且只有一个字符, 可以是任意字符, 如 aab, abb, acb, a0b

示例1:(假设文件夹下有aab, abb, acb, a0b四个文件)

[[email protected]_0_5_centos test]# ll ?ab
-rw------- 1 root root 0 12月 24 20:56 aab
[[email protected]_0_5_centos test]# ll a?b
-rw------- 1 root root 0 12月 24 20:56 a0b
-rw------- 1 root root 0 12月 24 20:56 aab
-rw------- 1 root root 0 12月 24 20:56 abb
-rw------- 1 root root 0 12月 24 20:56 acb
[[email protected]_0_5_centos test]# 

3: [list] 匹配 list 中的任意单个字符

a[xyz]b,a与b之间必须也只能有一个字符, 但只能是 x 或 y 或 z, 如 axb, ayb, azb3。

示例1:(假设文件夹下有axb, ayb, azb3个文件)

[[email protected]_0_5_centos test]# ll
总用量 0
-rw------- 1 root root 0 12月 24 21:00 axb
-rw------- 1 root root 0 12月 24 21:00 ayb
-rw------- 1 root root 0 12月 24 21:00 azb3
[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# ll a[xyz]b
-rw------- 1 root root 0 12月 24 21:00 axb
-rw------- 1 root root 0 12月 24 21:00 ayb
[[email protected]_0_5_centos test]# 

4: [!list] 匹配除 list 中的任意单一字符

a[!0-9]b,a与b之间必须也只能有一个字符, 但不能是阿拉伯数字, 如 axb, aab, a-b。

示例1:(假设文件夹下有axb, ayb, azb个文件)

[[email protected]_0_5_centos test]# ll
总用量 0
-rw------- 1 root root 0 12月 24 21:03 axb
-rw------- 1 root root 0 12月 24 21:03 ayb
-rw------- 1 root root 0 12月 24 21:03 azb
[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# ll a[!xyz]b
ls: 无法访问a[!xyz]b: 没有那个文件或目录
[[email protected]_0_5_centos test]# ll a[!xy]b
-rw------- 1 root root 0 12月 24 21:03 azb
[[email protected]_0_5_centos test]# 

5: [c1-c2] 匹配 c1-c2 中的任意单一字符

a[0-9]b,匹配0与9之间其中一个字符,如 a0b, a1b… a9b

示例1:(文件夹下有a0b, a1b… a9b 10个文件)

[[email protected]_0_5_centos test]# ls
a0b  a1b  a2b  a3b  a4b  a5b  a6b  a7b  a8b  a9b
[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# ll a[3-7]b
-rw------- 1 root root 0 12月 24 21:07 a3b
-rw------- 1 root root 0 12月 24 21:07 a4b
-rw------- 1 root root 0 12月 24 21:07 a5b
-rw------- 1 root root 0 12月 24 21:07 a6b
-rw------- 1 root root 0 12月 24 21:07 a7b
[[email protected]_0_5_centos test]# 

6: {s1…s2} 匹配 s1 到 s2 这个段的字符串

示例1:(创建a0b,a1b ,a2b … a9b 10个文件)

[[email protected]_0_5_centos test]# ll
总用量 0
[[email protected]_0_5_centos test]# touch a{0..9}b
[[email protected]_0_5_centos test]# 
[[email protected]_0_5_centos test]# ls
a0b  a1b  a2b  a3b  a4b  a5b  a6b  a7b  a8b  a9b
[[email protected]_0_5_centos test]# 

4:文本带颜色或者背景色输出

1: 31-37 输出文本带颜色 echo -e “\e[1;33m文本信息\e[0m”

示例1:

[[email protected]_0_5_centos etc]# echo -e "\e[1;33m文本信息\e[0m"
文本信息(这个是有颜色的)
[[email protected]_0_5_centos etc]# 

示例2:

[[email protected]_0_5_centos etc]# echo -e "\e[1;33m${JAVA_HOME}\e[0m"
/usr/java/jdk1.8.0_131
[[email protected]_0_5_centos etc]# 

2: 41-47 输出文本带背景颜色 echo -e “\e[1;43m文本信息\e[0m”

示例1:

[[email protected]_0_5_centos etc]# echo -e "\e[1;43m${JAVA_HOME}\e[0m" 
/usr/java/jdk1.8.0_131
[[email protected]_0_5_centos etc]# 
相关标签: shell脚本