标签:
单分支的if语句:
双分支的if语句:if 测试语句then代码分支fi
例1:通过参数传递一个用户名给脚本,此用户不存时,则添加之;if 测试条件;then条件为真时执行的分支else条件为假时执行的分支fi
#!/bin/bash
if ! (grep "^$1\>" /etc/passwd &> /dev/null);then
useradd $1
echo $1 | passwd --stdin $1 &>/dev/null
echo "add user $1 finished."
else
echo "the user $1 is exists."
fi
- #如果给出的用户不存在,就添加,用户帐号与密码相同,如果存在就提示存在
#!/bin/bash
if [ $# -eq 1 ];then
echo "At least one username."
exit 2
fi
#先判断是否输入了一个参数,如果没有或者输入了多个参数就退出
#如果是“-lt”,则表示最少一个参数,即使输入多个也只取第一个
if ! (grep "^$1\>" /etc/passwd &> /dev/null);then
useradd $1
echo $1 | passwd --stdin $1 &>/dev/null
echo "add user $1 finished."
else
echo "the user $1 is exists."
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two num."
exit 2
fi
if [ $1 -ge $2 ];then
echo "Max num is $1."
else
echo "Max num is $2."
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two num."
fi
declare -i max=$1
if [ $1 -lt $2 ];then
max=$2
fi
echo "The max num is $max"
#先对max进行赋值,然后进行比较
#!/bin/bash
if [ $# -lt 1 ];then
echo "Must give a username."
fi
num=$(id -u $1)
let num2=$num%2
#echo "num is $num"
#echo "num2 is $num2"
if [ $num2 -eq 1 ];then
echo "the uid is ji"
else
echo "the uid is ou"
fi
#!/bin/bash
if [ $# -lt 2 ];then
echo "Must give two filename."
exit 2
fi
if [ -f $1 ];then
num1=$(cat $1 |wc -l)
echo "the file $1 is exists and hangshu is $num1."
else
echo "the file $1 is not exists."
exit 4
fi
if [ -f $2 ];then
num2=$(cat $2 |wc -l)
echo "the file $2 is exists and hangshu is $num2."
else
echo "the file $2 is not exists."
exit 3
fi
if [ $num1 -lt $num2 ];then
echo "$2 hangshu is more."
else
echo "$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 ];then
echo "Must give a path."
exit 2
fi
if [ -L $1 ];then
echo "Symbolic link."
elif [ -b $1 ];then
echo "block special."
elif [ -c $1 ];then
echo "Character special file."
elif [ -S $1 ];then
echo "Socket file."
elif [ -f $1 ];then
echo "common file."
elif [ -d $1 ];then
echo "Directory."lse
echo "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 ];then
echo "the user is root."
elif [ $uid -le 1000 ];then
echo "the user is system user."
else
echo "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/bash
declare -i sum=0
declare -i i=1
#until [ $i -gt 100 ];do
#判断i的值是否大于100,为假时循环
while [ $i -le 100 ];do
#判断i的值是否小于等于100,为真时循环
let sum+=$i
let i++
done
echo $sum
#!/bin/bash
declare -i sum=0
#for i in {1..100};do
for i in `seq 1 100`;do
let sum=$sum+$i
done
echo "sum=$sum"
#!/bin/bash
for i in {101..103};do
name="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/bash
for j in {1..9};do
for i in `seq 1 $j`;do
echo -n -e "${i}x${j}=$[${i}*${j}]\t"
done
echo
done
#!/bin/bash
for s in {1..9};do
j=$[10-$s]
for i in $(seq 1 $j);do
echo -n -e "${i}X${j}=$[${i}*${j}]\t"
done
echo
done
#!/bin/bash
declare -i s=9
until [ $s -eq 0 ];do
declare -i j=1
#注意:每次循环,i的值都会发生变化,一定要重新定义
until [ $j -gt $s ];do
echo -n -e "${s}X${j}=$[${s}*${j}]\t"
let j++
done
- let s--
echo
done
#!/bin/bash
declare -i s=9
while [ $s -gt 0 ];do
declare -i j=1
while [ $j -le $s ];do
echo -n -e "${s}X${j}=$[${s}*${j}]\t"
let j++
done
let s--
echo
done
标签:
原文地址:http://www.cnblogs.com/zhangpf/p/5657387.html