shell学习笔记(四)

一、shell脚本中的循环

1、while

例如:while [ x -lt 10 ];do echo $x done

2、for 

例如:for i in 1 2 3;do echo$i done

for循环还可以循环文件:

for i in (cat 1.txt);

do

echo "xxx"

done

3、case   

例如:case i in

    [a-z])

      echo "xxx"

    ;;

    [0-9])

      echo "xxx"

    ;;

    *)

      echo "xxx"

    ;;

esac

二、函数

1、定义函数:functin add(){

if [ $1 -gt $2 ];then

  echo $1

else

  echo $2

fi

}

调用函数:add 1 3

2、(())中可以进行数学计算;

例如:累加

tot=0

for i in 1 2 3

do

  tot=$(($tot+$i))

done

echo $tot

猜你喜欢

转载自www.cnblogs.com/flyforever/p/9317202.html