美文网首页
linux shell脚本-基础知识(三)

linux shell脚本-基础知识(三)

作者: 小pb | 来源:发表于2019-12-04 22:01 被阅读0次

步进循环语句for

1.带列表的for循环语句

基本语法:

for variable in {list}
do 
    statement1
    statement2
done

例1:使用带列表的for语句

#! /bin/bash

for var in 1 2 3 4 5 6 7 8
do
    echo "the number is $var"
done

可将上面的代码改造,得到相同的结果

#! /bin/bash

for var in {1..8}
do
    echo "the number is $var"
done

例2:指定for语句的步长
基本语法:

for variable in {start..end..step}
do
    statement1
    statement2
    ..
done
#! /bin/bash

sum=0;
for i in {1..100..2}
do
    let "sum+=i"
done
echo "the sum is $sum"

例3:使用ls命令的输出结果作为for循环的执行条件

#! /bin/bash

#${ls}可换成*,代表ls *
for file in $(ls)
do
    echo "$file"
done

例4:使用for循环逐个处理脚本的参数

#! /bin/bash

echo "$*"
for arg in $*
do
    echo "${arg}"
done

2.不带列表的for循环语句

基本语法:

for variable
do
    statement1
    statement2
    ..
done

例5:使用不带列表的for循环语句

#! /bin/bash

for arg
do
    echo "$arg"
done

结果为

root@VM_16_3_centos for]$ sh for_arg.sh z x c v b n
z
x
c
v
b
n

3.类C风格的for循环语句

基本语法:

for ((expression1;expression2;expression3))
do
    statement1
    statement2
    ..
done

例6:使用上述语法

#! /bin/bash

for (( i=1; i<5; i++))
do
    echo "$i"
done

4.使用for循环语句处理数组

基本语法:

for variable in ${array[*]}
do
    statement1
    statement2
    ..
done

例7:处理数组

#! /bin/bash

array=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
for day in ${array[*]}
do
    echo $day
done

结果为

[root@VM_16_3_centos for]$ sh array.sh 
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

while循环语句

1.while语句的基本语法

基本语法:

while expression
do
    statement1
    statement2
    ..
done

当expression表达式的值为0时,执行循环内的语句;否则退出while循环

例11:使用while循环来输出数字1~9的平方

#! /bin/bash

i=1
while [ "$i" -lt 10 ]
do
    let "square=i*i"
    echo "$i*$i=$square"
    let "i=i+1"
done

执行结果:

[root@VM_16_3_centos for]$ sh while.sh 
1*1=1
2*2=4
3*3=9
4*4=16
5*5=25
6*6=36
7*7=49
8*8=64
9*9=81

2.通过break语句跳出指定的层数

例12:给breakbreakbreak一个参数使其跳出指定的层数

for (( i=1; i<=9; i++ ))
do
    for (( j=1; j<=i; j++ ))
    do
        let "product=i*j"
        printf "$i*$j=$product"
        # 主要进行格式整理
        if [[ "product" -gt 9 ]]
        then
            printf "    "
        else
            printf "     "
        fi
        if [[ "$j" -eq 5 ]]
        then
            break 2
        fi
    done
    echo
done

结果为:

[root@VM_16_3_centos for]$ sh break.sh 
1*1=1     
2*1=2     2*2=4     
3*1=3     3*2=6     3*3=9     
4*1=4     4*2=8     4*3=12    4*4=16    
5*1=5     5*2=10    5*3=15    5*4=20    5*5=25    [root@VM_16_3_centos for]$

乘法表中当j 等于5的时候,break 2 语句跳出了两层循环,所以在for循环后的echo没有执行到。所以最后的命令提示行和脚本在一行中。

含参数的continue语句

例13:使用含参数的continue语句

#! /bin/bash

for i in a b c d
do
    echo -n "$i "
    for j in `seq 10`
    do
        if [ $j -eq 5 ]
        then
            continue 2
        fi
        echo -n "$j "
    done
    echo
done

执行结果:

[root@VM_16_3_centos for]$ sh continue.sh 
a 1 2 3 4 b 1 2 3 4 c 1 2 3 4 d 1 2 3 4 [root@VM_16_3_centos for]$ 

当执行的到 j 等于5的时候,continue 2 跳过外层echo。

参考文章:
【从0到1 Shell】(六)循环结构

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • linux shell脚本攻略笔记

    LINUX SHELL脚本攻略笔记[速查] linux shell脚本攻略笔记

  • Shell 脚本

    shell脚本之前的基础知识http://www.92csz.com/study/linux/12.htm she...

  • linux shell脚本-基础知识(三)

    步进循环语句for 1.带列表的for循环语句 基本语法: 例1:使用带列表的for语句 可将上面的代码改造,得到...

  • 17. Interview-Linux

    1 用过哪些Linux命令? 2 写过shell脚本吗?shell脚本基本格式? 3 Linux I/O读写方式 ...

  • 指令随笔

    linux 修改shell脚本的编码 在window下编写的shell脚本编码为dos,在linux环境下不能直接...

  • Linux Shell:基础知识和Shell变量

    摘要:Linux,Shell 整理Shell内容要点: Shell基础知识 Shell变量的类型 Shell变量赋...

  • shell脚本

    1)什么是shell脚本 文档: http://www.runoob.com/linux/linux-shell....

  • 自动化脚本实践(Shell + Expect)

    Linux Shell脚本入门: Linux awk 命令 | 菜鸟教程 Shell 教程 | 菜鸟教程 lin...

  • 使用 SHC 加密 Shell 脚本

    如何在Linux环境中加密shell脚本?shell脚本包含密码,不希望其他具有执行权限的人查看shell脚本并获...

网友评论

      本文标题:linux shell脚本-基础知识(三)

      本文链接:https://www.haomeiwen.com/subject/sqhrgctx.html