您的位置:首页 > 其它

day03脚本练习题

2016-06-17 18:05 267 查看
1、用while循环输出数字1到10(降序输出)
用while循环输出数字35到50(升序输出)
#!/bin/bash
j=10
i=35
while [ $j -ge 1 ]
do
echo$j
let j--
done
while [ 50 -ge $i ]
do
echo$i
let i++
done

2、批量添加15个系统用户要求如下
用户名的密码与用户名相同
用户名的前辍是user,用户名格式如下
user01 user02 ... user09 user10 ... user15
强制新添加的用户首次登陆时,重置自已的登陆系统的密码

#!/bin/basha
i=useradd
u=user0
j=1
o=user
k=10
while [ 9 -ge $j ]
do
$i $u$j
echo "$u$j" | passwd --stdin $u$j &> /dev/null
chage -d 0 $u$j
let j++
done
while [ 15 -ge $k ]
do
$i $o$k
echo "$o$k" | passwd --stdin $o$k &> /dev/null
chage -d 0 $o$k
let k++
done

3、编写脚本userdel.sh(删除刚才批量添加的15个系统用户)
#!/bin/basha
u=user0
j=1
o=user
k=10
while [ 9 -ge $j ]
do
userdel -r $u$j
let j++
done
while [ 15 -ge $k ]
do
userdel -r $o$k
let k++
done

4、useradd.sh(交互式批量添加系统用户)
功能如下:
1、可以设置添加的用户数量、用户的密码、用户的家目录、用户的有效期
用户前辍

#!/bin/bash
read -p "请输入你要添加的用户数量:" yh
if [ -z $yh ];then
echo "数量不能为空"
exit
fi
i=1
while [ $yh -ge $i ]
do
read -p "请输入用户家目录存放的位置如:/home/xxx:" mulu
read -p "请输入你的用户名:" name
useradd -d $mulu $name
passwd $name
read -p "请输入你的用户的有效期如:xxxx-xx-xx:" yxq
chage -E $yxq $name
let i++
done


#!/bin/bash
read -p "请输入用户的个数:" "usernum"
read -p "请输入用户名:" username
read -p "请输入密码:" userpas
if [ -z $usernum ] || [ -z $username ] || [ -z $useraps ];then
echo “输入不完整,自动退出”
else
i=1
while [ $i -le $usernum]
do
useradd $username$i
echo $userpas | passwd --stdin $username &> /dev/null
let i++
done
fi

5、用(C-for)批量添加15个系统用户要求如下
用户名的密码与用户名相同
用户名的前辍是user,用户名格式如下
user01 user02 ... user09 user10 ... user15
强制新添加的用户首次登陆时,重置自已的登陆系统的密码
#!/bin/bash

for((i=1;i<=15;i++))
do
if[ $i -lt 10 ];then
useradd user0$i
echo "user-$i" | passwd --stdin user0$i
chage -d 0 user0$i
else
useradd user$i
echo "user$i" | passwd --stdin user$i
chage -d 0 user$i
fi
done

6、用C-for编写脚本(删除上面批量添加的15个用户)
#!/bin/bash
for((i=1;i<=9;i+=))
do
userdel -r user0$i
done
for((i=10;i<=15;i++))
do
userdel -r user$i
done

7、用until循环结构输出值1-5
#!/bin/bash
i=1
until [ $i -gt 5 ]
do
echo $i
let i++
done

8、four.sh(在一个脚本里面分别用四种循环结构输出数字8到12)
#!/bin/bash

#for循环
for i in `seq 8 12`
do
echo $i
done

#while循环
i=8
while [ $i -le 12 ]
do
echo $i
let i++
done

#C-for循环
for((i=8;i<=12;i++))
do
echo $i
done

#until循环
i=8
until [ $i -gt 12 ]
do
echo $i
let i++
done

9、check_gatewy.sh
(当前服务器使用的网关地址不能用时,向终端发送警告信息)
using gateway error
echo "using gateway error" > /dev/tty1
echo "using gateway error" > /dev/pts/1
write

#!/bin/bash

wg=`route -n | tail -1 | awk '{print $2}'`

ping -c 3 $wg &> /dev/null

if [ $? -eq 0 ];then
echo "网关正常"
else
echo “using gateway error” > /dev/tty1
echo "using gateway error" > /dev/pts/1
fi

1.100 route 网通
10、auto_gw.sh,自动切换当前主机的网关
192.168.1.0/24------iptables---
1.251 1.254
route 电信

#!/bin/bash

gw=`route -n | tail -1 | awk '{print $2}'`

while :
do
ping -c 3 $gw &> /dev/null

if [ $? -ne 0 ];then
route add default gw 192.168.1.254 &> /dev/null
route del default gw 192.168.1.100 &> /dev/null
else
route add default gw 192.168.1.100 &> /dev/null
route del default gw 192.168.1.254 &> /dev/null
fi
done

11、如果系统的是 星期一 到 星期五 就输出工作日
如果系统的是 星期六 就输出 约会日
如果系统的是 星期日 就输出 休息日

#!/bin/bash
read -p "请输放你选择的星期 星期一 | 星期二 | 星期三 | 星期四 | 星期五 | 星期六 | 星期日 " xq
case $xq in
星期一)
echo "今天是工作日";;
星期二)
echo "今天是工作日";;
星期三)
echo "今天是工作日";;
星期四)
echo "今天是工作日";;
星期五)
echo "今天是工作日";;
星期六)
echo "今天是约会日";;
星期日)
echo "今天是休息日";;
*)
echo "$0只能选择这里的其中一个 星期一 | 星期二 | 星期三 | 星期四 | 星期五 | 星期六 | 星期日"
esac

12、very_num_dj.sh (输出任意数相乘的积)
#!/bin/bash
i=1
while [ $# -gt 0 ]
do
i=`expr $i \* $1`
shift
done
echo $i

13、very_num_shang.sh(输出任意数相除的商)

#!/bin/bash
i=`expr $1 \* $1`
while [ $# -gt 0 ]
do
i=`expr $i / $1`
shift
done
echo $i

14、very_num_shang.sh(输出任意数相减的差)

#!/bin/bash
i=`expr 2 \* $1`
while [ $# -gt 0 ]
do
i=`expr $i - $1`
shift
done
echo $i

15、very_num_shang.sh(输出任意数相加的和)
#!/bin/bash
i=0
while [ $# -gt 0 ]
do
i=`expr $i + $1`
shift
done
echo $i



#!bin/bash
one=$1
shift
while [ $# -gt 0 ]
do
let one+=$1
shift
done
echo $one
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  脚本 day 练习题