shell相关的用法
if用法
-q不输出结果,只做判断
[root@rs1 tmp]# if grep -q '^daixuan:' /etc/passwd; then echo "daixuan text" ;fi
,当/etcpasswd下包含daixuan,就输出daixuan test
case用法实例
[root@rs1 tmp]# vim case.sh
#/bin/bash read -p "Please input a number: " n m=$[$n%2] echo $m case $m in 1) echo "The number is jishu." ;; 0) echo " The number is oushu." ;; *) echo "It is not jishu or oushu." ;; esac
[root@rs1 tmp]# sh case.sh
Please input a number: 12
0
The number is oushu.
[root@rs1 tmp]# sh case.sh
Please input a number: 11
1
The number is jishu.
[root@rs1 tmp]# sh case.sh
Please input a number: abcdef
0
The number is oushu.
for用法实例
sed的使用
[root@rs1 tmp]# seq 1 5
1
2
3
4
5
[root@rs1 tmp]# seq 1 2 10
1
3
5
7
9
[root@rs1 tmp]# seq 10 -2 1
10
8
6
4
2
[root@rs1 tmp]# seq -w 1 10
01
....
10
[root@rs1 tmp]# vim for.sh
#/bin/bash for i in `seq 1 10`; do echo $i done
[root@rs1 tmp]# sh for.sh
#/bin/bash sum=0 for i in {1..10} do sum=$[$sum+$i] done echo $sum
[root@rs1 tmp]# sh for.sh
55
while实例
[root@rs1 tmp]# vim while.sh
#/bin/bash n=0 while [ $n -le 10 ] do echo $n n=$[$n+1] done
shell 中断继续退出
continue 结束本次循环,没有打印 3
[root@rs1 tmp]# vim break.sh
#/bin/bash for i in `seq 1 5` do #echo $i if [ $i -eq 3 ] then continue fi echo $i done echo "for done"
[root@rs1 tmp]# sh break.sh
1
2
4
5
for done
break 结束整个循环,没有打印3、4、5;但是执行了echo "for done"
[root@rs1 tmp]# vim break.sh echo "for done"
#/bin/bash for i in `seq 1 10` do #echo $i if [ $i -eq 3 ] then break fi echo $i done echo "for done"
[root@rs1 tmp]# sh break.sh
1
2
for done
exit 直接结束整个shell
[root@rs1 tmp]# vim break.sh
#/bin/bash for i in `seq 1 5` do #echo $i if [ $i -eq 3 ] then exit fi echo $i done echo "for done"
[root@rs1 tmp]# sh break.sh
1
2
Shell函数
定义shell函数,默认函数变量全局有效,变量定义局部变量使用local sum,在函数外部sum不生效
[root@rs1 tmp]# vim fun.sh
#/bin/bash function mysum() { local sum=$[$1+$2] echo $sum } a=1 b=2 mysum $a $b echo $sum
[root@rs1 tmp]# sh fun.sh
3
//3在加入local不打印,局部变量不能全局使用
shell的数组
[root@rs1 tmp]# a=1
[root@rs1 tmp]# echo $a
1
[root@rs1 tmp]# a=(1 2 3 4)
[root@rs1 tmp]# echo $a
1
[root@rs1 tmp]# echo ${a[@]}
1 2 3 4
[root@rs1 tmp]# echo ${a[*]}
1 2 3 4
[root@rs1 tmp]# echo ${a[0]}
1
[root@rs1 tmp]# echo ${a[1]}
2
[root@rs1 tmp]# echo ${a[2]}
3
[root@rs1 tmp]# echo ${a[3]}
4
[root@rs1 tmp]# echo ${a[4]} //${a[4]}为空
//没有结果
[root@rs1 tmp]# a[4]=9 //给数组增加一个值
[root@rs1 tmp]# echo ${a[4]}
9
[root@rs1 tmp]# echo ${a[*]}
1 2 3 4 9
[root@rs1 tmp]# echo ${#a[*]} //查看数组的长度,即元素的个数
5
[root@rs1 tmp]# for i in `seq 0 9`; do a[$i]=$RANDOM; done; echo ${a[@]}
13362 23672 22230 8715 10367 21082 18530 14746 28089 10189
[root@rs1 tmp]# echo ${a[@]}
13362 23672 22230 8715 10367 21082 18530 14746 28089 10189
[root@rs1 tmp]# unset a[9] //删除a[9],10189被删除
[root@rs1 tmp]# echo ${a[@]}
13362 23672 22230 8715 10367 21082 18530 14746 28089
[root@rs1 tmp]# echo ${a[@]:0:4} //打印前四个数
13362 23672 22230 8715
[root@rs1 tmp]# for i in `seq 0 9`; do a[$i]=$RANDOM; done ; echo ${a[@]} |sed 's/ /\n/g'|sort -n //随机获得10个数,然后替换空格为换行,之后-n 从小到大排序
3553
4101
7879
13870
16011
17170
17640
21997
29239
30581