在linux 系统上一般使用root执输入命令后回车后都是直接执行不提示的是否执行的,如果一些敏感的命令,如rm -rf / ; reboot; halt ;kill -9等等,不小心输入而执行的话后果不堪设想。下面以一个实例,使命令能实现交互提示输入yes/no等来确认是否执行。
以下是一个用于杀掉tomcat进程的脚本,通过执行后输入"yes/no"来选择执行与否
[root@web abc]# cat kill_zombi_tomcat
while true
do
echo -n "You really want to kill tomcat process as below? (yes/no):"
echo -e "\n`ps -ef | grep ^tomcat | cut -c 1-15`"
read -p "please enter your chose :" crm
if [ "$crm"x = "yes"x ]; then
echo "Execute killing progresses"
ps -ef | grep ^tomcat | cut -c 9-15|xargs kill
else
break
fi
break
done
[root@web abc]#
[root@web abc]#./kill_zombi_tomcat
You really want to kill tomcat process as below? (yes/no):
tomcat 9930
tomcat 9959
ples enter your chose :no