博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux Shell 基础语法学习
阅读量:4229 次
发布时间:2019-05-26

本文共 30694 字,大约阅读时间需要 102 分钟。

Shell 的基本语法可能并不算复杂,但是在 Linux 强大 buff 的加持之下,高手却能用它创造出变化万千。

hello world

[root@master ~]# echo "hello world"hello world[root@master shell_learning]# ./test.sh-bash: ./test.sh: Permission denied[root@master shell_learning]# chmod +x ./test.sh[root@master shell_learning]# ./test.shhello world

${}、$()、$(()) 

${}:对变量的替换,同 $var。

[root@master shell_learning]# a=Linux[root@master shell_learning]# echo $ab[root@master shell_learning]# echo ${a}bLinuxb

${} :变量设定。

命令 分隔符 说明 结果
初始化:[root@master shell_learning]# file=/dir1/dir2/dir3/my.file.txt
echo ${file#*/} / 拿掉 file 中第一个 / 及其左边的字符串 dir1/dir2/dir3/my.file.txt
echo ${file##*/} / 拿掉 file 中最后一个 / 及其左边的字符串 my.file.txt
echo ${file#*.} . 拿掉 file 中第一个 . 及其左边的字符串 file.txt
echo ${file##*.} . 拿掉 file 中最后一个 . 及其左边的字符串 txt
echo ${file%/*} / 拿掉 file 中最后一个 / 及其右边的字符串 /dir1/dir2/dir3
echo ${file%%/*} / 拿掉 file 中第一个 / 及其右边的字符串 (空值)
echo ${file%.*} . 拿掉 file 中最后一个 . 及其右边的字符串 /dir1/dir2/dir3/my.file
echo ${file%%.*} . 拿掉 file 中第一个 . 及其右边的字符串 /dir1/dir2/dir3/my
echo ${file/dir/path}   将 file 中第一个 dir 替换为 path /path1/dir2/dir3/my.file.txt
echo ${file//dir/path}   将 file 中所有的 dir 替换为 path /path1/path2/path3/my.file.txt
                                                                                               注解

# 是去掉左边

(在键盘上 # 在 $ 的左边)

一个 # 表示左边开始最小匹配,## 表示从左边开始最大匹配(贪婪匹配),

一个 % 表示右边开始最小匹配,%% 表示从右边开始最大匹配(贪婪匹配),

% 是去掉右边

(在键盘上 % 在 $ 的右边)

* 是用来匹配不要的字符,也就是想要去掉的那部分。

还可以指定字符分隔符号,与 * 配合,决定取哪部分。

 
echo ${#file}   获取 file 变量长度 27
echo ${file-my.file.txt}   若 $file 没设定,则使用 my.file.txt 作传回值 /dir1/dir2/dir3/my.file.txt
echo ${file:-my.file.txt}   若 $file 没有设定或为空值,则使用 my.file.txt 作传回值 /dir1/dir2/dir3/my.file.txt
echo ${file+my.file.txt}   若 $file 设为空值或非空值,均使用 my.file.txt 作传回值 my.file.txt
echo ${file:+my.file.txt}   若 $file 为非空值,则使用 my.file.txt 作传回值 my.file.txt
echo ${file=txt}   若 $file 没设定,则回传 txt ,并将 $file 赋值为 txt /dir1/dir2/dir3/my.file.txt
echo ${file:=txt}   若 $file 没设定或空值,则回传 txt ,将 $file 赋值为 txt /dir1/dir2/dir3/my.file.txt
echo ${file?my.file.txt}   若 $file 没设定,则将 my.file.txt 输出至 STDERR /dir1/dir2/dir3/my.file.txt
echo ${file:?my.file.txt}   若 $file 没设定或空值,则将 my.file.txt 输出至 STDERR /dir1/dir2/dir3/my.file.txt
 
初始化:[root@master shell_learning]# A=(a b c def)  # 定义字符数组
echo ${A[@]}   返回数组全部元素 a b c def
echo ${A[*]}   返回数组全部元素 a b c def
echo ${A[0]}   返回数组第一个元素 a
echo ${#A[@]}   返回数组元素总个数 4
echo ${#A[*]}   返回数组元素总个数 4
echo ${#A[3]}   返回第四个元素的长度,即 def 的长度 3
echo A[3]=xzy   则是将第四个组数重新定义为 xyz  

 $():对命令的替换,同 ``(反引号)。

[root@master shell_learning]# lstest_dir  test.sh  test.txt[root@master shell_learning]# a=$(ls)[root@master shell_learning]# echo $atest_dir test.sh test.txt[root@master shell_learning]# a=`ls`[root@master shell_learning]# echo $atest_dir test.sh test.txt

$(()):对内部内容进行整数运算 $((var1*var2))。或将其他进制转换为十进制 $((N#var)),其中 N 为进制。

[root@master shell_learning]# echo $(2*3)-bash: 2*3: command not found[root@master shell_learning]# echo $((2*3))6[root@master shell_learning]# a=5;b=7;c=2[root@master shell_learning]# echo $((a+b*c))19[root@master shell_learning]# echo $(($a+$b*$c))19
[root@master shell_learning]# echo $((2#110))6[root@master shell_learning]# echo $((16#FF))255[root@master shell_learning]# echo $((16#2a))42[root@master shell_learning]# echo $((16#2A))42
[root@master shell_learning]# a=5;b=7[root@master shell_learning]# ((a++))[root@master shell_learning]# echo $a6[root@master shell_learning]# ((a--)); echo $a5[root@master shell_learning]# ((a
b)); echo $?1

Shell 变量

变量名和等号之间不能有空格,这个其实也好理解,因为在 Linux 当中,默认把空格当成命令行参数分隔的标识,如果有空格的话,第一个参数就被 Shell 当成命令了。

使用变量

[root@master shell_learning]# name='hello'[root@master shell_learning]# echo $namehello
for skill in Ada Coffe Action Java; do    echo "I am good at ${skill}Script"done------------------------------------------------------------[root@master shell_learning]# ./test.shI am good at AdaScriptI am good at CoffeScriptI am good at ActionScriptI am good at JavaScript

赋值变量

使用变量的时候才加美元符($),赋值的时候不用。

I am good at JavaScript[root@master shell_learning]# your_name="tom"[root@master shell_learning]# echo $your_nametom[root@master shell_learning]# your_name="alibaba"[root@master shell_learning]# echo $your_namealibaba

只读变量

#!/bin/bashmyUrl="https://www.google.com"readonly myUrlmyUrl="https://www.runoob.com"------------------------------------------------------------[root@master shell_learning]# ./test.sh./test.sh: line 4: myUrl: readonly variable

删除变量

#!/bin/shmyUrl="https://www.runoob.com"unset myUrlecho $myUrl------------------------------------------------------------[root@master shell_learning]# ./test.sh

Shell 字符串

单引号

单引号字符串的限制:

  • 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
  • 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。
[root@master shell_learning]# echo 'Hello, I know you are \"$your_name\"! \n'Hello, I know you are \"$your_name\"! \n

双引号

双引号的优点:

  • 双引号里可以有变量
  • 双引号里可以出现转义字符
[root@master shell_learning]# echo "Hello, I know you are \"$your_name\"! \n"Hello, I know you are "alibaba"! \n

 字符串拼接

# test.shyour_name="Looking"greeting="hello, "$your_name" !"greeting2="hello, $your_name !"greeting3="hello, ${your_name} !"echo $greetingecho $greeting2echo $greeting3greeting4='hello, '$your_name' !'greeting5='hello, ${your_name} !'echo $greeting4echo $greeting5------------------------------------------------------------[root@master shell_learning]# ./test.sh hello, Looking !hello, Looking !hello, Looking !hello, Looking !hello, ${your_name} !

获取字符串长度

[root@master shell_learning]# string='abcd'[root@master shell_learning]# echo $stringabcd[root@master shell_learning]# echo ${#string}4

提取子字符串

[root@master shell_learning]# string="runoob is a great site"# 这儿要给变量加上 {} 帮助 Shell 识别边界哟[root@master shell_learning]# echo ${string:1:4} # 输出 unoounoo[root@master shell_learning]# echo $string:1:4runoob is a great site:1:4

查找子字符串

查找字符 i 或 o 的位置(哪个字母先出现就计算哪个):

string="runoob is a great site"echo `expr index "$string" io`[root@master shell_learning]# ./test.sh 4[root@master shell_learning]# expr index "$string" io4

注意: 以上脚本中 ` 是反引号,而不是单引号 ',不要看错了哦,相信你在 MySQL 脚本当中也见过这个反引号。

Shell 数组

定义数组

array_name=('hello' 'world' '!')array_name2=('hello', 'world', '!')array_name3=('hello''world''!')array_name4=()array_name4[0]='hello'array_name4[1]='world'array_name4[2]='!'

读取数组

array_name=('hello' 'world' '!')array_name3=('hello''world''!')array_name4=()array_name4[0]='hello'array_name4[1]='world'array_name4[2]='!'echo ${array_name[@]}echo ${array_name2[@]}echo ${array_name3[@]}echo ${array_name4[@]}------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !hello world !hello world !

获取数组长度

array_name=('hello' 'world' '!')length=${#array_name[@]}echo $lengthlength_1=${#array_name[1]}echo $length_1------------------------------------------------------------[root@master shell_learning]# ./test.sh 35

Shell 注释

单行注释

以 # 开头的行就是注释,会被解释器忽略。

通过每一行加一个 # 号设置多行注释。

​array_name=('hello' 'world' '!')length=${#array_name[@]}echo $lengthlength_1=${#array_name[1]}echo $length_1#--------------------------------------------# 这是一个注释# author:菜鸟教程# site:www.runoob.com# slogan:学的不仅是技术,更是梦想!#--------------------------------------------##### 用户配置区 开始 ######## 这里可以添加脚本描述信息# ###### 用户配置区 结束  #####
array_name=('hello' 'world' '!')length=${#array_name[@]}echo $lengthlength_1=${#array_name[1]}echo $length_1comment(){-------------------------------------------- 这是一个注释 author:菜鸟教程 site:www.runoob.com slogan:学的不仅是技术,更是梦想!--------------------------------------------#### 用户配置区 开始 ##### 这里可以添加脚本描述信息 #### 用户配置区 结束  #####}

脚本执行时仍然正常输出结果:

[root@master shell_learning]# ./test.sh 35

如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?

每一行加个 # 符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。

多行注释

array_name=('hello' 'world' '!')length=${#array_name[@]}echo $lengthlength_1=${#array_name[1]}echo $length_1:<

EOF 也可以使用其他符号:

array_name=('hello' 'world' '!')length=${#array_name[@]}echo $lengthlength_1=${#array_name[1]}echo $length_1:<

Shell 传递参数

# test.shecho "Shell 传递参数实例!";echo "执行的文件名:$0";echo "第一个参数为:$1";echo "第二个参数为:$2";echo "第三个参数为:$3";------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!执行的文件名:./test.sh第一个参数为:hello第二个参数为:world第三个参数为:!

$# 

传递到脚本的参数个数

# test.shecho "Shell 传递参数实例!"echo "参数个数为:$#"------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!参数个数为:3

$*

以一个单字符串显示所有向脚本传递的参数。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。

# test.shecho "Shell 传递参数实例!"echo "传递的参数作为一个字符串显示:$*";------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!传递的参数作为一个字符串显示:hello world !

$$

脚本运行的当前进程 ID 号

# test.shecho "Shell 传递参数实例!"echo "脚本运行的当前进程ID号:$$";------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!脚本运行的当前进程ID号:5443

$!

# test.shecho "Shell 传递参数实例!"echo "后台运行的最后一个进程的ID号:$!";------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!后台运行的最后一个进程的ID号:

$@

与$*相同,但是使用时加引号,并在引号中返回每个参数。

如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。

# test.shecho "Shell 传递参数实例!"echo "传递的参数作为一个字符串显示:$@";------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!传递的参数作为一个字符串显示::hello world !

$-

显示Shell使用的当前选项,与功能相同。

# test.shecho "Shell 传递参数实例!"echo "$-"------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!hB

$?

显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

# test.shecho "Shell 传递参数实例!"echo "$?"------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !Shell 传递参数实例!0

$* and $@

$* 与 $@ 区别:

  • 相同点:都是引用所有参数。
  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 " * " 等价于 "1 2 3"(传递了一个参数),而 "@" 等价于 "1" "2" "3"(传递了三个参数)
# test.shecho "-- \$* 演示 ---"for i in "$*"; do    echo $idoneecho "-- \$@ 演示 ---"for i in "$@"; do    echo $idone------------------------------------------------------------[root@master shell_learning]# ./test.sh hello world !-- $* 演示 ---hello world !-- $@ 演示 ---helloworld!

参数校验

-n str,字符串不为null,长度大于零。

# test.shif [ -n "$1" ]; then    echo "包含第一个参数"else    echo "没有包含第一参数"fi------------------------------------------------------------[root@master shell_learning]# ./test.sh hello包含第一个参数[root@master shell_learning]# ./test.sh没有包含第一参数

中括号

Shell 里面的中括号(包括单中括号与双中括号)可用于一些条件的测试:

  • 算术比较, 比如一个变量是否为0, [ $var -eq 0 ]
  • 文件属性测试,比如一个文件是否存在,[ -e $var ], 是否是目录,[ -d $var ]
  • 字符串比较, 比如两个字符串是否相同, [[ $var1 = $var2 ]]

Shell 基本运算符

算术运算符

运算符 说明 举例
+ 加法 `expr $a + $b` 结果为 30。
- 减法 `expr $a - $b` 结果为 -10。
* 乘法 `expr $a \* $b` 结果为  200。
/ 除法 `expr $b / $a` 结果为 2。
% 取余 `expr $b % $a` 结果为 0。
= 赋值 a=$b 将把变量 b 的值赋给 a。
== 相等。用于比较两个数字,相同则返回 true。 [ $a == $b ] 返回 false。
!= 不相等。用于比较两个数字,不相同则返回 true。 [ $a != $b ] 返回 true。

注意:条件表达式要放在方括号之间,并且要有空格,例如: [$a==$b] 是错误的,必须写成 [ $a == $b ]

注意:

  • 乘号(*)前边必须加反斜杠(\)才能实现乘法运算(转义);
  • if...then...fi 是条件语句,后续将会讲解。
[root@master ~]# a=10[root@master ~]# b=20[root@master ~]# val=`expr $a + $b`[root@master ~]# echo "a + b = $val"a + b = 30[root@master ~]# val=`expr $a - $b`[root@master ~]# echo "a - b = $val"a - b = -10[root@master ~]# val=`expr $a \* $b`[root@master ~]# echo "a * b = $val"a * b = 200[root@master ~]# val=`expr $b / $a`[root@master ~]# echo "b / a = $val"b / a = 2[root@master ~]# val=`expr $b % $a`[root@master ~]# echo "b % a = $val"b % a = 0[root@master ~]# > if [ $a == $b ]> then>     echo "a == b"> fi[root@master ~]# > if [ $a != $b ]> then>     echo "a != b"> fia != b

关系运算符

运算符 说明 举例
-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
-ne 检测两个数是否不相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
-lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
-ge 检测左边的数是否大于等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
-le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。
[root@master ~]# a=10[root@master ~]# b=20[root@master ~]# > if [ $a -eq $b ]> then>    echo "$a -eq $b : a 等于 b"> else>    echo "$a -eq $b: a 不等于 b"> fi10 -eq 20: a 不等于 b----------------------------------------[root@master ~]# > if [ $a -ne $b ]> then>    echo "$a -ne $b: a 不等于 b"> else>    echo "$a -ne $b : a 等于 b"> fi10 -ne 20: a 不等于 b----------------------------------------[root@master ~]# > if [ $a -gt $b ]> then>    echo "$a -gt $b: a 大于 b"> else>    echo "$a -gt $b: a 不大于 b"> fi10 -gt 20: a 不大于 b----------------------------------------[root@master ~]# > if [ $a -lt $b ]> then>    echo "$a -lt $b: a 小于 b"> else>    echo "$a -lt $b: a 不小于 b"> fi10 -lt 20: a 小于 b----------------------------------------[root@master ~]# > if [ $a -ge $b ]> then>    echo "$a -ge $b: a 大于或等于 b"> else>    echo "$a -ge $b: a 小于 b"> fi10 -ge 20: a 小于 b----------------------------------------[root@master ~]# > if [ $a -le $b ]> then>    echo "$a -le $b: a 小于或等于 b"> else>    echo "$a -le $b: a 大于 b"> fi10 -le 20: a 小于或等于 b

布尔运算符

运算符 说明 举例
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
-o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。
[root@master ~]# a=10[root@master ~]# b=20[root@master ~]# [root@master ~]# > if [ $a != $b ]> then>    echo "$a != $b : a 不等于 b"> else>    echo "$a == $b: a 等于 b"> fi10 != 20 : a 不等于 b----------------------------------------[root@master ~]# > if [ $a -lt 100 -a $b -gt 15 ]> then>    echo "$a 小于 100 且 $b 大于 15 : 返回 true"> else>    echo "$a 小于 100 且 $b 大于 15 : 返回 false"> fi10 小于 100 且 20 大于 15 : 返回 true----------------------------------------[root@master ~]# > if [ $a -lt 100 -o $b -gt 100 ]> then>    echo "$a 小于 100 或 $b 大于 100 : 返回 true"> else>    echo "$a 小于 100 或 $b 大于 100 : 返回 false"> fi10 小于 100 或 20 大于 100 : 返回 true----------------------------------------[root@master ~]#> if [ $a -lt 5 -o $b -gt 100 ]> then>    echo "$a 小于 5 或 $b 大于 100 : 返回 true"> else>    echo "$a 小于 5 或 $b 大于 100 : 返回 false"> fi10 小于 5 或 20 大于 100 : 返回 false

逻辑运算符

运算符 说明 举例
&& 逻辑的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 逻辑的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
[root@master ~]# a=10[root@master ~]# b=20[root@master ~]#> if [[ $a -lt 100 && $b -gt 100 ]]> then>    echo "10 不大于 100 且 20 不小于 100: 返回 true"> else>    echo "10 不大于 100 且 20 不小于 100: 返回 false"> fi10 不大于 100 且 20 不小于 100: 返回 false----------------------------------------[root@master ~]# [root@master ~]#> if [[ $a -lt 100 || $b -gt 100 ]]> then>    echo "10 不大于 100 或 20 不小于 100:返回 true"> else>    echo "10 不大于 100 或 20 不小于 100:返回 false"> fi10 不大于 100 或 20 不小于 100:返回 true

字符串运算符

运算符 说明 举例
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否不为 0,不为 0 返回 true。 [ -n "$a" ] 返回 true。
$ 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。
[root@master ~]# a="abc"[root@master ~]# b="efg"[root@master ~]# [root@master ~]#> if [ $a = $b ]> then>    echo "$a = $b : a 等于 b"> else>    echo "$a = $b: a 不等于 b"> fiabc = efg: a 不等于 b----------------------------------------[root@master ~]#> if [ $a != $b ]> then>    echo "$a != $b : a 不等于 b"> else>    echo "$a != $b: a 等于 b"> fiabc != efg : a 不等于 b----------------------------------------[root@master ~]#> if [ -z $a ]> then>    echo "-z $a : 字符串长度为 0"> else>    echo "-z $a : 字符串长度不为 0"> fi-z abc : 字符串长度不为 0----------------------------------------[root@master ~]#> if [ -n "$a" ]> then>    echo "-n $a : 字符串长度不为 0"> else>    echo "-n $a : 字符串长度为 0"> fi-n abc : 字符串长度不为 0----------------------------------------[root@master ~]#> if [ $a ]> then>    echo "$a : 字符串不为空"> else>    echo "$a : 字符串为空"> fiabc : 字符串不为空

文件测试运算符

操作符 说明 举例
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
[root@master ~]# file="/root/test.txt"[root@master ~]#> if [ -r $file ]> then>    echo "文件可读"> else>    echo "文件不可读"> fi文件可读----------------------------------------[root@master ~]#> if [ -w $file ]> then>    echo "文件可写"> else>    echo "文件不可写"> fi文件可写----------------------------------------[root@master ~]#> if [ -x $file ]> then>    echo "文件可执行"> else>    echo "文件不可执行"> fi文件不可执行----------------------------------------[root@master ~]#> if [ -f $file ]> then>    echo "文件为普通文件"> else>    echo "文件为特殊文件"> fi文件为普通文件----------------------------------------[root@master ~]#> if [ -d $file ]> then>    echo "文件是个目录"> else>    echo "文件不是个目录"> fi文件不是个目录----------------------------------------[root@master ~]#> if [ -s $file ]> then>    echo "文件不为空"> else>    echo "文件为空"> fi文件不为空----------------------------------------[root@master ~]#> if [ -e $file ]> then>    echo "文件存在"> else>    echo "文件不存在"> fi文件存在

Shell echo

显示普通字符串

[root@master ~]# [root@master ~]# echo "It is a test"It is a test

显示转义字符

[root@master ~]# echo "\"It is a test\"""It is a test"

显示变量

[root@master ~]# read nameOK[root@master ~]# echo "$name It is a test"OK It is a test
# test.sh#!/bin/shread nameecho "$name It is a test"------------------------------------------------------------[root@master ~]# ./test.sh OKOK It is a test

显示换行

[root@master ~]# echo -e "OK! \n" # -e 开启转义OK! [root@master ~]# echo "It is a test"It is a test[root@master ~]# echo "OK! \n" # 未开启转义OK! \n

显示不换行

[root@master ~]# echo -e "OK! \c" # -e 开启转义 \c 不换行OK! [root@master ~]# echo "It is a test"It is a test

结果定向至文件

[root@master ~]# echo "It is a test" > myfile[root@master ~]# cat myfileIt is a test

原样输出字符串

[root@master ~]# name="Looking"[root@master ~]# echo '$name\"'$name\"[root@master ~]# echo "$name\""Looking"

显示命令执行结果

注意: 这里使用的是反引号 `(esc 按键下边那个), 而不是单引号 '。

[root@master ~]# dateSun Aug 16 22:31:35 CST 2020[root@master ~]# echo `date`Sun Aug 16 22:31:46 CST 2020

Shell printf

# format-string为双引号[root@master ~]# printf "%d %s\n" 1 "abc"1 abc# 单引号与双引号效果一样 [root@master ~]# printf '%d %s\n' 1 "abc" 1 abc # 没有引号也可以输出[root@master ~]# printf %s abcdefabcdef[root@master ~]# # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用[root@master ~]# printf %s abc defabcdef[root@master ~]# [root@master ~]# printf "%s\n" abc defabcdef[root@master ~]# printf "%s %s %s\n" a b c d e f g h i ja b cd e fg h ij# 如果没有 arguments,那么 %s 用 NULL 代替,%d 用 0 代替[root@master ~]# printf "%s and %d \n" and 0 [root@master ~]# printf "a string, no processing:<%s>\n" "A\nB"a string, no processing:
[root@master ~]# printf "a string, no processing:<%b>\n" "A\nB"a string, no processing:
[root@master ~]# printf "www.runoob.com \a"www.runoob.com [root@master ~]#

Shell test

数值测试

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真
[root@master ~]# num1=100[root@master ~]# num2=100[root@master ~]#> if test $[num1] -eq $[num2]> then>     echo '两个数相等!'> else>     echo '两个数不相等!'> fi两个数相等![root@master ~]#> if [ $num1 == $num2 ]> then>     echo '两个数相等!'> else>     echo '两个数不相等!'> fi两个数相等!
[root@master ~]# a=5[root@master ~]# b=6[root@master ~]# result=$[ a + b ][root@master ~]# echo "result 为: $result"result 为: 11[root@master ~]# result=$[ a - b ][root@master ~]# echo "result 为: $result"result 为: -1

字符串测试

参数 说明
= 等于则为真
!= 不相等则为真
-z 字符串 字符串的长度为零则为真
-n 字符串 字符串的长度不为零则为真
[root@master ~]# num1="ru1noob"[root@master ~]# num2="runoob"[root@master ~]#> if test $num1 = $num2> then>     echo '两个字符串相等!'> else>     echo '两个字符串不相等!'> fi两个字符串不相等![root@master ~]#> if [ $num1 == $num2 ];> then>     echo '两个字符串相等!'> else>     echo '两个字符串不相等!'> fi两个字符串不相等![root@master ~]# num1="runoob"[root@master ~]# num2=""[root@master ~]# test -z "$num1"[root@master ~]# echo $?1[root@master ~]# test -n "$num1"[root@master ~]# echo $?0[root@master ~]# test -z "$num2"[root@master ~]# echo $?0[root@master ~]# test -n "$num2"[root@master ~]# echo $?1

文件测试

参数 说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真
# test.shcd /binif test -e ./bashthen    echo '文件已存在!'else    echo '文件不存在!'fi------------------------------------------------------------[root@master ~]# ./test.sh 文件已存在!

Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。

# test.shcd /binif test -e ./notFile -o -e ./bashthen    echo '至少有一个文件存在!'else    echo '两个文件都不存在'fi------------------------------------------------------------[root@master ~]# ./test.sh 至少有一个文件存在!

其他测试

Shell 流程控制

if 和 if else

[root@master ~]# a=10[root@master ~]# b=20[root@master ~]#> if [ $a == $b ]> then>    echo "a 等于 b"> elif [ $a -gt $b ]> then>    echo "a 大于 b"> elif [ $a -lt $b ]> then>    echo "a 小于 b"> else>    echo "没有符合的条件"> fia 小于 b
[root@master ~]# num1=$[2*3][root@master ~]# num2=$[1+5][root@master ~]#> if test $[num1] -eq $[num2]> then>     echo '两个数字相等!'> else>     echo '两个数字不相等!'> fi两个数字相等!
[root@master ~]# if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fitrue

 for 循环

一行流:for var in item1 item2 ... itemN; do command1; command2… done;

[root@master ~]#> for loop in 1 2 3 4 5> do>     echo "The value is: $loop"> doneThe value is: 1The value is: 2The value is: 3The value is: 4The value is: 5------------------------------------------------------------[root@master ~]#> for loop in hello world !> do>     echo $loop> donehelloworld!

如上所示,这里的 for 循环与 C 中的相似,但并不完全相同。通常情况下 shell 变量调用需要加 $,但是 for 的 (()) 中不需要,下面来看一个例子(使用的时候可以加)。

[root@master ~]#> for((i=1; i<=5; i++));do>     echo "这是第 $i 次调用";> done;这是第 1 次调用这是第 2 次调用这是第 3 次调用这是第 4 次调用这是第 5 次调用------------------------------------------------------------[root@master ~]#> for((i=1; $i<=5; i++));do>     echo "这是第 $i 次调用";> done;这是第 1 次调用这是第 2 次调用这是第 3 次调用这是第 4 次调用这是第 5 次调用

while 语句

[root@master ~]# int=1[root@master ~]#> while(( $int<=5 ))> do>     echo $int>     let "int++"> done12345
echo '按下 
退出'echo -n '输入你最喜欢的网站名: 'while read FILMdo echo "是的!$FILM 是一个好网站"done

until 循环

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。

condition 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。

[root@master ~]# a=0[root@master ~]# [root@master ~]#> until [ ! $a -lt 10 ]> do>    echo $a>    a=`expr $a + 1`> done0123456789------------------------------------------------------------[root@master ~]#> until [ $a -gt 20 ]> do>    echo $a>    a=`expr $a + 1`> done1011121314151617181920

case 命令

Shell case 语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

case 取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

# test.shecho '输入 1 到 4 之间的数字:'echo '你输入的数字为:'read aNumcase $aNum in    1)  echo '你选择了 1'    ;;    2)  echo '你选择了 2'    ;;    3)  echo '你选择了 3'    ;;    4)  echo '你选择了 4'    ;;    *)  echo '你没有输入 1 到 4 之间的数字'    ;;esac------------------------------------------------------------[root@master ~]# ./test.sh 输入 1 到 4 之间的数字:你输入的数字为:3你选择了 3[root@master ~]# ./test.sh 输入 1 到 4 之间的数字:你输入的数字为:5你没有输入 1 到 4 之间的数字
# test.shsite="runoob"case "$site" in   "runoob") echo "菜鸟教程"   ;;   "google") echo "Google 搜索"   ;;   "taobao") echo "淘宝网"   ;;esac------------------------------------------------------------[root@master ~]# ./test.sh 菜鸟教程

break 命令

break命令允许跳出所有循环(终止执行后面的所有循环)。

# test.shwhile :do    echo -n "输入 1 到 5 之间的数字:"    read aNum    case $aNum in        1|2|3|4|5) echo "你输入的数字为 $aNum!"        ;;        *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"            break        ;;    esacdone------------------------------------------------------------[root@master ~]# ./test.sh 输入 1 到 5 之间的数字:2你输入的数字为 2!输入 1 到 5 之间的数字:4你输入的数字为 4!输入 1 到 5 之间的数字:6你输入的数字不是 1 到 5 之间的! 游戏结束

continue 命令

continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

# test.shwhile :do    echo -n "输入 1 到 5 之间的数字: "    read aNum    case $aNum in        1|2|3|4|5) echo "你输入的数字为 $aNum!"        ;;        *) echo "你输入的数字不是 1 到 5 之间的!"            continue            echo "游戏结束"        ;;    esacdone------------------------------------------------------------[root@master ~]# ./test.sh 输入 1 到 5 之间的数字: 3你输入的数字为 3!输入 1 到 5 之间的数字: 7你输入的数字不是 1 到 5 之间的!输入 1 到 5 之间的数字: 3你输入的数字为 3!输入 1 到 5 之间的数字: ...

无限循环

while :do    commanddone
while truedo    commanddone
for (( ; ; ))

Shell 函数

函数 demo

  • 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
  • 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。
# test.shdemoFun(){    echo "这是我的第一个 shell 函数!"}echo "-----函数开始执行-----"demoFunecho "-----函数执行完毕-----"------------------------------------------------------------[root@master ~]# ./test.sh -----函数开始执行-----这是我的第一个 shell 函数!-----函数执行完毕-----

 函数返回值在调用该函数后通过 $? 来获得。注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

# test.shfunWithReturn(){    echo "这个函数会对输入的两个数字进行相加运算..."    echo "输入第一个数字: "    read aNum    echo "输入第二个数字: "    read anotherNum    echo "两个数字分别为 $aNum 和 $anotherNum !"    return $(($aNum+$anotherNum))}funWithReturnecho "输入的两个数字之和为 $? !"------------------------------------------------------------[root@master ~]# ./test.sh 这个函数会对输入的两个数字进行相加运算...输入第一个数字: 23输入第二个数字: 32两个数字分别为 23 和 32 !输入的两个数字之和为 55 !

函数参数

# test.shfunWithParam(){    echo "第一个参数为 $1 !"    echo "第二个参数为 $2 !"    echo "第十个参数为 $10 !"    echo "第十个参数为 ${10} !"    echo "第十一个参数为 ${11} !"    echo "参数总数有 $# 个!"    echo "作为一个字符串输出所有参数 $* !"}funWithParam 1 2 3 4 5 6 7 8 9 34 73------------------------------------------------------------[root@master ~]# ./test.sh 第一个参数为 1 !第二个参数为 2 !第十个参数为 10 !第十个参数为 34 !第十一个参数为 73 !参数总数有 11 个!作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

另外,还有几个特殊字符用来处理参数(之前的小结有讲过):

参数处理 说明
$# 传递到脚本或函数的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。

函数中的 $?

# test.shfunction demoFun1(){    echo "这是我的第一个 shell 函数!"    return `expr 1 + 1`}demoFun1echo $?function demoFun2(){ echo "这是我的第二个 shell 函数!" expr 1 + 1}demoFun2echo $?demoFun1echo 在这里插入命令!echo $?------------------------------------------------------------[root@master ~]# ./test.sh 这是我的第一个 shell 函数!2这是我的第二个 shell 函数!20这是我的第一个 shell 函数!在这里插入命令!0

$? 仅对其上一条指令负责,一旦函数返回后其返回值没有立即保存入参数,那么其返回值将不再能通过 $? 获得,因此,demoFun1 函数体里边有 return 语句,所以其执行后的 $? 没获得返回值 0。

调用 demoFun2 后,函数最后一条命令 expr 1 + 1 得到的返回值($?值)为 0,意思是这个命令没有出错。所有的命令的返回值仅表示其是否出错,而不会有其他有含义的结果。

第二次调用 demoFun1 后,没有立即查看 $? 的值,而是先插入了一条别的 echo 命令,最后再查看 $? 的值得到的是 0,也就是上一条 echo 命令的结果,而 demoFun1 的返回值被覆盖了。

函数结果作为条件参数

函数与命令的执行结果可以作为条件语句使用。要注意的是,和 C 语言不同,shell 语言中 0 代表 true,0 以外的值代表 false。

# test.shecho "Hello World !" | grep -e Helloecho $?echo "Hello World !" | grep -e Byeecho $?if echo "Hello World !" | grep -e Hellothen    echo trueelse    echo falsefiif echo "Hello World !" | grep -e Byethen    echo trueelse    echo falsefifunction demoFun1(){    return 0}function demoFun2(){    return 12}if demoFun1then    echo trueelse    echo falsefiif demoFun2then    echo tureelse    echo falsefi------------------------------------------------------------Hello World !01Hello World !truefalsetruefalse

grep 是从给定字符串中寻找匹配内容的命令。首先看出如果找到了匹配的内容,会打印匹配部分且得到的返回值 $? 为 0,如果找不到,则返回值 $? 为 1。接下来分别将这两次执行的 grep 命令当作条件语句交给 if 判断,得出返回值 $? 为 0,即执行成功时,条件语句为 true,当返回值 $? 为 1,即执行失败时,条件语句为 false。之后再用函数的 return 值作为测试,其中 demoFun1 返回值为 0,demoFun2 返回值选择了任意一个和 0 不同的整数,这里为 12。将函数作为条件语句交给 if 判断,得出返回值为 0 时,依然为 true,而返回值只要不是 0,条件语句都判断为 false。

Shell 输入输出重定向

命令 说明
command > file 将输出重定向到 file。
command < file 将输入重定向到 file。
command >> file 将输出以追加的方式重定向到 file。
n > file 将文件描述符为 n 的文件重定向到 file。
n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m 将输出文件 m 和 n 合并。
n <& m 将输入文件 m 和 n 合并。
<< tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

输出重定向

执行后,并没有在终端输出信息,这是因为输出已被从默认的标准输出设备(终端)重定向到指定的文件。

[root@master ~]# who > users[root@master ~]# cat usersroot     pts/0        2020-08-19 20:56 (192.168.128.1)

输出重定向会覆盖文件内容:

[root@master ~]# echo "Hello World" > users[root@master ~]# cat usersHello World

如果不希望文件内容被覆盖,可以使用 >> 追加到文件末尾:

[root@master ~]# echo "I am Looking" >> users[root@master ~]# cat usersHello WorldI am Looking

输入重定向

和输出重定向一样,Unix 命令也可以从文件获取输入,语法为:

command1 < file1

这样,本来需要从键盘获取输入的命令会转移到文件读取内容。

注意:输出重定向是大于号(>),输入重定向是小于号(<)。

接着以上实例,我们需要统计 users 文件的行数:

[root@master ~]# wc -l users2 users

也可以将输入重定向到 users 文件:

[root@master ~]# wc -l < users2

 注意:上面两个例子的结果不同:第一个例子,会输出文件名;第二个不会,因为它仅仅知道从标准输入读取内容。

重定向深入

一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:

  • 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

默认情况下,command > file 将 stdout 重定向到 file,command < file 将stdin 重定向到 file。

如果希望 stderr 重定向到 file,可以这样写:

$ command 2 > file

如果希望 stderr 追加到 file 文件末尾,可以这样写:

$ command 2 >> file

2 表示标准错误文件(stderr)。

如果希望将 stdout 和 stderr 合并后重定向到 file,可以这样写:

$ command > file 2>&1

或者

$ command >> file 2>&1

如果希望对 stdin 和 stdout 都重定向,可以这样写:

$ command < file1 >file2

command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2。

Here Document

Here Document 是 Shell 中的一种特殊的重定向方式,用来将输入重定向到一个交互式 Shell 脚本或程序。

它的基本的形式如下:

command << delimiter    documentdelimiter

它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

注意:

  • 结尾的 delimiter 一定要顶格写,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 缩进。
  • 开始的 delimiter 前后的空格会被忽略掉。
[root@master workspace]# wc -l << EOF>     欢迎来到>     菜鸟教程>     www.runoob.com> EOF3
[root@master workspace]# cat << EOF> 欢迎来到> 菜鸟教程> www.runoob.com> EOF欢迎来到菜鸟教程www.runoob.com

/dev/null 文件

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

$ command > /dev/null

/dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到"禁止输出"的效果。

如果希望屏蔽 stdout 和 stderr,可以这样写:

$ command > /dev/null 2>&1

注意:0 是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

这里的 2 和 > 之间不可以有空格,2> 是一体的时候才表示错误输出。

Shell 文件包含

和其他语言一样,Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。

. filename   # 注意点号(.)和文件名中间有一空格或source filename
# test1.shurl="http://www.runoob.com"
# test2.sh. ./test1.sh# 或者使用以下包含文件代码# source ./test1.shecho "菜鸟教程官网地址:$url"------------------------------------------------------------# test2.sh# . ./test1.sh# 或者使用以下包含文件代码source ./test1.shecho "菜鸟教程官网地址:$url"

我们为 test2.sh 添加可执行权限并执行:

[root@master workspace]# chmod +x test2.sh[root@master workspace]# ./test2.sh 菜鸟教程官网地址:http://www.runoob.com

注意:被包含的文件 test1.sh 可以不需要可执行权限。

to be continued

------------------------------------------------------------

 

转载地址:http://acjqi.baihongyu.com/

你可能感兴趣的文章
Administrator's Guide to SQL Server 2005
查看>>
Ajax Design Patterns
查看>>
DNS and BIND (5th Edition)
查看>>
Firewall Fundamentals
查看>>
Learning PHP and MySQL
查看>>
Agile Software Construction
查看>>
Computer Security Basics
查看>>
Sams Teach Yourself MySQL in 10 Minutes
查看>>
Information Systems : The State of the Field
查看>>
IPv6 Essentials
查看>>
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
查看>>
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
查看>>
Pro .NET 2.0 Windows Forms and Custom Controls in C#
查看>>
Beginning Regular Expressions
查看>>
Beginning Visual Web Developer 2005 Express: From Novice to Professional
查看>>
Beginning Programming
查看>>
Windows .NET Server 2003 Domains & Active Directory
查看>>
Information Systems : Achieving Success by Avoiding Failure
查看>>
Web Systems Design and Online Consumer Behavior
查看>>
VoIP For Dummies
查看>>