免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2259 | 回复: 5
打印 上一主题 下一主题

一个关机重启相关的shell脚本有错误,求解释。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-29 17:43 |只看该作者 |倒序浏览
自己写了一个关机重启相关的shell脚本,可以运行总是有错误,偶是初学的,也发现不了错误在哪里,以下是shell源代码,求高手解释。。。。。。。



#!/bin/bash
#Program:
#    For shut down
#History:
#    2012/02/29 ChenYan First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export Path
###########
echo "*******************************************"
echo "**This is a shutdown operating procedures**"
echo "******Please select your of operation******"
echo "***1 Restart          2 shutdown***"
echo "***3 Timing shutdown  4 exit******"
echo "*******************************************"

read -p "Please input options:"sel
clear
read -p "are you sure your choice?(Y/N) "sur
if [ $sur == "N" -o $sur == "n"];then
    exit 1

elif[ $sur == "Y" -o $sur == "y"];then
    exit 0;

    case $sel in
    "1")
      ord='shutdown -r now'
      ;;
    "2")
      ord='shutdown -h now'
      ;;
    "3")
      ord='shutdown -h -t $mm'
      ;;
    "4")
     read -p "Are you sure you want to give up the operation?(Y/N)" sur2
     if [ $sur2 == "Y" -o $sur2 == "y" ];then
         exit 0
     elif [ $sur2 == "N" -o $sur2 == "n"];then
         exit 1
     fi
     ;;
     esac
else
    echo "I don't know what's your menu......"
    exit 1;
fi

论坛徽章:
381
CU十二周年纪念徽章
日期:2014-01-04 22:46:58CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55CU大牛徽章
日期:2013-04-17 11:17:19CU大牛徽章
日期:2013-04-17 11:17:32CU大牛徽章
日期:2013-04-17 11:17:37CU大牛徽章
日期:2013-04-17 11:17:42CU大牛徽章
日期:2013-04-17 11:17:47CU大牛徽章
日期:2013-04-17 11:17:52CU大牛徽章
日期:2013-04-17 11:17:56
2 [报告]
发表于 2012-02-29 22:26 |只看该作者
报错信息是什么

论坛徽章:
0
3 [报告]
发表于 2012-03-01 01:46 |只看该作者
本帖最后由 splendid12 于 2012-03-01 01:49 编辑

#!/bin/bash
#Program:
#    For shut down
#History:
#    2012/02/29 ChenYan First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export Path
###########
echo "*******************************************"
echo "**This is a shutdown operating procedures**"
echo "******Please select your of operation******"
echo "***1 Restart          2 shutdown***"
echo "***3 Timing shutdown  4 exit******"
echo "*******************************************"

clear    #建议(这意味着你可以坚持你的),之前加个 clear 吧,清屏么

read -p "Please input options:" sel    #加空格
clear
read -p "are you sure your choice?(Y/N) " sur    #加空格
if [ $sur == "N" -o $sur == "n" ] ; then    #加空格
    exit 1   

elif [ $sur == "Y" -o $sur == "y" ] ; then    #加空格
    exit 0;    #此处逻辑错误,上面已经 有 exit 了,这里还有,就是说,无论输入 y n ,都会执行 exit x,exit 就是退出了,所以这句 dd 掉

    case $sel in
    "1")
      ord='shutdown -r now'
      ;;
    "2")
      ord='shutdown -h now'
      ;;
    "3")
      ord='shutdown -h -t $mm'
      ;;
    "4")
     read -p "Are you sure you want to give up the operation?(Y/N)" sur2   
     #一个风格的建议(意味着你可以坚持你自己的),这句英文太生硬,可以改为 quit?
     if [ $sur2 == "Y" -o $sur2 == "y" ] ; then    #加空格
         exit 0
     elif [ $sur2 == "N" -o $sur2 == "n" ] ; then    #加空格
         exit 1    #逻辑错误,n 即不想退出,所以应该重新进行选择,这里可以写成这样: . `pwd`/$0     
     fi
     ;;
     esac
else
    echo "I don't know what's your menu......"
    exit 1;
fi

总体说明:
1)错误处在 if [ ] ; then 的空格为问题上;
2)程序输入的 y n 太多了,让人使用得很不舒服(个人感觉);
3)总体挺好的。

论坛徽章:
0
4 [报告]
发表于 2012-03-01 07:02 |只看该作者
:wink::wink:

论坛徽章:
0
5 [报告]
发表于 2012-03-01 09:56 |只看该作者
不懂帮顶

论坛徽章:
0
6 [报告]
发表于 2012-03-01 18:12 |只看该作者
回复 3# splendid12
如您所说,我改成下面的样子了,但不管执行哪个选项都会退出,又变成[root@localhost script]_,请问这是怎么回事啊??你那里测试能用吗?
#!/bin/bash
#Program:
#    For shut down
#History:
#    2012/02/29 Yu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
###########
echo "*******************************************"
echo "**This is a shutdown operating procedures**"
echo "******Please select your of operation******"
echo "***1 Restart          2 shutdown***"
echo "***3 Timing shutdown  4 exit******"
echo "*******************************************"

read -p "Please input options:" sel
clear
read -p "are you sure your choice?(Y/N) " sur
if [ $sur == "N" -o $sur == "n" ]; then
    exit 1

elif [ $sur == "Y" -o $sur == "y" ]; then
    exit 0;
    case $sel in
    "1")
      ord='shutdown -r now'
      ;;
    "2")
      ord='shutdown -h now'
      ;;
    "3")
      ord='shutdown -h -t $mm'
      ;;
    "4")
     read -p "quit?(Y/N)" sur2
     if [ $sur2 == "Y" -o $sur2 == "y" ]; then
         exit 0
     elif [ $sur2 == "N" -o $sur2 == "n"]; then
         . 'pwd'/$0
     fi
     ;;
     esac
else
    echo "I don't know what's your menu......"
    exit 1;
fi


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP