【问题标题】:Error converting a bash function to a csh alias将 bash 函数转换为 csh 别名时出错
【发布时间】:2013-01-21 15:02:52
【问题描述】:

我正在编写一个 csh 别名,以便可以在我的 csh 中使用以下 bash 函数:

function up( )
{
    LIMIT=$1
    P=$PWD
    for ((i=1; i <= LIMIT; i++))
    do
        P=$P/..
    done
    cd $P
    export MPWD=$P
}

(我从here偷了上面的bash函数)

我写了这个:

alias up 'set LIMIT=$1; set P=$PWD; set counter = LIMIT;  while[counter!=0] set counter = counter-1; P=$P/.. ; end cd $P; setenv MPWD=$P'

但是,我收到以下错误:

while[counter!=0]: No match.
P=/net/devstorage/home/rghosh/..: Command not found.
end: Too many arguments.

我的脚本没有按预期工作。我一直在阅读来自 here 的 csh。

我不是 csh 专家,我上面写的是我的第一个 csh 脚本。请让我知道我做错了什么。

【问题讨论】:

  • csh 使用setenv 而不是export
  • @WilliamPursell 我将导出更改为 setenv,但我仍然遇到同样的错误
  • @WilliamPursell 现在的问题有很大不同。你能再看看它吗?
  • 在我看来,正确的解决方案是:“不要使用 csh”!
  • 算术需要'at-signment' ("@");除此之外,我同意威廉普尔塞尔的观点。或者,如果你真的想使用 write csh 别名,你最好非常非常仔细地阅读它的文档

标签: bash unix csh tcsh


【解决方案1】:

你也可以这样做

alias up 'cd `yes ".." | head -n\!* | tr "\n" "\/"`'

yes ".." 将无限期地重复字符串..head 将在调用别名时将其截断为作为参数传递的数字(!* 扩展为传递的参数;类似于 $@),tr 会将换行符转换为 /

radical7 的回答似乎更简洁;但仅适用于 tcsh (正是您想要的)。无论外壳如何,这都应该工作

【讨论】:

    【解决方案2】:

    可以使用csh的repeat函数

    alias up 'cd `pwd``repeat \!^ echo -n /..`'
    

    不需要循环(这很方便,因为 tcsh 中的 while 构造看起来很挑剔)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 2020-03-07
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      相关资源
      最近更新 更多