代码之家  ›  专栏  ›  技术社区  ›  Aakash Goel

这个C shell脚本有什么问题?

  •  2
  • Aakash Goel  · 技术社区  · 14 年前

    我正在尝试为bash脚本编写一个与C shell等价的脚本 mentioned here .

    这就是我所拥有的:

    #! /bin/tcsh
    
    set now=`date +%Y%m%d%H%M.%S`
    if (( ! -f "./cache" ) || (-n  "`find ./monme -newer ./cache`" ))
    then
        touch cache -t "$now"
        echo "new files added" | mail -s "new build" myemail@myserver.com
    endif
    

    $ ./scr
    if: Badly formed number.
    $
    

    This page

    set now=`date +%Y%m%d%H%M`
    

    但我还是犯了同样的错误。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Aakash Goel    14 年前

    我把你的剧本删节成这样:

    #! /bin/tcsh
    
    if ( -n  "`find ./monme -newer ./cache`" ) then
        echo hello
    endif
    

    这就产生了同样的错误。我想罪魁祸首是

    -n  "`find ./monme -newer ./cache`"
    

    是什么 -n 该怎么办?我想它想要一个数字,但得到了别的。。。

    bash中的-n表示“字符串长度不为零”。在我的tcsh版本中,替换它就像使用==''一样简单,如下所示:

    if (( ! -f "./cache" ) || ("`find ./monme -newer ./cache`" != ""))
    then
        touch cache -t "$now"
        echo "new files added" | mail -s "new build" myemail@myserver.com
    endif
    

    试试看是否有效。