shell学习整理笔记

1.shell脚本是一个以.sh问扩展名的文件。
“#!”是一个约定的标记,是告诉系统需要什么解释器来执行,echo命令用于向窗口输出文本。
如:
#!/bin/bash
echo "Hello world!"
将代码保存为test.sh ,并cd到保存目录:
chmod +x ./test.sh //是脚本拥有执行权限
./test.sh //执行脚本
注意:一定要写成./test.sh,而不是test.sh。

1.1 定义变量
定义变量时,变量名不加$符号,如: varriableName=“value”
注意:变量名和等号之间不能有空格

1.2 使用变量
使用一个定义过的变量,只要在变量名前面加$符号即可。如:
your_name="jason"
echo $your_name
echo ${your_name}
注意:变量名外面的花括号是可选的,加花括号的目的是让解释器识别变量的边界

1.3 只读变量
使用readonly命令可以将变量定义为只读变量,只读变量的值不能被改变。 如:
#!/bin/bash
myUrl="http://www.baidu.com"
readonly myUrl
myUrl="http://www.sohu.com"

1.4 删除变量
使用unset命令可以删除变量,如: unset variable_name

2.变量类型
一、局部变量: 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他的shell启动的程序不能访问局部变量
二、环境变量: 包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要时候shell脚本也可以定义环境变量
三、shell变量: shell变量时有shell程勋设置的特殊变量,shell变量中又一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行

2.1 特殊变量
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n是一个数字,表示第几个参数,例如,第一个参数是$1,第二个$2
$# 传递给脚本或函数的参数的个数
$* 传递给脚本或者函数的所有参数
$@ 传递给脚本或函数的所有参数,被双引号“”包含时,与$*稍有不同
$? 上个命令的退出状态,或函数的返回值
$$ 当前shell进程ID

2.2 命令行参数
运行脚本时传递给脚本的参数称为命令行参数,命令行参数用$n表示,如:$1表示第一个参 数,$2表示第二个参数
如:
#!/bin/bash
echo "file name:$0"
echo "first paramater:$1"
echo "second parameter:$2"

$*和$@的区别
$*和$@都表示传递给函数或脚本的所有参数,不被双引号“”包含时,都以$1,$2的形式输出所有参数但是当它们被双引号“”包含时,“$*”会将所有的参数作为一个整体,以“$1 $2”的形式输出所有参数;“$@”会将各个参数分开,以$1,$2的形式输出所有参数

例子:
a=10
echo -e "Value of a is $a \n"
运行结果:Value of a is 10
这里的-e表示对转义字符进行替换。
转义字符:
\\ 反斜杠
\a 警报,响铃
\b 退格(删除键)
\f 换页(FF),将当前位置移到下页开头
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符

