shell基础编程
1编写shell程序判断字符串是否为数字字符串
#! /bin/bash
expr $1 "+" 10 &> /dev/null
if [ $? -eq 0 ];then
echo "$1 is number"
else
echo "$1 not number"
fi
2编写shell程序比较2个数的大小
#! /bin/bash
max=0
if [ "$1" -ge "$2" ]
then
max="$1"
else
max="$2"
fi
echo "max的值为:"
echo $max
3编写shell程序判断输入的信息是否为0·9数字,如果是则输出对应的数字,否则输出“输入不正确提示”
#! /bin/bash
read -p "Please input an integer: " num
case "$num" in
*[!0-9]*)
echo "输入不正确";;
* )
echo $num;;
esac
4编写shell程序计算1·100的和
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$i+$sum]
done
echo $sum
~
5 编写shell程序每个2秒输出一次“系统挂载”
#! /bin/bash
while true
do
echo "系统挂载"
sleep 1
done
6 编写shell程序批量创建10个账号并创建密码
创建用户
#! /bin/bash
source /etc/profile
for num in `seq -f '%02g' 1 10`
do
useradd yuki${num} -M
definePasswd=`echo $RANDOM |md5sum|cut -c 1-8`
echo ${definePasswd} | passwd --stdin yuki${num}
echo yuki${num} definePasswd is ${definePasswd} >> /home/savePasswd.log
done
删除用户
#! /bin/bash
source /etc/profile
for num in `seq -f '%02g' 1 10`
do
userdel -r yuki${num}
done
7 编写shell程序:手机充值10元,每发一条信息花费1角5分。当余额低于1角5分时,不能再发短信,并提示“余额不足,请充值”。充值后可以继续发短信。
#! /bin/bash
TOTAL=1000
CONSUME=200
function isnum(){
expr $1 + 1 &>/dev/null
if [ $? -ne 0 -a "$1" != "-1" ];then
return 1
fi
return 0
}
function consume(){
read -p "please input your message:" content
read -p "Are you sure send?{y|Y|n|N}:" option
case $option in
y|Y)
echo "send $content successfully!!!"
((TOTAL=TOTAL-CONSUME))
echo "you money haved $TOTAL availed"
;;
n|N)
echo "canceled"
;;
*)
echo "invalid input"
;;
esac
}
function recharge(){
if [ $TOTAL -le $CONSUME ];then
echo "you money haved $TOTAL availed,it is not enough!!!"
read -p "you want to recharge money?{y|Y|n|N}:" option2
case $option2 in