shell-流程控制

shell-流程控制

for语句

数字循环1

#!/bin/bash
for((i=1;i<=3;i++));
do
    echo ${i}
done

 

数字循环2

#!/bin/bash
for i in $(seq 1 3);
do
    echo ${i}
done

 

数字循环3

#!/bin/bash
for i in {1..3};
do
    echo ${i}
done

 

数字循环4

#!/bin/bash
awk 'BEGIN{for(i=1; i<=3; i++) print i}'  

 

字符循环1

#!/bin/bash
for s in "hello";
do
    echo ${s}
done

 

字符循环2

#!/bin/bash
for c in $*;
do
    echo ${c} is the input char
done

 

字符循环3

#!/bin/bash
for c in 1 2 3;
do
    echo ${c}
done

 

字符循环4

#!/bin/bash
str1="hello world"
for c in ${str1};
do
    echo ${c}
done

 

路径循环1

#!/bin/bash
for
file in /proc/*; do echo $file is file path \! ; done

 

路径循环2

#!/bin/bash  
for file in $(ls *.sh)  
do  
echo $file is file path \! ;  
done  

 

while语句

用于执行一系列命令和从输入文件中读取数据

#!/bin/bash
x=1
while((${x}<5));
do
    echo ${x}
    #let "x++"
    x=`expr ${x} + 1`
done

说明:let命令,用于执行一个或多个表达式,变量计算中不需要加上$来表示变量

 

无限循环

#!/bin/bash
while true
do
    echo 1
done

or

#!/bin/bash
for((;;));
do
    echo 1
done

 

until语句

用于执行一系列命令直至条件为true时停止,一般while优于until

#!/bin/bash
x=1
until((x>3));
do
    echo ${x}
    x=`expr ${x} + 1`
done

 

case语句

#!/bin/bash
score="A"

case ${score} in
    "A") echo "A SCORE"
    ;;
    "B") echo "B SCORE"
    ;;
    *) echo "UNKNOWN SCORE"
esac

 

运算符

比较运算符

-gt    大于

-lt     小于

-eq   等于

==    等于则条件为真

!=     不等于则条件为真

-z     字符串长度为0则为真

-n     字符串长度不为0则为真

 

逻辑运算符

-a    并且

-o    或者

 

参考资料:Linux下Shell的for循环语句

参考资料:与shell脚本编程大全

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值