2.3 变量替换
变量替换可以根据变量的状态来改变它的值
${var} 变量本来的值
${var:-word} 如果变量var为空或已被删除(unset),那么返回word,但不改边var的值
${var:=word} 如果变量var为空或者已被删除(unset),那么返回word,并将var的值设置为word
${var:?message} 如果变量var为空或者已被删除(unset),那么将消息message送到标准错误输出,可以用来检测变量var是否可以被正常复制。若此替换出现在shell脚本中,那么脚本将停止运行
${var:+word} 如果变量var被定义,那么返回word,但不该表var 的值
例如:
echo ${var:-"variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="variable is not set"}
echo "2 - Value of var is ${var}"

unset var
echo ${var:+"this is default value"}
echo "3 - Value of var is $var"

var="prefix"
echo "4 - Value of var is $var"

echo ${var:?"print this message"}
echo "5 - Value of var ${var}"

bash不支持简单的数学运算,但是可以通过其他命令来实现
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加:
val=`expr 2 + 2`
echo "Total value :$val"
注意:表达式和运算符之间要有空格,例如2+2是不对的,必须写成 2 + 2
完整的表达式要被 ` `包含,,注意这个字符不是常用的单引号,是ESC键下边。

2.4 算术运算符
例:
#!/bin/sh
b=20
val=`expr $a + $b`
echo "a+b:$val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
echo "a is not equal to b"
fi
注意:乘号(*)前边必须加反斜杠(\)才能实现乘法运算;
if……then……fi是条件表达式

算术运算符列表
+ 加法 `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 ].

2.5 关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字
例:
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
echo "$a -ne $b: a is not equal to b"
echo "$a -ne $b : a is equal to b"
if [ $a -gt $b ]
echo "$a -gt $b: a is greater than b"
echo "$a -gt $b: a is not greater than b"
if [ $a -lt $b ]
echo "$a -lt $b: a is less than b"
echo "$a -lt $b: a is not less than b"
if [ $a -ge $b ]
echo "$a -ge $b: a is greater or equal to b"
echo "$a -ge $b: a is not greater or equal to b"
if [ $a -le $b ]
echo "$a -le $b: a is less or equal to b"
echo "$a -le $b: a is not less or equal to b"
fi
关系运算符列表
-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 true。
-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。

2.6 布尔运算符
例:
if [ $a != $b ]
echo "$a != $b : a is not equal to b"
echo "$a != $b: a is equal to b"
if [ $a -lt 100 -a $b -gt 15 ]
echo "$a -lt 100 -a $b -gt 15 : returns true"
echo "$a -lt 100 -a $b -gt 15 : returns false"
if [ $a -lt 100 -o $b -gt 100 ]
echo "$a -lt 100 -o $b -gt 100 : returns true"
echo "$a -lt 100 -o $b -gt 100 : returns false"
if [ $a -lt 5 -o $b -gt 100 ]
fi

布尔运算符列表
! 非运算,表达式为 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。

2.7 字符串运算符
例:
a="abc"
b="efg"
if [ $a = $b ]
echo "$a = $b : a is equal to b"
echo "$a = $b: a is not equal to b"
if [ -z $a ]
echo "-z $a : string length is zero"
echo "-z $a : string length is not zero"
if [ -n $a ]
echo "-n $a : string length is not zero"
echo "-n $a : string length is zero"
if [ $a ]
echo "$a : string is not empty"
echo "$a : string is empty"
fi

字符串运算符列表
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否为0,不为0返回 true。 [ -z $a ] 返回 true。
str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。

2.8 文件测试运算符
例:
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
echo "File has read access"
echo "File does not have read access"
if [ -w $file ]
echo "File has write permission"
echo "File does not have write permission"
if [ -x $file ]
echo "File has execute permission"
echo "File does not have execute permission"
if [ -f $file ]
echo "File is an ordinary file"
echo "This is sepcial file"
if [ -d $file ]
echo "File is a directory"
echo "This is not a directory"
if [ -s $file ]
echo "File size is zero"
echo "File size is not zero"
if [ -e $file ]
echo "File exists"
echo "File does not exist"
fi
文件测试运算列表
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -b $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。

shell注释都是以#开头的

3.shell字符串
3.1 单引号
str=‘this is a string’
单引号字符的限制:
·单引号里的任何字符都会原样输出,单引号字符串中的变量时无效的
·单引号字符串中不能出现单引号(对单引号使用转义符后也不行)

3.2 双引号
your_name='jason'
str="hello,I know your are \"your_name\"!\n"
双引号的优点:
·双引号里可以有变量
·双引号可以出现转义字符

3.3 拼接字符串
your_name="jason"
greeting="hello,"$your_name"!"
greeting_1="hello,${your_name}!"

echo $greeting $greeting_1

3.4 获取字符串长度
string=“abcd”
echo ${#string} #输出 4

3.5 提取子字符串
string="alibaba is a great company"
echo ${#string:1:4} #输出liba
注:如果是string[1:4]是包含头,不包含结尾的,例如:string[1:4]值为:lib

3.6 查找子字符串
string=“alibab is a great company”
echo `expr index "$string' is`

4.定义数组
在shell中,用括号来表示数组,数组元素用“空格”符号分割开,定义数组的一般形式为:
array_name=(value1 …… valuen)
例: array_name=(value0 value1 value2 value3)
或者 array_name=(
value0
value1
value2
value3
)
还可以单独定义数组的各个分量:
array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

4.1 读取数组
读取数组元素值一般格式是: ${array_name[index]}
如: valuen=${array_name[2]}

4.2 获取数组的长度
length=${#array_name[@]}或length=${#array_name[*]}或length=${#array_name[n]}

5.echo 是shell的一个内部指令,用于在屏幕上打印出指定的字符串, 命令格式:echo arg
显示转义字符: echo “\"It is a test\"” 双引号可以省略

5.1 显示变量
name=“OK”
echo “$name It is a test”

5.2 显示不换行
echo “OK!\c”
echo "It is a test"

5.3 显示结果重定向至文件
echo "It is a test" >myfile

5.4 原样输出字符串
echo '$name\”' 如需要原样输出字符串(不进行转义),请使用单引号

6.shell printf命令:格式化输出语句 例: $printf "hello,shell\n"
printf format-string [arguments...]
format-string 为格式控制字符串,arguments为参数列表。
如:
# format-string为双引号
$ printf "%d %s\n" 1 "abc"
1 abc
# 单引号与双引号效果一样
$ printf '%d %s\n' 1 "abc"
# 没有引号也可以输出
$ printf %s abcdef
abcdef
# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
$ printf %s abc def
$ printf "%s\n" abc def
abc
def
$ printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i
j
# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
$ printf "%s and %d \n"
and 0
# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
$ printf "The first program always prints'%s,%d\n'" Hello Shell
-bash: printf: Shell: invalid number
The first program always prints 'Hello,0'
$
注意:根据POSIX标准,浮点格式%e、%E、%f、%g与%G是“不需要被支持”。这是因为awk支持浮点预算,且有它自己的printf语句。这样Shell程序中需要将浮点数值进行格式化的打印时,可使用小型的awk程序实现。然而,内建于bash、ksh93和zsh中的printf命令都支持浮点格式。

7.shell if else语句
7.1 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。
Shell 有三种 if ... else 语句:
if ... fi 语句;
if ... else ... fi 语句;
if ... elif ... else ... fi 语句。

1.if……else语句
语法:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。
最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。
注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。
例:
if [ $a == $b ]
echo "a is equal to b"
echo "a is not equal to b"
fi
2.if……else……fi语句
语法:
else
Statement(s) to be executed if expression is not true
fi
如果 expression 返回 true,那么 then 后边的语句将会被执行;否则,执行 else 后边的语句。
例:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

3.if……elif……fi语句
if……elif……fi语句可以对多个条件进行判断。
语法为:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。
例:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
echo "a is equal to b"
elif [ $a -gt $b ]
echo "a is greater than b"
elif [ $a -lt $b ]
echo "a is less than b"
echo "None of the condition met"
fi
if……else语句也可以写成一行,以命令的方式来运行,像这样:
if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

if...else 语句经常与test命令结合使用,如:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
注:test命令用于检查某个条件是否成立,与方括号([ ])类似。

7.2 shell中的test命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
数值测试
参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真
例:
num1=100
num2=100
if test $[num1] -eq $[num2]
echo 'The two numbers are equal!'
echo 'The two numbers are not equal!'
fi

字符串测试
参数 说明
= 等于则为真
!= 不相等则为真
-z 字符串 字符串长度伪则为真
-n 字符串 字符串长度不伪则为真
例:
num1=100
num2=100
if test num1=num2
echo 'The two strings are equal!'
echo 'The two strings are not equal!'
fi

文件测试
参数 说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真
例:
cd /bin
if test -e ./bash
echo 'The file already exists!'
echo 'The file does not exists!'
fi

另外,shell还提供了与(!)、或(-o)、非(-a)三个逻辑操作符用于将测试条件连接起来,其优先级:“!”最高,”-a“次之,”-o“最低
例如:
cd /bin
if test -e ./notFile -o ./bash
echo 'One file exists at least!'
echo 'Both dose not exists!'
fi

9.shell case esac 语句
case……esac 与其他语言中的switch……case语句类似,是一种多分支选择结构。
case语句匹配一个值或者一种模式,如果匹配成功,执行相匹配的命令,case语句格式如下:
case 值 in
模式1)
command1
command2
command3
;;
模式2)
*)
esac
case工作方式如上所示,取值后面必须为关键字in,每一模式必须以右括号)结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;。;;与其他语言中的 break类似,意思是跳到整个case语句的最后,取值将检测匹配的每一模式,一旦模式匹配成功,则执行完匹配模式相应命令后不再执行其他的模式,如果无一匹配模式,使用星号*捕获该值,再执行候命的命令。
例如:
echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
3) echo 'You select 3'
4) echo 'You select 4'
*) echo 'You do not select a number between 1 to 4'
esac
又例如:
#!/bin/bash
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
-d) DIR="${2}"
echo "Dir name is $DIR"
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
esac

