(2)在文件/tmp/maintenance中添加如下内容:
172.16.0.6
172.16.0.17
172.16.0.20
(3)测试172.16.0.0/16网络内的所有主机是否在线,如果在线就显示其在线,如果此主机
在/tmp/maintenance文件中,就显示其正处于维护状态;否则,就显示其状态未知;
#!/bin/bash
file=/tmp/maintenace
if [ -e $file ]; then
rm -rf $file &> /dev/null
fi
touch $file
cat >> $file << EOF
172.16.0.6
172.16.0.17
172.16.0.20
EOF
bnet=172.16
for net in {0..254} ; do
for host in {1..254} ; do
if ping -c1 -W1 $bnet.$net.$host &> /dev/null ; then
echo "$bnet.$net.$host is up."
elif grep "$bnet.$net.$host$" $file &> /dev/null ;then
echo "$bnet.$net.$host is under maintenance."
else
echo "$bnet.$net.$host state is unknow."
fi
done
done
6写一个脚本,完成以下功能:
(1)、提示用户输入一个用户名;
(2)、显示一个菜单给用户,形如:
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
(3)、提醒用户选择一个选项,并显示其所选择的内容;如果用户给的是一个非上述所提示的选项,则提醒用户给出的选项错误,并请其重新选择后执行;
第一种方法:
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo " Come on ,the user you input unexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your choice:" op
case $op in
U|u)
id -u $username;;
G|g)
id -g $username;;
S|s)
grep "^$username>" /etc/passwd | cut -d: -f7;;
Q|q)
exit 8 ;;
*)
echo "input option wrong ,quit"
exit 9
esac
第二种方法:
#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo "Come on ,you input user notexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your option:" op
while true; do
case $op in
U|u)
id -u $username
break
G|g)
id -g $username
break
S|s)
grep "^$username>" /etc/passwd | cut -d: -f7
break
Q|q)
exit 7 ;;
*)
read -p "Wrong option,Enter a right option:" op ;;
esac
done
7写一个脚本:
(1)、判断一个指定的脚本是否是语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出,其它任何键可以通过vim打开这个指定的脚本;
(2)、如果用户通过vim打开编辑后保存退出时仍然有错误,则重复第1步中的内容;否则,就正常关闭退出。
第一种方法
#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && exit 2
until bash -n $1 &> /dev/null ; do
read -p " Q|q to quit .others to edit:" opt










