shell 脚本之 for while 的几种使用方法 以及expr let 括号的使用示例

shell 脚本之 for while 的几种使用方法 以及expr let 括号的使用示例

i=1;((i=i+1));echo $i;
i=1;let 'i++';echo $i;
i=1;i=`expr $i + 1`;echo $i;
i=1;i=$(expr $i + 1);echo $i;
i=1;i=$((i + 1));echo $i;

以上结果都是2

for ((i=0;i<5;i++)); do echo $i;done
for i in $(seq 0 4); do echo $i;done
for i in `seq 0 4`; do echo $i;done
for i in {1..4};do echo $i;done

以上结果都是打印一个顺序号

int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

以上结果也是打印一个顺序号

i=1;while (($i < 10));do echo $i;((i++)); done;
i=1;while [[ $i -lt 10 ]];do echo $i;((i++)); done;
i=1;while [ $i -lt 10 ];do echo $i;((i++)); done;
i=1;while [ $i -lt 10 ];do echo $i;((i=i+1)); done;
i=1;s=0;while [ $i -lt 10 ];do s=`expr $s + $i`;echo ${i}:${s};((i++)); done;

以上是各种方式使用while打印顺序号

if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

以上为if的使用

发布了48 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/chscomfaner/article/details/103885658