免费注册 查看新帖 |

Chinaunix

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

[其他] [csh]if-then中间的表达式不用括号吗 [复制链接]

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-04-15 06:20:0015-16赛季CBA联赛之福建
日期:2016-08-08 13:37:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-28 13:09 |只看该作者 |倒序浏览
下面语句能正常工作,可是不理解为什么不报语法错。

  1. #!/bin/csh
  2. if (`ll a.txt | awk '{print $5}'`)== 0 then
  3.     echo empty file
  4. else
  5.     echo no empty
  6. endif
复制代码



手册上是if (expr) then,
所以感觉下面这样写才对。
  1. if (`ll a.txt | awk '{print $5}'` == 0) then
复制代码

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
2 [报告]
发表于 2014-07-28 14:16 |只看该作者
没玩过cshell,有if (xxoo)这样的写法???
bash里都是
if [xxoo]或者if [[ xxoo ]]

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
3 [报告]
发表于 2014-07-28 14:19 |只看该作者
楼上正解
man csh看下条件测试的部分。

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
4 [报告]
发表于 2014-07-28 14:58 |只看该作者
回复 3# expert1
man csh 的部分 输出.
  1.        if (expr) then
  2.        ...
  3.        else if (expr2) then
  4.        ...
  5.        else
  6.        ...
  7.        endif   If  the  specified  expr is true then the commands to the first
  8.                else are executed; otherwise if expr2 is true then the commands
  9.                to  the  second  else are executed, etc.  Any number of else-if
  10.                pairs are possible; only one endif is needed.  The else part is
  11.                likewise  optional.   (The  words else and endif must appear at
  12.                the beginning of input lines; the if must appear alone  on  its
  13.                input line or after an else.)
复制代码

论坛徽章:
24
申猴
日期:2014-10-10 15:56:39射手座
日期:2014-10-10 15:57:18黑曼巴
日期:2018-05-14 11:05:122016科比退役纪念章
日期:2018-05-14 11:05:0715-16赛季CBA联赛之北控
日期:2018-05-14 11:05:0015-16赛季CBA联赛之江苏
日期:2017-02-27 18:11:0715-16赛季CBA联赛之上海
日期:2018-08-15 09:48:5415-16赛季CBA联赛之佛山
日期:2018-07-20 17:14:2315-16赛季CBA联赛之佛山
日期:2019-09-10 18:08:4615-16赛季CBA联赛之山西
日期:2020-03-26 09:40:5115-16赛季CBA联赛之佛山
日期:2020-05-08 09:03:54
5 [报告]
发表于 2014-07-28 17:28 |只看该作者
本帖最后由 chengchow 于 2014-07-28 17:31 编辑

bash有个特性,命令正常返回0,否则为其他,大多情况下是1
你这个判断实际是将()内的输出结果作为一个完整的命令进行判断
假如你()内值是0,实际执行的是if 0==0  then
否则执行的是,if ?==0 then
linux没?==这种命令,所以你判断的条件的结果肯定是false
只能执行echo no empty
csh没玩过,这是在shell环境下执行结果

举个例子吧
kvm bin # if ls;then echo 1;else echo 2;fi
aa.sh
1
kvm bin # if `ls`;then echo 1;else echo 2;fi
-bash: /usr/local/bin/aa.sh: Permission denied
2

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-04-15 06:20:0015-16赛季CBA联赛之福建
日期:2016-08-08 13:37:03
6 [报告]
发表于 2014-07-29 13:20 |只看该作者
看来大家还是用bash的多啊。

补充一下,我用的是tcsh。
莫非tcsh语法比csh宽松了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP