bash脚本编程---循环
bash为过程式编程语言
代码执行顺序:
1.顺序执行:逐条执行
2.选择执行:代码有一个分支,条件满足时才会执行
两个或以上的分支,只会执行其中一个满足条件的分支
3.循环执行:代码片段(循环体)要执行0,1或多个来回
4.选择执行:
单分支的if语句:
双分支的if语句:if 测试语句then代码分支fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;if 测试条件;then条件为真时执行的分支else条件为假时执行的分支fi
#!/bin/bashif ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi- #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
注意:重定向>之前加&表示不论对错全部输出至指定文件夹
上面的补充:
#!/bin/bashif [ $# -eq 1 ];thenecho "At least one username."exit 2fi#先判断是否输入了一个参数,如果没有或者输入了多个参数就退出#如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个if ! (grep "^$1\>" /etc/passwd &> /dev/null);thenuseradd $1echo $1 | passwd --stdin $1 &>/dev/nullecho "add user $1 finished."elseecho "the user $1 is exists."fi
例2:通过命令行参数给定两个数字,输出其中较大的数值;
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."exit 2fiif [ $1 -ge $2 ];thenecho "Max num is $1."elseecho "Max num is $2."fi
另一种表达方式:
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two num."fideclare -i max=$1if [ $1 -lt $2 ];thenmax=$2fiecho "The max num is $max"#先对max进行赋值,然后进行比较
例2:通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数;
#!/bin/bashif [ $# -lt 1 ];thenecho "Must give a username."finum=$(id -u $1)let num2=$num%2#echo "num is $num"#echo "num2 is $num2"if [ $num2 -eq 1 ];thenecho "the uid is ji"elseecho "the uid is ou"fi
例3:通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;
都存在时返回每个文件的行数,并说明其中行数较多的文件;
#!/bin/bashif [ $# -lt 2 ];thenecho "Must give two filename."exit 2fiif [ -f $1 ];thennum1=$(cat $1 |wc -l)echo "the file $1 is exists and hangshu is $num1."elseecho "the file $1 is not exists."exit 4fiif [ -f $2 ];thennum2=$(cat $2 |wc -l)echo "the file $2 is exists and hangshu is $num2."elseecho "the file $2 is not exists."exit 3fiif [ $num1 -lt $num2 ];thenecho "$2 hangshu is more."elseecho "$1 hangshu us more."
1.bash编程之选择执行--if语句
选择执行:
1.&&,||
2.if语句
3.case语句
if语句有三种格式:
1.1 单分支if语句
if 测试条件;then为真时执行;fi
1.2 双分支if语句
if 测试条件;then为真时执行else为假时执行fi
1.3 多分支语句
if 测试条件;then条件1为真时执行elif condition-2;then条件2为真时执行elif condition-3;then条件3为真时执行。。。。elif condition-n;then条件n为真时执行else所有条件都不满足时执行的分支fi
注意:即便多个条件可能同时都能满足,分支只会执行中其中一个,首先测试为“真”;
例1:脚本参数传递一个文件路径给脚本,判断此文件的类型;
#!/bin/bash#if [ $# -lt 1 ];thenecho "Must give a path."exit 2fiif [ -L $1 ];thenecho "Symbolic link."elif [ -b $1 ];thenecho "block special."elif [ -c $1 ];thenecho "Character special file."elif [ -S $1 ];thenecho "Socket file."elif [ -f $1 ];thenecho "common file."elif [ -d $1 ];thenecho "Directory."lseecho "UNKNOW."fi
例2:写一个脚本
(1) 传递一个参数给脚本,此参数为用户名;
(2) 根据其ID号来判断用户类型(centos7系统):
0: 管理员
1-999:系统用户
1000+:登录用户
#!/bin/bash[ $# -lt 1 ] && echo "At least one username."&& exit 1 #先判断参量是否存在! id $1 &>/dev/null && echo "Bo such user."&& exit 2 #判断用户是否存在uid=$(id -u $1)if [ $uid -eq 0 ];thenecho "the user is root."elif [ $uid -le 1000 ];thenecho "the user is system user."elseecho "the user is login user."fi
2.bash编程之循环执行
循环执行:将一段代码重复执行0、1或多次
进入条件:条件满足时才进入循环
退出条件:每个循环都应该有退出条件,以有条件退出循环
bash脚本:for,while,until
2.1 for循环
两种格式:1.遍历列表;2.控制变量
1.遍历列表:
for VARIABLE in LIST;do循环体done
进入条件:只要列表有元素,即可进入循环
退出条件:列表中的元素表里完成
列表的生成方式:
1.直接给出
2.整数给出
3.返回列表的命令(a):{start..end}(b):seq [start [incremtal]] last
4.glob
5.变量引用:$@,$*
2.2 while循环
while CONDITION;do循环体循环控制变量修正表达式done
进入条件:condition测试条件为”真“
退出条件:condition测试条件为”假“
2.3 until循环
until CONDITION;do循环体循环控制变量修正表达式done
进入条件:condition测试条件为”假“
退出条件:condition测试条件为”真“
例1:求100以内所有正整数之和,用三种方法(for,while,until)
#!/bin/bashdeclare -i sum=0declare -i i=1#until [ $i -gt 100 ];do#判断i的值是否大于100,为假时循环while [ $i -le 100 ];do#判断i的值是否小于等于100,为真时循环let sum+=$ilet i++doneecho $sum
for循环
#!/bin/bashdeclare -i sum=0#for i in {1..100};dofor i in `seq 1 100`;dolet sum=$sum+$idoneecho "sum=$sum"
例2:创建10个用户,user101-user110;密码同用户名;
#!/bin/bashfor i in {101..103};doname="user${i}"! id $name &> /dev/null && useradd $name && echo "add user ${name} succeed!"echo "$i" | passwd --stdin "$name" &> /dev/null && echo "set passwd '${name}' for $name succeed!"done
例3:打印九九乘法表
提示:外循环控制乘数,内循环控制被乘数
#!/bin/bashfor j in {1..9};dofor i in `seq 1 $j`;doecho -n -e "${i}x${j}=$[${i}*${j}]\t"doneechodone
例4:打印逆序九九乘法表
#!/bin/bashfor s in {1..9};doj=$[10-$s]for i in $(seq 1 $j);doecho -n -e "${i}X${j}=$[${i}*${j}]\t"doneechodone
until
#!/bin/bashdeclare -i s=9until [ $s -eq 0 ];dodeclare -i j=1#注意:每次循环,i的值都会发生变化,一定要重新定义until [ $j -gt $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++done- let s--
echodone
while
#!/bin/bashdeclare -i s=9while [ $s -gt 0 ];dodeclare -i j=1while [ $j -le $s ];doecho -n -e "${s}X${j}=$[${s}*${j}]\t"let j++donelet s--echodone

浙公网安备 33010602011771号