bash脚本编程---循环

bash为过程式编程语言
代码执行顺序:
1.顺序执行:逐条执行
2.选择执行:代码有一个分支,条件满足时才会执行
                      两个或以上的分支,只会执行其中一个满足条件的分支
3.循环执行:代码片段(循环体)要执行0,1或多个来回
4.选择执行:
单分支的if语句:
if 测试语句
then
   代码分支
fi
双分支的if语句:
if 测试条件;then
      条件为真时执行的分支
else
     条件为假时执行的分支
fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;
  1. #!/bin/bash
  2. if ! (grep "^$1>" /etc/passwd &> /dev/null);then
  3. useradd $1
  4. echo $1 | passwd --stdin $1 &>/dev/null
  5. echo "add user $1 finished."
  6. else
  7. echo "the user $1 is exists."
  8. fi
  9. #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
注意:重定向>之前加&表示不论对错全部输出至指定文件夹
上面的补充:
  1. #!/bin/bash
  2. if [ $# -eq 1 ];then
  3. echo "At least one username."
  4. exit 2
  5. fi
  6. #先判断是否输入了一个参数,如果没有或者输入了多个参数就退出
  7. #如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个
  8. if ! (grep "^$1>" /etc/passwd &> /dev/null);then
  9. useradd $1
  10. echo $1 | passwd --stdin $1 &>/dev/null
  11. echo "add user $1 finished."
  12. else
  13. echo "the user $1 is exists."
  14. fi
例2:通过命令行参数给定两个数字,输出其中较大的数值;
  1. #!/bin/bash
  2. if [ $# -lt 2 ];then
  3. echo "Must give two num."
  4. exit 2
  5. fi
  6. if [ $1 -ge $2 ];then
  7. echo "Max num is $1."
  8. else
  9. echo "Max num is $2."
  10. fi
另一种表达方式:
  1. #!/bin/bash
  2. if [ $# -lt 2 ];then
  3. echo "Must give two num."
  4. fi
  5. declare -i max=$1
  6. if [ $1 -lt $2 ];then
  7. max=$2
  8. fi
  9. echo "The max num is $max"
  10. #先对max进行赋值,然后进行比较
例2:通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数;
  1. #!/bin/bash
  2. if [ $# -lt 1 ];then
  3. echo "Must give a username."
  4. fi
  5. num=$(id -u $1)
  6. let num2=$num%2
  7. #echo "num is $num"
  8. #echo "num2 is $num2"
  9. if [ $num2 -eq 1 ];then
  10. echo "the uid is ji"
  11. else
  12. echo "the uid is ou"
  13. fi
例3:通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;
都存在时返回每个文件的行数,并说明其中行数较多的文件;
  1. #!/bin/bash
  2. if [ $# -lt 2 ];then
  3. echo "Must give two filename."
  4. exit 2
  5. fi
  6. if [ -f $1 ];then
  7. num1=$(cat $1 |wc -l)
  8. echo "the file $1 is exists and hangshu is $num1."
  9. else
  10. echo "the file $1 is not exists."
  11. exit 4
  12. fi
  13. if [ -f $2 ];then
  14. num2=$(cat $2 |wc -l)
  15. echo "the file $2 is exists and hangshu is $num2."
  16. else
  17. echo "the file $2 is not exists."
  18. exit 3
  19. fi
  20. if [ $num1 -lt $num2 ];then
  21. echo "$2 hangshu is more."
  22. else
  23. echo "$1 hangshu us more."

1.bash编程之选择执行--if语句

选择执行:
1.&&,||
2.if语句
3.case语句
if语句有三种格式:

1.1 单分支if语句

  1. if 测试条件;then
  2. 为真时执行;
  3. fi

1.2 双分支if语句

  1. if 测试条件;then
  2. 为真时执行
  3. else
  4. 为假时执行
  5. fi

1.3 多分支语句

  1. if 测试条件;then
  2. 条件1为真时执行
  3. elif condition-2;then
  4. 条件2为真时执行
  5. elif condition-3;then
  6. 条件3为真时执行
  7. 。。。。
  8. elif condition-n;then
  9. 条件n为真时执行
  10. else
  11. 所有条件都不满足时执行的分支
  12. fi
注意:即便多个条件可能同时都能满足,分支只会执行中其中一个,首先测试为“真”;
例1:脚本参数传递一个文件路径给脚本,判断此文件的类型;
  1. #!/bin/bash
  2. #
  3. if [ $# -lt 1 ];then
  4. echo "Must give a path."
  5. exit 2
  6. fi
  7. if [ -L $1 ];then
  8. echo "Symbolic link."
  9. elif [ -b $1 ];then
  10. echo "block special."
  11. elif [ -c $1 ];then
  12. echo "Character special file."
  13. elif [ -S $1 ];then
  14. echo "Socket file."
  15. elif [ -f $1 ];then
  16. echo "common file."
  17. elif [ -d $1 ];then
  18. echo "Directory."lse
  19. echo "UNKNOW."
  20. fi
例2:写一个脚本
(1) 传递一个参数给脚本,此参数为用户名;
(2) 根据其ID号来判断用户类型(centos7系统):
      0: 管理员
      1-999:系统用户
      1000+:登录用户
  1. #!/bin/bash
  2. [ $# -lt 1 ] && echo "At least one username."&& exit 1 #先判断参量是否存在
  3. ! id $1 &>/dev/null && echo "Bo such user."&& exit 2 #判断用户是否存在
  4. uid=$(id -u $1)
  5. if [ $uid -eq 0 ];then
  6. echo "the user is root."
  7. elif [ $uid -le 1000 ];then
  8. echo "the user is system user."
  9. else
  10. echo "the user is login user."
  11. fi

2.bash编程之循环执行

循环执行:将一段代码重复执行0、1或多次
进入条件:条件满足时才进入循环
退出条件:每个循环都应该有退出条件,以有条件退出循环
bash脚本:for,while,until

2.1 for循环

两种格式:1.遍历列表;2.控制变量
1.遍历列表:
  1. for VARIABLE in LIST;do
  2. 循环体
  3. done
进入条件:只要列表有元素,即可进入循环
退出条件:列表中的元素表里完成
列表的生成方式:
1.直接给出
2.整数给出
(a):{start..end}
(b):seq [start [incremtal]] last
3.返回列表的命令
4.glob
5.变量引用:$@,$*

2.2 while循环

  1. while CONDITION;do
  2. 循环体
  3. 循环控制变量修正表达式
  4. done
进入条件:condition测试条件为”真“
退出条件:condition测试条件为”假“

2.3 until循环

  1. until CONDITION;do
  2. 循环体
  3. 循环控制变量修正表达式
  4. done
进入条件:condition测试条件为”假“
退出条件:condition测试条件为”真“
例1:求100以内所有正整数之和,用三种方法(for,while,until)
  1. #!/bin/bash
  2. declare -i sum=0
  3. declare -i i=1
  4. #until [ $i -gt 100 ];do
  5. #判断i的值是否大于100,为假时循环
  6. while [ $i -le 100 ];do
  7. #判断i的值是否小于等于100,为真时循环
  8. let sum+=$i
  9. let i++
  10. done
  11. echo $sum
for循环
  1. #!/bin/bash
  2. declare -i sum=0
  3. #for i in {1..100};do
  4. for i in `seq 1 100`;do
  5. let sum=$sum+$i
  6. done
  7. echo "sum=$sum"
例2:创建10个用户,user101-user110;密码同用户名;
  1. #!/bin/bash
  2. for i in {101..103};do
  3. name="user${i}"
  4. ! id $name &> /dev/null && useradd $name && echo "add user ${name} succeed!"
  5. echo "$i" | passwd --stdin "$name" &> /dev/null && echo "set passwd '${name}' for $name succeed!"
  6. done
例3:打印九九乘法表
提示:外循环控制乘数,内循环控制被乘数
  1. #!/bin/bash
  2. for j in {1..9};do
  3. for i in `seq 1 $j`;do
  4. echo -n -e "${i}x${j}=$[${i}*${j}] "
  5. done
  6. echo
  7. done
例4:打印逆序九九乘法表
  1. #!/bin/bash
  2. for s in {1..9};do
  3. j=$[10-$s]
  4. for i in $(seq 1 $j);do
  5. echo -n -e "${i}X${j}=$[${i}*${j}] "
  6. done
  7. echo
  8. done
until
  1. #!/bin/bash
  2. declare -i s=9
  3. until [ $s -eq 0 ];do
  4. declare -i j=1
  5. #注意:每次循环,i的值都会发生变化,一定要重新定义
  6. until [ $j -gt $s ];do
  7. echo -n -e "${s}X${j}=$[${s}*${j}] "
  8. let j++
  9. done
  10. let s--
  11. echo
  12. done
while
  1. #!/bin/bash
  2. declare -i s=9
  3. while [ $s -gt 0 ];do
  4. declare -i j=1
  5. while [ $j -le $s ];do
  6. echo -n -e "${s}X${j}=$[${s}*${j}] "
  7. let j++
  8. done
  9. let s--
  10. echo
  11. done

原文地址:https://www.cnblogs.com/zhangpf/p/5657387.html