while循环:条件满足,则循环;失败,则退出
如何退出?
必须有时刻,条件测试不成功
? :条件控制变量
while 条件测试:do
循环体
done
until循环;条件不满足,则循环;否则,退出
until 测试条件;do
循环体
done
bash编程之组合测试条件
逻辑与:多个条件同时满足
[ CONDITION1 ] && [ CONDITION2 ]
[ CONDITION1 -a CONDITION2 ]
[[ CONDITION1 && CONDITION2 ]]
注意:前两个使用单双中括号都可,但&&不允许用于单个中括号中,所有第三者只能用于双中括号中
逻辑或:多个条件满足一个
[ CONDITION1 ] || [ CONDITION2 ]
[ CONDITION1 -o CONDITION2 ]
[[ CONDITION1 || CONDITION2 ]]
注意 || 不允许出现在单中括号中
得摩根定律
!(条件1或者 条件2) = !条件1 并且!条件2
!(条件1且条件2)=!条件1 或者 !条件2
练习:
1.:通过键盘提示用户输入字符,将用户输入的小写字母转换为大写,转换一次之后,在此提醒,再输入再转换,直到输入quit退出;
#! /bin/bash#read -p -t 5 "Enter a Word: " word
while [[ "$word" != "quit" ]]; do
echo $word | tr 'a-z' 'A-Z'
read -p -t 5 "Enter a Word again: " word
done2.写一个脚本,实现如下功能;
1、显示如下菜单:
CPU) show cpu info;
men) show memory info;
disk) show disk info;
quit) quit
Enter your option:
2、根据用户的选择输出相应信息,每次执行后,不退出,而由用户咋此指定新的选项
#! /bin/bash#cat <<EOF
cpu) print cpu infomation
men) print memory infomation
disk) print disk infomation
quit) Quit
EOFread -p "Enter your option: " option
option=`echo $option | tr 'a-z' 'A-Z'`
while [[ "$option" != "QUIT" ]]; do
if [[ "$option" == "CPU" ]]; then
cat /proc/cpuinfo
elif [[ "$option" == "MEM" ]]; then
free -m
elif [[ "$option" == "DISK" ]]; then
df -Th
else
echo "Wrong Option..."
fi
read -p "Enter your option: " option
option=`echo $option | tr 'a-z' 'A-Z'`
done3.提示用户输入一个用户名,显示用户名UID和SHELl信息,否则,则显示无此用户,显示完成后,提示用户再次输入,如果quit则退出;
#! /bin/bash#cat <<EOF
Username)Enter your Username..
quit)quit..
EOFread -p "Enter Your userName: " userName
userName=`echo $userName | tr 'A-Z' 'a-z'`
while [[ "$userName" != "QUIT" ]]; do
sysUser=`cat /etc/passwd | grep ^$userName | cut -d: -f1 | tr 'A-Z' 'a-z'`
if [[ "$userName" == "$sysUser" ]]; then
echo "This $userName `cat /etc/passwd | grep -i ^$userName | cut -d: -f3,7`"
else
echo "No Such $userName.."
fi
read -p "Enter Your userName: " userName
userName=`echo $userName | tr 'A-Z' 'a-z'`
done#! /bin/bash#read -t 2 -p "Enter a user name: " userName
userName=`echo $userName | tr 'A-Z' 'a-z'`
UID=`grep "^$userName\>" /etc/passwd | cut -d: -f3`
SH=`grep "^$userName\>" /etc/passwd | cut -d: -f7`
while [[ "$userName" != "quit" ]]; do
if [ -z "$userName" ]; then
echo "Username null...."
elif id $userName &> /dev/null; then
echo "$userName uid: $UID"
echo "$userName Shell: $SH"
else
echo "No such user...."
fi
read -t 2 -p "Enter a user name again(quit to exit) " userName
done4.求100以内所有正整数的和;
#! /bin/bash#declare -i sum=0
declare -i i=1
while [ $i -le 100 ];do
let sum+=$i
let i++
doneecho $sum
#! /bin/bash#declare -i sum=0
declare -i i=1
until [ $i -gt 100 ]; do
let sum+=$i
let i++
doneecho $sum
5.求100以内所有偶数之和;
#! /bin/bash#declare -i evensum=0
declare -i i=1
while [ $i -le 100 ]; do
if [ $[$i%2] -eq 0 ]; then
let evensum+=$i
fi
let i++
doneecho $evensum
#! /bin/bash#declare -i sum=0
declare -i i=0
while [[ $i -le 100 ]]; do
let sum+=$i
let i+=2
doneecho $sum
6.用until求100以内整数之和;
#! /bin/bash#declare -i sum=0
declare -i i=1
until [ $i -gt 100 ]; do
let sum+=$i
let i++
doneecho $sum
7.提供一个用户名,判断用户是否登陆当前系统;
1.如果没有登陆,则停止5秒之后再次判断,直到用户登录系统,显示用户登录,而后退出
#! /bin/bash
#
read -t 5 -p "Enter Your userName: " userName
userName=`echo $userName | tr 'A-Z' 'a-z'`
who | grep "$userName" &> /dev/null
retVal=$?
while [ $retVal -ne 0 ]; do
sleep 5
read -t 5 -p "Enter Your userName: " userName
userName=`echo $userName | tr 'A-Z' 'a-z'`
done
echo "Welcome $userName login System..."
#! /bin/bash
#
read -t 5 -p "Enter Your userName: " userName
while ! id $userName &> /dev/null; do
read -t 5 -p "Enter Your userName: " userName
done
who | grep "^$userName" &> /dev/null
retVal=$?
while [ $retVal -ne 0 ]; do
sleep 5
who | grep "$userName" &> /dev/null
retVal=$?
done
echo "Welcome $userName login System..."
#! /bin/bash
#
read -t 5 -p "Enter Your userName: " userName
while ! id $userName &> /dev/null; do
read -t 5 -p "Enter Your userName again: " userName
done
while ! who | grep "^$userName" &> /dev/null; do
sleep 5
done
echo "Welcome $userName login System..."
#! /bin/bash
#read -t 5 -p "Enter Your userName: " userName
until [ -n "$userName" ] && id $userName &> /dev/null ; do
read -t 5 -p "Enter Your userName again: " userName
doneuntil who | grep "^$userName" &> /dev/null; do
sleep 5
doneecho "Welcome $userName login System..."
8.取出当前系统上,默认shell为bash的用户;
#! /bin/bash#while read line; do
[[ `echo $line | cut -d: -f7` == "/bin/bash" ]] && echo $line | cut -d: -f1
done < /etc/passwd
9.显示其ID号为偶数的用户;
#! /bin/bash#while read line; do
userID=`echo $line | cut -d: -f3`
if [ $[$userID%2] -eq 0 ]; then
echo -n "$userID: "
echo $line | cut -d: -f1
fi
done < /etc/passwd
10.显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30的行;
#! /bin/bash#while read line; do
charCounts=`echo $line | wc -c`
if [ $charCounts -gt 30 ]; then
echo -n "$charCounts: "
echo $line
fi
done < /etc/rc.d/rc.sysinit
11.显示用户其UID和GID均为偶数的用户;
#! /bin/bash#while read line; do
userID=`echo $line | cut -d: -f3`
groupID`echo $line | cut -d: -f4`
if [ $[$userID%2] -eq 0 -a $[$groupID%2] -eq 0 ]; then
echo -n "$userID,$groupID: "
echo $line | cut -d: -f1
fi
done < /etc/passwd
12.显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30且以非#开头的行;
#! /bin/bash#while read line; do
charCounts=`echo $line | wc -c`
if [ $charCounts -gt 30 ] && [["$line" =~ ^[^#] ]]; then
echo -n "$charCounts: "
echo $line
fi
done < /etc/rc.d/rc.sysinit
13.写一个脚本,完成如下任务;
1.提示用户输入一个磁盘设备文件路径不存在或不是一个块设备,则提示用户重新输入,知道输入正确为止,或者输入quit以9为退出码结束脚本
2.提示用户"下面的操作会清空磁盘的数据,并提问是否继续"
如果用户给出字符y或yes,则继续,否则,则提供以8为退出码结束脚本
3.将用户指定的磁盘上的分区清空,而后创建两个分区,大小分别为100M和512M
4.格式化这两个分区
5.将第一个分区挂载至/mnt/boot目录,第二个分区挂载至/mnt/sysroot目录