10.shell for循环
for循环一般格式为:
for 变量 in 列表
do
command1
command2
...
commandN
done

列表是一组值(数字、字符串)组成的序列,每个值通过空格分隔,每循环一次,就将列表中的下一个值赋给变量
in列表是可选的,如果不用它,for循环使用命令行的位置参数。
例如:(顺序输出当前列表中的数字)
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
又例如:(顺序输出字符串的字符)
for str in 'This is a string'
echo $str
done
例如:(显示主目录下以.bash.开头的文件)
for FILE in $HOME/.bash*
echo $FILE
done

11.shell while循环
while循环用于不断执行一系列命令,也用于从输入文件中读取数据,命令通常为测试条件,其格式为:
while command
do
Statement(s) to be executed if command is true
done
命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

以下是一个基本的while循环,测试条件是:如果COUNTER小于 5 ,那么返回true。COUNTER从0开始,每次循环处理时,COUNTER加1,运行上述脚本,返回数字1到5,然后终止。
COUNTER=0
while [ $COUNTER -lt 5 ]
do
COUNTER='expr $COUNTER + 1'
echo $COUNTER
done

12.shell until循环
until循环执行一系列命令直至条件为true时停止,until循环与while循环在处理方式上刚好相反,一般while循环由于until循环,但在某些时候,也只是极少数情况下,until循环更加有用。
until循环格式为:
until command
Statement(s) to be executed until command is true
done

