shell函数与数组

Posted 尹正杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell函数与数组相关的知识,希望对你有一定的参考价值。

              SHELL脚本编程循环篇-for循环

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

 

一.for循环的语法格式
[root@node101.yinzhengjie.org.cn ~]# help for
for: for NAME [in WORDS ... ] ; do COMMANDS; done
    Execute commands for each member in a list.
    
    The `for\' loop executes a sequence of commands for each member in a
    list of items.  If `in WORDS ...;\' is not present, then `in "$@"\' is
    assumed.  For each element in WORDS, NAME is set to that element, and
    the COMMANDS are executed.
    
    Exit Status:
    Returns the status of the last command executed.
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
    Arithmetic for loop.
    
    Equivalent to
        (( EXP1 ))
        while (( EXP2 )); do
            COMMANDS
            (( EXP3 ))
        done
    EXP1, EXP2, and EXP3 are arithmetic expressions.  If any expression is
    omitted, it behaves as if it evaluates to 1.
    
    Exit Status:
    Returns the status of the last command executed.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# help for
1>.语法格式一
for 变量 in 值1 值2 值3 ...
    do
        源代码
done

也可以写成一行,案例如下:   [root@node101.yinzhengjie.org.cn
~]# for i in {1..100};do let sum1+=i;done;echo sum=$sum1;   sum=5050   [root@node101.yinzhengjie.org.cn ~]#
2>.语法格式二(类似C语言风格的变量操作)
for (( 初始值;循环控制条件;变量变化 ))
    do
        源代码
done


也可以写成一行,案例如下:   [root@node101.yinzhengjie.org.cn
~]# for((sum=0,i=1;i<=100;i++));do let sum2+=i;done;echo sum=$sum2   sum=5050   [root@node101.yinzhengjie.org.cn ~]#

3>.for循环执行机制

  依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束
 
二.列表生成方式
1>.直接给出列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list1.sh 
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/list.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i count=1

for i in 1 a 3 b 5 c 7
    do
        echo "arg $count is $i"
    count=count+1
done
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list1.sh
arg 1 is 1
arg 2 is a
arg 3 is 3
arg 4 is b
arg 5 is 5
arg 6 is c
arg 7 is 7
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list1.sh
2>.整数列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list2.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/list2.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i count=1

for i in {A..z}
    do
        echo "arg $count is $i"
    count=count+1
