Linux Shell经典面试题(其三)
参考:http://blog.csdn.net/hiyun9/article/details/52057255
1.打印选择菜单,一键安装Web服务:
[root@oldboyscripts]# sh menu.sh
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
要求:
1>、当用户输入1时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出”lampis installed”后退出脚本;
2>、当用户输入2时,输出“startinstalling lnmp.”然后执行/server/scripts/lnmp.sh输出”lnmpis installed”后退出脚本;
3>、当输入3时,退出当前菜单及脚本;
4>、当输入任何其它字符,给出提示“Input error”后退出脚本。
5>、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。
############选择执行脚本的脚本编写##############
$ vi menu.sh
#!/bin/bash
echo "1.[install lamp]"
echo "2.[install lnmp]"
echo "3.[exit]"
read -t 30 -p "Please input tut the num you want:" a
case $a in
1)
if
[-f lamp.sh ];then
./lamp.sh
sleep 3
echo "lamp is installed!"
else
echo "no lamp.sh!"
fi
;;
2)
if
[-f lnmp.sh ];then
./lnmp.sh
sleep 3
echo "lnmp is installed!"
else
echo "no lnmp.sh!"
fi
;;
3)
exit 0
;;
*)
echo "Input error"
exit 0
esac
2.用shell处理以下内容
1>、按单词出现频率降序排序!
2>、按字母出现频率降序排序!
the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
$ cat test
the squid project provides a number of resources toassist users design,implement and support squid installations.Please browsethe
documentation and support sections for more infomation
############按单词出现频率降序排序的脚本编写##############
$ vi by-word-num-sort-desc.sh
#!/bin/bash
sed -i 's/\,\|\./ /g' test
for i in `cat test`
do
echo $i
done
# 执行此脚本
$ sh ./by-word-num-sort-desc.sh |sort|uniq -c |sort -r
############按字母出现频率降序排序的脚本编写##############
$ vi by-char-num-sort-desc.sh
#!/bin/bash
sed -i 's/\,\|\./ /g' test
for i in `cat test`
do
for j in `echo $i`
do
echo $j | awk -F '' '{for(i=1;i<=NF;i++)print $i}'
done
done
# 执行此脚本
$ sh ./by-char-num-sort-desc.sh |sort|uniq -c |sort -nr