linux——shell把编程:循环

循环:

  • for:

    for 变量名 in 列表;do
      循环体
    done
  • while:

  • until:

获取循环列表:

  • 直接写出列表:
  • 命令生成
    • {1..10},{a..z}
    • seq命令:
  • 返回列表的命令:$(ls /data),`ls /data`
  • 使用glob:例如:*.sh
  • 引用变量:$@,$,(脚本中,“$@”,参数独立显示,“$”为参数数组)

for循环1..100求和:

#!/bin/bash

declare -i sum=0 #声明int变量
for i in {1..100};do
   let sum+=i
done
echo sum=$sum

for1..100奇数求和:

#!/bin/bash

declare -i sum=0 #声明int变量
for i in {1..100};do
    if [ $[i%2] -eq 1 ];then
        let sum+=i
    fi
done
echo sum=$sum

猜你喜欢

转载自www.cnblogs.com/franc/p/12520167.html
今日推荐