done
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list2.sh
arg 1 is A
arg 2 is B
arg 3 is C
arg 4 is D
arg 5 is E
arg 6 is F
arg 7 is G
arg 8 is H
arg 9 is I
arg 10 is J
arg 11 is K
arg 12 is L
arg 13 is M
arg 14 is N
arg 15 is O
arg 16 is P
arg 17 is Q
arg 18 is R
arg 19 is S
arg 20 is T
arg 21 is U
arg 22 is V
arg 23 is W
arg 24 is X
arg 25 is Y
arg 26 is Z
arg 27 is [
arg 28 is 
arg 29 is ]
arg 30 is ^
arg 31 is _
arg 32 is `
arg 33 is a
arg 34 is b
arg 35 is c
arg 36 is d
arg 37 is e
arg 38 is f
arg 39 is g
arg 40 is h
arg 41 is i
arg 42 is j
arg 43 is k
arg 44 is l
arg 45 is m
arg 46 is n
arg 47 is o
arg 48 is p
arg 49 is q
arg 50 is r
arg 51 is s
arg 52 is t
arg 53 is u
arg 54 is v
arg 55 is w
arg 56 is x
arg 57 is y
arg 58 is z
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list2.sh
3>.返回列表的命令
[root@node101.yinzhengjie.org.cn ~]# cat shell/list3.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/list3.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i count=1

for i in `seq 100 3 200`
    do
        echo "arg $count is $i"
    count=count+1
done
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list3.sh
arg 1 is 100
arg 2 is 103
arg 3 is 106
arg 4 is 109
arg 5 is 112
arg 6 is 115
arg 7 is 118
arg 8 is 121
arg 9 is 124
arg 10 is 127
arg 11 is 130
arg 12 is 133
arg 13 is 136
arg 14 is 139
arg 15 is 142
arg 16 is 145
arg 17 is 148
arg 18 is 151
arg 19 is 154
arg 20 is 157
arg 21 is 160
arg 22 is 163
arg 23 is 166
arg 24 is 169
arg 25 is 172
arg 26 is 175
arg 27 is 178
arg 28 is 181
arg 29 is 184
arg 30 is 187
arg 31 is 190
arg 32 is 193
arg 33 is 196
arg 34 is 199
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list3.sh
4>.使用glob,如:"*.sh"
[root@node101.yinzhengjie.org.cn ~]# cat shell/list4.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/list4.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i count=1

for i in /etc/profile.d/*.sh
    do
        echo "arg $count is $i"
        count=count+1
done
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list4.sh
arg 1 is /etc/profile.d/256term.sh
arg 2 is /etc/profile.d/bash_completion.sh
arg 3 is /etc/profile.d/colorgrep.sh
arg 4 is /etc/profile.d/colorls.sh
arg 5 is /etc/profile.d/crond.sh
arg 6 is /etc/profile.d/lang.sh
arg 7 is /etc/profile.d/less.sh
arg 8 is /etc/profile.d/qt-graphicssystem.sh
arg 9 is /etc/profile.d/qt.sh
arg 10 is /etc/profile.d/vim.sh
arg 11 is /etc/profile.d/which2.sh
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list4.sh
5>.位置变量引用
[root@node101.yinzhengjie.org.cn ~]# cat shell/list5.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/list4.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i count=1

for i in $@
    do
        echo "arg $count is $i"
        count=count+1
done
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list5.sh python java golang shell c++ php
arg 1 is python
arg 2 is java
arg 3 is golang
arg 4 is shell
arg 5 is c++
arg 6 is php
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/list5.sh python java golang shell c++ php
 
.请用for循环实现以下小练习
1>.计算1到100之间的和
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/sum.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

sum=0

for i in {1..100};do
    let sum=sum+i
done

echo "1到100的和为: $sum"
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh
1到100的和为: 5050
[root@node101.yinzhengjie.org.cn ~]# 
参考案例1
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/sum.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

sum=0

for i in {1..100};do
    let sum=$[sum+i]
done

echo "1到100的和为: $sum"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh
1到100的和为: 5050
[root@node101.yinzhengjie.org.cn ~]# 
参考案例2
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/sum.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

sum=0

for i in {1..100};do
    let sum=$((sum+i))
done

echo "1到100的和为: $sum"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh
1到100的和为: 5050
[root@node101.yinzhengjie.org.cn ~]# 
参考案例3
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/sum.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

declare -i sum=0

for i in {1..100};do
    sum=sum+i
done

echo "1到100的和为: $sum"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh
1到100的和为: 5050
[root@node101.yinzhengjie.org.cn ~]# 
参考案例4
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh 
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/sum.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

sum=0
read -t 30 -p "Please enter the start number>>> " StartNumber
read -t 30 -p "Please enter an end number>>> " EndNumber

for ((i=$StartNumber;i<=$EndNumber;i=i+1))
    do
       sum=$(($sum+$i))
done
        
echo "从$StartNumber加到$EndNumber的总和是:$sum"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 
Please enter the start number>>> 1
Please enter an end number>>> 100
从1加到100的总和是:5050
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
参考案例5
2>.猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/monkey.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

sum=1
for i in {9..1}
    do
        let sum=(sum+1)*2
done
echo "桃子的个数是: $sum"
unset sum
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
桃子的个数是: 1534
[root@node101.yinzhengjie.org.cn ~]# 
参考案例1
[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#FileName:        shell/monkey.sh
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The test script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

read -p "请输入天数: " day
read -p "请输入最后一天剩余个数: " sum

let day=day-1

for i in `seq 1 $day`
    do
        let sum=(sum+1)*2
done
echo "桃子的个数是: $sum"
unset sum
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
请输入天数: 10
请输入最后一天剩余个数: 1
桃子的个数是: 1534
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
请输入天数: 32
请输入最后一天剩余个数: 2
桃子的个数是: 8589934590
[root@node101.yinzhengjie.org.cn ~]# 
参考案例2
3>.将一个目录的所有文件后缀都更改为"*.log"