bashunixcshtcsh

Error converting a bash function to a csh alias


I am writing a csh alias so that I can use the following bash function in my csh :

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

(I stole the above bash function from here)

I have written this:

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'

However, I am getting the following error:

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

and my script is not working as intended. I have been reading up on csh from here.

I am not an expert in csh and what I have written above is my first csh script. Please let me know what I am doing wrong.


Solution

  • You can also do this

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

    yes ".." will repeat the string .. indefinitely; head will truncate it to the number passed as argument while calling the alias ( !* expands to the arguments passed; similar to $@ ) and tr will convert the newlines to /.

    radical7's answer seems to be more neat; but will only work for tcsh ( exactly wat you wanted ). This should work irrespective of the shell