脚本编程:
顺序结构
选择结构
if
case
循环结构
for
while
until
while循环:适合循环次数未知的场景,一定要有退出条件
while
进入循环:条件满足
退出循环:条件不满足
语法:
while CONDITON;do
statement
…
done
-+
计算100以内所有整数的合
#!/bin/bash
#
declare -i I=1
declare -i SUM=0
while [ $I -le 100];do
let SUM+=$I
let I+=1
done
echo"$SUM"
练习2:写一个脚本,用户输入任何字母都变成大写,输入quit退出
#!/bin/bash
#
read -p "Inputsometing:" STRING
while [ $STRING !=‘quit‘ ];do
echo $STRING |tr‘a-z‘ ‘A-Z‘
read -p "Inputsometing:" STRING
done
练习3.写一个脚本每隔5秒钟检测一下admin用户是否登录。
#!/bin/bash
#
who |grep admin&>/dev/null
RETVAL=$?
while [ $RETVAL -ne0 ];do
echo "`date`,admin is not log."
sleep 5
who |grep admin&>/dev/null
RETVAL=$?
done
echo "admin islogged in."
until
进入循环:条件不满足
退出循环:条件满足
until CONDITION;do
statement
…
done
练习,写一个脚本,当用户输入quit时退出,输入小写字母时自动变成大写字母
#!/bin/bash
#
read -p"shuru": R
until [ $R = ‘quit‘];do
echo $R |tr ‘a-z‘‘A-Z‘
read -p"shuru": R
done
练习,写一个脚本,每隔五秒钟判断admin是否登录
#!/bin/bash
#
who|grep admin&>/dev/null
R=$?
until [ $R -eq 0];do
echo"`date`,admin not come"
sleep 5
who|grep admin&>/dev/null
R=$?
done
echo “admin loggedin.”
本文出自 “linux运维” 博客,谢绝转载!
原文地址:http://coolcl.blog.51cto.com/4514424/1789166