command一般为条件表达式,如果返回值为false,则继续执行循环体内的语句,否则跳出循环。
例如:
(使用until命令输出0~9的数字)
a=0
until [ ! $a -lt 10 ]
echo $a
a=`expr $a + 1`
done

在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,shell也使用break和continue来跳出循环。
12.1 break命令
break命令允许跳出所有循环(终止后面的所有循环)
例 :
(脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示,返回shell提示符下,就要使用break命令)
while :
echo -n "Input a number between 1 to 5: "
1|2|3|4|5) echo "Your number is $aNum!"
*) echo "You do not select a number between 1 to 5,game is over!"
break
esac
done

在嵌套循环中,break命令后面还可以跟一个整数,表示跳出第几层循环。例如:break n

下面是一个嵌套循环的例子,如果 var1 等于 2,并且 var2 等于 0,就跳出循环:
#!/bin/bash
for var1 in 1 2 3
for var2 in 0 5
if [ $var1 -eq 2 -a $var2 -eq 0 ]
break 2
echo "$var1 $var2"
fi
done
done

12.2 continue 命令
continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。
例如:
*) echo "You do not select a number between 1 to 5!"
continue
echo "Game is over!"
done
再例如:
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
echo "Number is an even number!!"
fi
echo "Found odd number"
done

13.函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Shell 也支持函数。Shell 函数必须先定义后使用。
13.1 Shell 函数的定义格式如下:
function_name () {
list of commands
[ return value ]
}
如果你愿意,也可以在函数名前加上关键字 function:
function function_name () {
}
函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。

Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。

如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。

例如:
# Define your function here
Hello () {
echo "Url is http://see.xidian.edu.cn/cpp/shell/"
}
# Invoke your function
Hello
再例如:
再来看一个带有return语句的函数:
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
funWithReturn
# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"

13.2 shell 函数参数
在shell中,调用函数时可以向其传递参数,再函数体内部,通过$n的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二参数……
例:
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
funWithParam 1 2 3 4 5 6 7 8 9 34 73
运行脚本:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
注意,$10不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
另外,还有几个特殊的变量来处理函数
特殊变量 说明
$# 传递给函数的参数个数。
$* 显示所有传递给函数的参数。
$@ 与$*相同,但是略有区别,请查看Shell特殊变量。
$? 函数的返回值。

14.unix命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示,一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器。
14.1 输出重定向
命令的输出不仅可以是显示器,还可以很容易的转移到文件,这被称为输出重定向

语法为: $ command > file 这样,输出到显示器的内容就可以被重定向到文件。
例如: $ who > users

14.2 输入重定向
语法为: command < file
注意:输出重定向是大于号> ,输入重定向是小于号<。

14.3 重定向深入讲解:
1.一般情况下,每个 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。

全部可用的重定向命令列表
命令 说明
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 之间的内容作为输入。

15.Here Document

Here Document 目前没有统一的翻译,这里暂译为”嵌入文档“。Here Document 是 Shell 中的一种特殊的重定向方式,它的基本的形式如下:
command << delimiter
document
delimiter
它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

注意:
结尾的delimiter 一定要顶格写,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 缩进。
开始的delimiter前后的空格会被忽略掉

下面的例子,通过 wc -l 命令计算 document 的行数:
$wc -l << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
3
$
也可以 将 Here Document 用在脚本中,例如:
#!/bin/bash
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
运行结果:
in Cape Town.

下面的脚本通过 vi 编辑器将 document 保存到 test.txt 文件:
filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands
$ sh test.sh
Vim: Warning: Input is not from a terminal
$
打开 test.txt,可以看到下面的内容:
$ cat test.txt
/dev/null 文件

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:
$ command > /dev/null
/dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到”禁止输出“的效果。

如果希望屏蔽 stdout 和 stderr,可以这样写:
$ command > /dev/null 2>&1

16 shell 文件包含
像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本。

Shell 中包含脚本可以使用:
. filename
source filename
两种方式的效果相同,简单起见,一般使用点号(.),但是 注意点号(.)和文件名中间有一空格

例如,创建两个脚本,一个是被调用脚本 subscript.sh,内容如下:
url="http://see.xidian.edu.cn/cpp/view/2738.html"
一个是主文件 main.sh,内容如下:
. ./subscript.sh
echo $url
执行脚本:
$chomd +x main.sh
./main.sh
http://see.xidian.edu.cn/cpp/view/2738.html
$
注意:被包含脚本不需要有执行权限。

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...