0

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

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

(我从这里偷了上面的 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.

而且我的脚本没有按预期工作。我从这里开始阅读 csh 。

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

4

2 回答 2

2

你也可以这样做

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

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

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

于 2013-04-16T08:08:34.293 回答
1

你可以使用csh的repeat功能

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

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

于 2013-01-25T14:10:39.993 回答