【问题标题】:csh script syntaxcsh 脚本语法
【发布时间】:2013-02-26 05:31:16
【问题描述】:

我是 csh 脚本的新手,这是我第一次编写任何脚本: 下面是代码:

#!/bin/csh

#arg1 path 
#arg2 condition 
#arg3 number of files 
#arg4-argN name of files

set i=0 
while ( $i < $3 ) 
        if ($2 == 0) then 
                cp /remote/$1/$($i+4) $1/new.$( $i+4 ) 
                p4 add $1/new.$($i+4) 
        else 
                p4 edit $1/new.$($i+4) 
                cp /remote/$1/$($i+4) $1/new.$($i+4)
        endif 
        $i = $i+1 
end 

但在这里我一直在出错。非法变量名。 我已经阅读了一些教程,但没有得到任何相关的东西。 请帮忙。 谢谢。

【问题讨论】:

  • 请在“#”之前忽略“/”,我将它们添加为它以该行为粗体
  • 由于您是 shell 脚本的新手,您应该知道有 doubts about the wisdom of writing shell scripts in csh。我想你有一个选择。
  • 谢谢 ruakh 现在看起来好多了:)
  • @miopret 不只是你的! csh 的语法本质上是模棱两可的(更是如此sh!),临床证明过度使用csh 会导致痴呆、牛皮癣和许多其他问题。严肃地说,每个“csh 新手”都需要尽早得到警告,选择csh 是朝着成为更好开发人员的错误方向迈出的一步。
  • 应该是@ i = $i + 1?此外,您可以从${#argv} 计算文件数,而不是根据用户输入的正确数字。

标签: shell csh


【解决方案1】:

您可以在第一行使用标志 -v 和 -x 来查看脚本的作用

#!/bin/csh -vx

问题出现在您尝试将四个添加到计数器变量的部分

$($i+4)

csh 不能这样添加。我会使用一个临时变量将四个添加到您的计数器,然后在所有调用中使用该变量

@ i = 0 
while ( $i < $3 ) 
        @ iplusfour = $i + 4
        if ($2 == 0) then 
                cp /remote/$1/$($i+4) $1/new.$iplusfour 
                p4 add $1/new.$iplusfour 
        else 
                p4 edit $1/new.$iplusfour 
                cp /remote/$1/$iplusfour $1/new.$iplusfour 
        endif 
        @i = $i + 1 
end 

我也采纳了 Willams 的评论。

【讨论】:

    【解决方案2】:

    最后一个增量可以简化为@ i++,即修饰muluman88的解决方案:

    @ i = 0 
    while ( $i < $3 ) 
        @ iplusfour = $i + 4
        if ($2 == 0) then 
            cp /remote/$1/$($i+4) $1/new.$iplusfour 
            p4 add $1/new.$iplusfour 
        else 
            p4 edit $1/new.$iplusfour 
            cp /remote/$1/$iplusfour $1/new.$iplusfour 
        endif 
        @ i++
    end 
    

    确保@ 符号后有(空格)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-05
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多