数组
[root@localhost tmp]# declare -a array1=(1 2 3 4 5) //定义数组的方法1
[root@localhost tmp]# array2=(one two three four five) //定义数组的方法2
[root@localhost tmp]# echo ${array2[0]} //0表示数组的第一个元素
one
[root@localhost tmp]# echo ${array2[1]}
two
[root@localhost tmp]# declare -p array1 //显示数组的所有参数
declare -a array1='([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")'
[root@localhost tmp]# echo ${array1[*]} //显示数组所有元素的值
1 2 3 4 5
[root@localhost tmp]# echo ${array1[@]} //显示数组所有元素的值
1 2 3 4 5
[root@localhost tmp]#
函数
[root@localhost tmp]# vim func1
1 #!/bin/bash
2 func1()
3 {
4 echo "This is the first function"
5 }
6 func1
7 unset func1 //取消函数
8 func1()
9 {
10 echo "This is a new function"
11 }
12 func1
[root@localhost tmp]# ./func1
This is the first function
This is a new function
[root@localhost tmp]#
------------
test
check file types and compare values
test 条件
条件为真返回 0,条件为假返回 1
条件:
字符串
-n STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
数字
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
文件:
-f 存在且是正规文件
-d 存在且是目录
-h 存在且是符号链接
-b 块设备
-c 字符设备
-e 文件存在
-r
-w
-x
file1 -nt file2 file1 比 file2 新(修改时间)
file1 -ot file2 file1 比 file2 旧(修改时间)
----------
条件判断
if判断语法:
1 #!/bin/bash
2 if 表达式
3 then
4 动作
5 elif 表达式
6 then
7 动作
8 else
9 动作
10 fi
helpif
if表达式 test命令 练习:
判断一个文件是否存在,如果存在复制到桌面,如果不存在新建
1 #!/bin/bash
2 if test -e /tmp/func.sh
3 then
4 cp /tmp/func.sh /root/Desktop
5 else
6 touch /tmp/func.sh
7 fi
1 #!/bin/bash
2 if [ -e /tmp/func.sh ] //test命令另一种写法
3 then
4 cp /tmp/func.sh /root/Desktop
5 echo 文件存在,已经拷贝到桌面
6 else
7 touch /tmp/func.sh
8 fi
摇奖:
1 #!/bin/bash
2 echo 这是一个猜数字游戏
3 guess=$[ $RANDOM % 100 + 1]
4 func()
5 {
6 read -p "请输入一个1-100之间的数字:" num
7 if [ $num -eq $guess ]
8 then
9 echo guess sucessfully
10 else
11 echo 'guess failed! Please guess again'
12 func
13 fi
14 }
15 func
多个条件联合
逻辑与
if [ $condition1 ] && [ $condition2 ]
if [ $condition -a $condition2 ]
if [[ $condition1 && $condition2 ]]
逻辑或
if [ $condition1 ] || [ $condition2 ]
if [ $condition -o $condition2 ]
if [[ $condition1 || $condition2 ]]
-------------
case
case 语句是 shell 中流控制的第二种方式, 语法如下:
case word in
pattern1)
list1
;;
pattern2)
list2
;;
... ...
patternN)
listN
;;
*)
list*
;;
esac
例子
1 #!/bin/bash
2 read -p "Please input a number: " i
3 case $i in
4 5)
5 echo i=$i;;
6 6)
7 echo i=$i;;
8 *)
9 echo Input wrong;;
10 esac
-------------
循环:
for...do...done
select...do...done
while...do...done
until...do...done
for i in 1 2 3
> do echo $i
> done
1 #!/bin/bash
2 for ((i=1;i<=10;i++)) //等同
3 for i in seq 10 //等同
seq
print a sequence of numbers
seq 10
seq 5 10
seq 1 2 10 // 以1开头,以2为间隔,以10结尾
seq 10 -2 -10
seq -f a%03g 10 // -f format 指定格式
seq -w 10 // -w --equal-width
-----------
while 循环:
语法
while cmd
do
list
done
例子
#!/bin/bash
#14.sh
x=0
while [ $x -lt 10 ]
do
echo $x
x=`expr $x + 1`
done
#15.sh
#!/bin/bash
sum=0
while [ $sum -lt 10 ]
do
sum=`expr $sum + 1`
useradd user$sum
echo "123456" | passwd --stdin user$sum
done
------------
until 循环:
语法
until cmd
do
list
done
例子
[root@server1 ~]# cat 16.sh
#!/bin/bash
#16.sh
x=1
until [ $x -ge 10 ]
do
echo $x
x=`expr $x + 1`
done
x=1
while [ ! $x -ge 10 ]
do
echo $x
x=`expr $x + 1`
done
-----------
select 循环:
语法
select name in word1 word2 word3 ... wordN
do
list
done
例子
1 #!/bin/bash
2 echo '请选择你喜欢的水果!'
3 select i in apple orange banana
4 do
5 echo $i
6 done
[root@localhost tmp]# ./select.sh
请选择你喜欢的水果!
1) apple
2) orange
3) banana
#? 2
orange
#?
[root@localhost tmp]#
循环控制:
break
continue