linux shell基础编程2,linuxshell基础编程
linux shell基础编程2,linuxshell基础编程
while循环
语法1:
while [ 条件 ]
do
命令序列
done
语法2:
while read -r line
do
命令序列
done
(切记while和左中括号一定要有空格)
例子
1 #!/bin/bash 2 j=1 3 SUM=0 4 while [ $j -lt 10 ] 5 do 6 SUM=$((SUM+j)) 7 j=$[j+1] 8 done 9 echo $SUM
if判断语句
语法1:
if 条件
then
命令序列
fi
语法2:
if 条件
then
条件序列
else
条件序列
fi
语法3:
if 条件
then
条件序列
elif 条件
then
条件序列
elif 条件
then
条件序列
else
条件序列
fi
例子
#!/bin/bash if [ -d /tmp/123 ];then echo "this is directory" else echo "this is not directory" fi
case语句
语法1:
case $变量名称 in
条件1)
命令序列
;;
条件2)
命令序列
;;
条件3)
命令序列
;;
*)
命令序列
esac
语法2:
case $变量名称 in
条件1|条件4)
命令序列
;;
条件2|条件5)
命令序列
;;
条件3|条件6)
命令序列
;;
*)
命令序列
esac
1 #!/bin/bash 2 case $1 in 3 top) 4 top 5 ;; 6 free) 7 free 8 ;; 9 df) 10 df 11 ;; 12 *) 13 echo "no param" 14 esacView Code
函数定义:
语法1:
方法名(){
命令序列
}
语法2:
function 方法名{
命令序列
}
1 #!/bin/bash 2 sum(){ 3 echo $(($1+$2)) 4 } 5 sum 5 6
评论暂时关闭