通过tcsh执行IF语句时遇到问题。这对我来说很好-
#!/bin/bash
if echo `cal|tail -6|sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' |tr -s '[:blank:]' '\n' | head -11|tail -10|tr -s '\n' ' '`|grep -w `date "+%e"`
then
echo "present"
else
echo "absent"
fi这就是问题所在
#!/bin/tcsh
if echo `cal|tail -6|sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' |tr -s '[:blank:]' '\n' | head -11|tail -10|tr -s '\n' ' '`|grep -w `date "+%e"`
then
echo "present"
else
echo "absent"
endif得到这个错误-
if: Expression Syntax.
then: Command not found.我真的需要使用"tcsh“来运行它。
发布于 2012-11-14 18:49:53
首先,您要知道您可以找到两个不同的shell系列,如下所示:
shell类型shell (Bash、zsh...)
如您所见,Bash和tcsh不是来自同一个shell家族。因此,在tcsh上,if语句与bash语句略有不同。在您的例子中,关键字"then“放错了地方。尝试将其放在"if“行的末尾,如下所示:
#!/bin/tcsh
if(echo `cal|tail -6|sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' \
|tr -s '[:blank:]' '\n' | head -11|tail -10|tr -s '\n' ' '`| \
grep -w `date "+%e"`) then
echo "present"
else
echo "absent"
endif希望能有所帮助。
发布于 2012-12-18 04:43:45
这在bash中是有效的,因为POSIX样式的shell中的if语句总是通过执行命令来工作的(而[恰好是test命令的别名)。
但是,tcsh中的if语句不是这样工作的。它们有自己的语法(在tcsh man page中的表达式中描述)。
尝试单独运行管道,然后在if中检查退出状态
cal | tail -6 | sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' | tr -s '[:blank:]' '\n' | head -11 | tail -10 | tr -s '\n' ' ' | grep -w `date "+%e"` >/dev/null
if ( $? == 0 ) then
echo "present"
else
echo "absent"
endif发布于 2017-12-19 21:43:57
我通常会这样做,保持条件语句简单。但是,您可以将变量塞进“if”中,然后只检查grep是否为空。
set present = `tail -6 .... | grep “”`
if ( $present != “” ) then
echo “present”
else
echo “not present”
endif 您也可以使用“-x”来帮助调试#!/bin/tcsh -x。这么小的东西,一个用来检查你的变量的回声就可以了,但是“-x”可能会给你需要的所有信息。
https://stackoverflow.com/questions/13377152
复制相似问题