Article Directory
Shell parameter passing
Parameter handling
Parameter handling | Description |
---|---|
$# | Number of parameters passed to the script |
$* | Display all the parameters passed to the script in a single string. If "$*" is enclosed in """, all parameters are output in the form of "$1 $2… $n" |
$@ | The effect is the same as the above $*, and each parameter is returned in quotation marks |
$$ | ID number of the current process running the script |
$? | Display the exit status of the last command. 0 means no error, any other value means there is an error |
$0 | The full path name of the current shell script |
The following shell script is used as a test:
[root@hadoop-master test]# cat ./shell-test/helloworold.sh
#!/bin/bash
echo "执行的文件名: $0"
echo "参数个数:$#"
echo "执行的所有参数:$*"
echo "执行的所有参数,并在引号中返回每个参数:$@"
echo "退出状态: $?"
echo "脚本运行的当前进程ID: $$"
The execution results are as follows:
[root@hadoop-master test]# ./shell-test/helloworold.sh 1 2 3
执行的文件名: ./shell-test/helloworold.sh
参数个数:3
执行的所有参数:1 2 3
执行的所有参数,并在引号中返回每个参数:1 2 3
退出状态: 0
脚本运行的当前进程ID: 2560
The difference between $* and $@:
The same point : all parameters are quoted.
Difference : Only reflected in double quotes. Assuming that three parameters 1, 2, and 3 are written when the script is running, "*" is equivalent to "1 2 3" (a parameter is passed) , and "@" is equivalent to "1" "2" "3" "(Three parameters are passed) .
For example:
[root@hadoop-master shell-test]# cat test.sh
#!/bin/bash
# $* 和 $@ 的区别
echo "-- \$* 演示 --"
for i in "$*"; do
echo $i
done
echo "-- \$@ 演示 --"
for i in "$@"; do
echo $i
done
The print result is as follows:
[root@hadoop-master shell-test]# ./test.sh 1 2 3
-- $* 演示 --
1 2 3
-- $@ 演示 --
1
2
3
Shell array
Define and read array elements
[root@hadoop-master shell-test]# cat test-array.sh
#!/bin/bash
#方式1
my_array1=(A B "C" D)
#方式2
my_array2[0]=1
my_array2[1]=2
echo "第一个元素为: ${my_array1[0]}"
echo "第二个元素为: ${my_array1[1]}"
echo "第三个元素为: ${my_array1[2]}"
echo "第四个元素为: ${my_array1[3]}"
echo "第一个元素为: ${my_array2[0]}"
echo "第二个元素为: ${my_array2[1]}"
Output result:
[root@hadoop-master shell-test]# ./test-array.sh
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D
第一个元素为: 1
第二个元素为: 2
Shell basic operators
Arithmetic Operator
Note : The commands or operations used in the shell are all backquotes "`" (below the Esc key) instead of single quotation marks "'". A
backslash () must be added before the multiplication sign (*) to achieve multiplication.
Arithmetic Operator
Operator | Description |
---|---|
+ | addition |
- | Subtraction |
* | multiplication |
/ | division |
% | Take the remainder |
= | Assignment |
== | equal. Compare two numbers, the same returns true |
!= | not equal. Compare two numbers, return true if they are not the same |
Operation example:
[root@hadoop-master shell-test]# cat test-expr.sh
#!/bin/bash
a=${
1}
b=${
2}
echo "a=${a},b=${b}"
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 等于 b"
fi
if [ $a != $b ]
then
echo "a 不等于 b"
fi
Print result:
[root@hadoop-master shell-test]# ./test-expr.sh 7 3
a=7,b=3
a + b : 10
a - b : 4
a * b : 21
b / a : 0
b % a : 3
a 不等于 b
Relational operator
Note: Relational operators only support numbers, not strings, unless the value of the string is a number.
Operator | Description | For example |
---|---|---|
-eq | Check whether two numbers are equal, and return true if they are equal. | [$a -eq $b] returns false. |
-born | Check whether two numbers are not equal, and return true if they are not equal. | [$a -ne $b] returns true. |
-gt | Check if the number on the left is greater than the right, and if so, return true. | [$a -gt $b] returns false. |
-lt | Check if the number on the left is less than the right, and if so, return true. | [$a -lt $b] returns true. |
-give | Check if the number on the left is greater than or equal to the right, if so, return true. | [$a -ge $b] returns false. |
-the | Check if the number on the left is less than or equal to the right, and if so, return true. | [$a -le $b] returns true. |
Test code:
[root@hadoop-master shell-test]# cat ./test-rela-expr.sh
#!/bin/bash
a=$1
b=$2
echo "a=${1}, b=${b}"
if [ $a -eq $b ]
then
echo "$a -eq $b : a 等于 b"
else
echo "$a -eq $b: a 不等于 b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a 不等于 b"
else
echo "$a -ne $b : a 等于 b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a 大于 b"
else
echo "$a -gt $b: a 不大于 b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a 小于 b"
else
echo "$a -lt $b: a 不小于 b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a 大于或等于 b"
else
echo "$a -ge $b: a 小于 b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a 小于或等于 b"
else
echo "$a -le $b: a 大于 b"
fi
Output result:
[root@hadoop-master shell-test]# ./test-rela-expr.sh 11 10
a=11, b=10
11 -eq 10: a 不等于 b
11 -ne 10: a 不等于 b
11 -gt 10: a 大于 b
11 -lt 10: a 不小于 b
11 -ge 10: a 大于或等于 b
11 -le 10: a 大于 b
Boolean operator
Operator | Description | For example |
---|---|---|
! | If the expression is true, it returns false, otherwise it returns true. | [! false] returns true. |
-The | OR operation, return true if an expression is true | [10 -lt 20 -o 20 -gt 100] returns true. |
-a | And operation, both expressions are true before returning true. | [10 -lt 20 -a 20 -gt 100] returns false. |
Test code:
[root@hadoop-master shell-test]# cat ./test-boolean.sh
#!/bin/bash
a=${
1}
b=${
2}
echo "a=${a}, b=${b}"
if [ $a != $b ]
then
echo "$a != $b : a 不等于 b"
else
echo "$a == $b: a 等于 b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a 小于 100 且 $b 大于 15 : 返回 true"
else
echo "$a 小于 100 且 $b 大于 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a 小于 100 或 $b 大于 100 : 返回 true"
else
echo "$a 小于 100 或 $b 大于 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a 小于 5 或 $b 大于 100 : 返回 true"
else
echo "$a 小于 5 或 $b 大于 100 : 返回 false"
fi
Print result
[root@hadoop-master shell-test]# ./test-boolean.sh 10 20
a=10, b=20
10 != 20 : a 不等于 b
10 小于 100 且 20 大于 15 : 返回 true
10 小于 100 或 20 大于 100 : 返回 true
10 小于 5 或 20 大于 100 : 返回 false
Logical Operators
Operator | Description | For example |
---|---|---|
&& | Logical ADN | [[ 10 -lt 100 && 20 -gt 100 ]] return false |
|| | Logical OR | [[ 10 -lt 100 || 20 -gt 100 ]] returns true |
Test code
[root@hadoop-master shell-test]# cat test-and-or.sh
#!/bin/bash
a=$1
b=$2
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "返回 true"
else
echo "返回 false"
fi
if [[ $a -lt 100 || $b -gt 100 ]]
then
echo "返回 true"
else
echo "返回 false"
fi
Print result:
[root@hadoop-master shell-test]# ./test-and-or.sh 10 20
返回 false
返回 true
String operators (emphasis)
Assume that the variable a is "abc" and the variable b is "efg". Note that the operator and the variable must be separated by spaces
Operator | Description | For example |
---|---|---|
= | Determine whether two strings are equal, return true if they are equal | [$a = $b] returns false |
!= | Judge whether two strings are equal, return true if they are not equal | [$a != $b] returns true |
-with | Judge whether the length of the string is 0, return true for 0 | [-z $a] returns false |
-n | Judge whether the string length is not 0, return true if it is not 0 | [-n $a] returns true |
$ | Judge whether the string is empty, return true if it is not empty | [$a] returns true |
Test example:
[root@hadoop-master shell-test]# cat ./test-str.sh
#!/bin/bash
a=$1
b=$2
c=$3
echo "a=$a, b=$b, c=$c"
if [ $a = $b ]
then
echo "a = b : is true"
else
echo "a = b : is fasle"
fi
if [ $a != $b ]
then
echo "a != b : is true"
else
echo "a != b : is false"
fi
if [ -z $a ]
then
echo "-z a : is true"
else
echo "-z a : is false"
fi
if [ -n $a ]
then
echo "-n a : is true"
else
echo "-n a : is fasle"
fi
if [ $c ]
then
echo "c : is not empty"
else
echo "c : is empty"
fi
Print result:
[root@hadoop-master shell-test]# ./test-str.sh abc edf
a=abc, b=edf, c=
a = b : is fasle
a != b : is true
-z a : is false
-n a : is true
c : is empty
文件测试运算符
操作符 | 说明 | 举例 |
---|---|---|
-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。 |
其他检查符:
-S: 判断某文件是否 socket。
-L: 检测文件是否存在并且是一个符号链接。
测试代码:
[root@hadoop-master shell-test]# cat ./test-file.sh
#!/bin/bash
file=$1
if [ -r $file ]
then
echo "文件可读"
else
echo "文件不可读"
fi
if [ -w $file ]
then
echo "文件可写"
else
echo "文件不可写"
fi
if [ -x $file ]
then
echo "文件可执行"
else
echo "文件不可执行"
fi
if [ -f $file ]
then
echo "文件为普通文件"
else
echo "文件为特殊文件"
fi
if [ -d $file ]
then
echo "文件是个目录"
else
echo "文件不是个目录"
fi
if [ -s $file ]
then
echo "文件不为空"
else
echo "文件为空"
fi
if [ -e $file ]
then
echo "文件存在"
else
echo "文件不存在"
fi
打印结果:
[root@hadoop-master shell-test]# ./test-file.sh test-str.sh
文件可读
文件可写
文件可执行
文件为普通文件
文件不是个目录
文件不为空
文件存在
Shell 常用语法
测试代码
[root@hadoop-master shell-test]# cat test-loop.sh
#!/bin/bash
#循环条件测试
a=$1
b=$2
c=$3
echo "a=$a, b=$b, c=$c"
echo "----if esle 测试----"
if [ $a == $b ]
then
echo "a 等于 b"
else
echo "a 不等于 b"
fi
echo "----if elif else测试----"
if [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -gt $c ]
then
echo "a 大于 c"
else
echo "a 不大于b和c"
fi
echo "----for循环测试---- "
for i in $@
do
echo "入参:$i"
done
echo "----while语句----"
j=$a
while(($j <= $c))
do
echo $j
j=`expr $j + 1`
done
echo "----until循环----"
k=$a
until [ ! $k -lt $c ]
do
echo $k
k=`expr $k + 1`
done
echo "----case...esac语句----"
case $a in
1) echo "选择了1"
;;
10) echo "选择了10"
;;
*) echo "a没有选择 1 或者 10"
;;
esac
测试结果:
[root@hadoop-master shell-test]# ./test-loop.sh 6 6 10
a=6, b=6, c=10
----if esle 测试----
a 等于 b
----if elif else测试----
a 不大于b和c
----for循环测试----
入参:6
入参:6
入参:10
----while语句----
6
7
8
9
10
----until循环----
6
7
8
9
----case...esac语句----
a没有选择 1 或者 10
Shell 函数
函数测试
[root@hadoop-master shell-test]# cat test-method.sh
#!/bin/bash
function1(){
echo "hello world my first shell function"
}
funWithReturn(){
echo "带有返回值的function"
val=`expr $1 + $2`
return $val
}
echo "执行function1"
function1
echo "函数执行完毕"
echo "执行funWithReturn"
funWithReturn 12 13
echo "函数funWithReturn执行完毕$?"
测试结果
[root@hadoop-master shell-test]# ./test-method.sh 1 4
执行function1
hello world my first shell function
函数执行完毕
执行funWithReturn
带有返回值的function
函数funWithReturn执行完毕25
注意:shell的内置return承接返回值用的是一个字节的大小,也就是8位,最多可以输出无符号0-255的整形,范围之外的数据全部溢出显示。因此在使用return的时候,务必留意数值大小。
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)。
一般情况下,每个 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。