免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4640 | 回复: 8

shell中if语句不能执行。。。 [复制链接]

论坛徽章:
0
发表于 2011-09-19 23:04 |显示全部楼层
本帖最后由 luoyan_xy 于 2011-09-20 09:44 编辑

写了一个这样的脚本,本来就是想写个无限循环,可写了几次发现不能运行,不知道原因,还请各位帮忙看下,谢谢。

       刚刚开始接触shell脚本,如果问题太简单,还请谅解。
  1. #!/bin/bash
  2. while ( 1 )
  3. do
  4. dmesg -c >> log.tmp
  5. sleep 5
  6. done
复制代码
  1. #!/bin/bash
  2. while ( 1 < 2 )
  3. do
  4. dmegs -c >> log.tmp
  5. sleep 5
  6. done
复制代码
刚开始这样写,不能运行,后来又改成这样
  1. #!/bin/bash
  2. i=1
  3. while ( $i < 2 )
  4. do
  5. dmesg -c >> log.tmp
  6. sleep 5
  7. done
复制代码
可是不能运行,可是这样的语句如果放在终端里执行是能够执行的...
最后改成这样才能运行:
  1. #!/bin/bash
  2. i=1
  3. j=2
  4. while [ $i -lt $j ]
  5. do
  6. dmesg -c >> log.tmp
  7. sleep 5
  8. done
复制代码
困惑的地方比较多,主要是(和 [ 的区别。。。

论坛徽章:
0
发表于 2011-09-19 23:07 |显示全部楼层
if下面 木有接do的吧。。。。。。。。。。

论坛徽章:
1
巳蛇
日期:2013-10-28 15:55:33
发表于 2011-09-20 07:48 |显示全部楼层
while :
do
...
done
无限循环

论坛徽章:
1
巳蛇
日期:2013-10-28 15:55:33
发表于 2011-09-20 07:49 |显示全部楼层
要C风格语法需要加(( )), 而非( )

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
发表于 2011-09-20 08:19 |显示全部楼层

  1.        if list; then list; [ elif list; then list; ] ... [ else list; ] fi
  2.               The if list is executed.  If its exit status is zero, the  then
  3.               list  is  executed.   Otherwise,  each elif list is executed in
  4.               turn, and if its exit status is zero,  the  corresponding  then
  5.               list  is  executed  and  the command completes.  Otherwise, the
  6.               else list is executed, if present.  The exit status is the exit
  7.               status  of  the  last command executed, or zero if no condition
  8.               tested true.

复制代码
你用的是bash语法么?
( 1 )
有1这个命令么?它的退出状态是0么?

论坛徽章:
15
CU大牛徽章
日期:2013-03-13 15:32:35白羊座
日期:2013-10-30 13:11:16未羊
日期:2013-11-29 10:47:02白羊座
日期:2014-01-22 11:34:25白羊座
日期:2014-03-04 11:05:50申猴
日期:2014-07-29 16:31:36天秤座
日期:2013-10-23 15:33:47未羊
日期:2013-10-09 09:18:53CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55未羊
日期:2013-09-23 09:29:22
发表于 2011-09-20 09:45 |显示全部楼层
if [ condition ]
then
         :
else
         :
fi

论坛徽章:
0
发表于 2011-09-20 09:50 |显示全部楼层
回复 4# 惟吾无为


    谢啦,按你说的能搞定了

    一直没对shell语法比较深入的学习,想当然的写出了上面的例子。。。

论坛徽章:
0
发表于 2011-09-20 11:56 |显示全部楼层
楼主去看看ABS吧

论坛徽章:
0
发表于 2011-09-23 12:16 |显示全部楼层
基础哦
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP