标签:
单分支的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
#!/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进行赋值,然后进行比较
#!/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
#!/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."
if 测试条件;then为真时执行;fi
if 测试条件;then为真时执行else为假时执行fi
if 测试条件;then条件1为真时执行elif condition-2;then条件2为真时执行elif condition-3;then条件3为真时执行。。。。elif condition-n;then条件n为真时执行else所有条件都不满足时执行的分支fi
#!/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
#!/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
for VARIABLE in LIST;do循环体done
3.返回列表的命令(a):{start..end}(b):seq [start [incremtal]] last
while CONDITION;do循环体循环控制变量修正表达式done
until CONDITION;do循环体循环控制变量修正表达式done
#!/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
#!/bin/bashdeclare -i sum=0#for i in {1..100};dofor i in `seq 1 100`;dolet sum=$sum+$idoneecho "sum=$sum"
#!/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
#!/bin/bashfor j in {1..9};dofor i in `seq 1 $j`;doecho -n -e "${i}x${j}=$[${i}*${j}]\t"doneechodone
#!/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
#!/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
#!/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
标签:
原文地址:http://www.cnblogs.com/zhangpf/p/5657387.html