码迷,mamicode.com
首页 > 系统相关 > 详细

linux 简单记录4--if,for,while,case 流程控制

时间:2020-06-19 17:57:08      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:add   处理   read   猜猜看   color   iscsi   结果   mes   lists   

--if 条件判断语句

if 条件判断
    then 命令序列1
else 命令序列2
fi
[root@iscsi opt]# vim mkmydata.sh
#!/bin/bash
dir="/opt/mydata"
if [ ! -e $dir ]
then
mkdir -p $dir
fi
[root@iscsi opt]# bash mkmydata.sh 
[root@iscsi opt]# ll /opt/mydata
total 0
[root@iscsi opt]# ls -d /opt/mydata
/opt/mydata

[root@iscsi opt]# vim chkhost.sh
[root@iscsi opt]# bash chkhost.sh 10.15.7.20
host 10.15.7.20 is on-line
[root@iscsi opt]# bash chkhost.sh 10.15.7.21
host 10.15.7.21 is off-line
[root@iscsi opt]# cat chkhost.sh 
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null #-c 次数,-i 定义每个数据包发送间隔,-W 等待超时时间
if [ $? -eq 0 ]
then
echo "host $1 is on-line"
else
echo "host $1 is off-line"
fi
# $?变量是否为0来判断上一条语句执行情况,成功为0,否则非0
if 条件判断1
    then 命令序列1
elif 条件判断2
    then 命令序列2
else 命令序列3
fi
[root@iscsi opt]# vim chkscore.sh
[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): 101
please enter the right score :101 
101 is Fail
[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): 87
87 is Excellent
[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): 70
70 is Pass
[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): -10
please enter the right score :-10 
-10 is Fail

[root@iscsi opt]# cat chkscore.sh 
#!/bin/bash
read -p "Enter your score(0-100): " GRADE
if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ] ; then
echo "please enter the right score :$GRADE "
fi

if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi

[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): 101
please enter the right score :101 
[root@iscsi opt]# vim chkscore.sh 
[root@iscsi opt]# bash chkscore.sh 
Enter your score(0-100): 102
please enter the right score :102 
[root@iscsi opt]# cat chkscore.sh 
#!/bin/bash
read -p "Enter your score(0-100): " GRADE
if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ] ; then
echo "please enter the right score :$GRADE "
exit 0 #exit 1
fi

if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi

2 for循环判断语句

for循环语句允许脚本一次性读取多个信息,然后逐一对信息进行操作处理,当要处理的数据有范围时,使用for循环很适合。
for 变量名 in 取值列表
do
    命令序列
done
[root@iscsi opt]# cat users.txt 
yhq
abc
hong
tang
car
[root@iscsi opt]# cat create_user.sh 
#!/bin/bash
read -p "Enter The Users Password : " PASSWD
for UNAME in `cat users.txt`
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "Already exists"
else
useradd $UNAME &> /dev/null #重定向到空洞文件/dev/null
echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "$UNAME , Create success"
else
echo "$UNAME , Create failure"
fi
fi
done
[root@iscsi opt]# bash create_user.sh 
Enter The Users Password : yhq
yhq , Create success
abc , Create success
hong , Create success
tang , Create success
car , Create success
[root@iscsi opt]# tail -n 5 /etc/passwd
yhq:x:1004:1004::/home/yhq:/bin/bash
abc:x:1005:1005::/home/abc:/bin/bash
hong:x:1006:1006::/home/hong:/bin/bash
tang:x:1007:1007::/home/tang:/bin/bash
car:x:1008:1008::/home/car:/bin/bash

使用ip地址列表,用for循环判断主机ip是否能ping通
[root@iscsi opt]# cat iplist.txt 
10.15.7.20
10.15.7.21
10.15.7.29
[root@iscsi opt]# cat checkiplist.sh 
#!/bin/bash
lists=$(cat /opt/iplist.txt)
for ip in $lists
do
ping -c 3 -i 0.2 -W 3 $ip &> /dev/null
if [ $? -eq 0 ] ; then
echo "host $ip is on-line"
else
echo "host $ip is off-line"
fi
done
[root@iscsi opt]# bash checkiplist.sh 
host 10.15.7.20 is on-line
host 10.15.7.21 is off-line
host 10.15.7.29 is off-line

3 while条件循环

while条件循环语句是一种让脚本根据某些条件来重复执行命令的语句,在执行前不确定最终执行的次数,不同于for循环语句中
有目标、有范围的使用场景。
while 条件判断操作
do
    命令序列
done
[root@iscsi opt]# cat guess.sh 
#!/bin/bash
PRICE=$(expr $RANDOM % 1000) ##expr命令获取结果
TIMES=0
echo "商品实际价格为 0-999 之间,猜猜看是多少? "
while true
do
read -p "请输入您猜测的价格数目: " INT
let TIMES++ ##times变量加1
if [ $INT -eq $PRICE ] ; then
echo "恭喜您答对了,实际价格是 $PRICE"
echo "您总共猜测了 $TIMES 次"
exit 0 ##终止脚本的运行
elif [ $INT -gt $PRICE ] ; then
echo "太高了! "
else
echo "太低了! "
fi
done
[root@iscsi opt]# bash guess.sh 
商品实际价格为 0-999 之间,猜猜看是多少? 
请输入您猜测的价格数目: 123
太低了! 
请输入您猜测的价格数目: 99
太低了! 
请输入您猜测的价格数目: 300
太高了! 
请输入您猜测的价格数目: 200
太低了! 
请输入您猜测的价格数目: 268
太低了! 
请输入您猜测的价格数目: 290
太高了! 
请输入您猜测的价格数目: 280
恭喜您答对了,实际价格是 280
您总共猜测了 7 次

4 case条件判断语句

case 变量值 in
模式1)
    命令序列1
    ;;
模式2)
    命令序列2
    ;;
    .....
*)
    默认命令序列
esac
[root@iscsi opt]# cat checkkeys.sh 
#!/bin/bash
read -p "请输入一个字符,并按 Enter 键确认: " KEY
case "$KEY" in
[a-z]|[A-Z])
echo "您输入的是 字母。 "
;;
[0-9])
echo "您输入的是 数字。 "
;;
*)
echo "您输入的是 空格、功能键或其他控制字符。 "
esac
[root@iscsi opt]# bash checkkeys.sh 
请输入一个字符,并按 Enter 键确认: b
您输入的是 字母。 
[root@iscsi opt]# bash checkkeys.sh 
请输入一个字符,并按 Enter 键确认: 2
您输入的是 数字。 
[root@iscsi opt]# bash checkkeys.sh 
请输入一个字符,并按 Enter 键确认: @
您输入的是 空格、功能键或其他控制字符。

注:实例来自于《linux就该这么学》

linux 简单记录4--if,for,while,case 流程控制

标签:add   处理   read   猜猜看   color   iscsi   结果   mes   lists   

原文地址:https://www.cnblogs.com/yhq1314/p/13164257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!