user3295674
user3295674

Reputation: 913

tcsh: Bad : modifier in $ ( )

I would like to pass the last argument given to my tcsh script. I noticed, by testing with echo, that

echo ${*: -1:1}

returns the LAST argument in a series or arguments. Great!

Issue is that when I try to put it in an if statement, or appending to it, I receive the "Bad : modifier in $ ( )" error. Why is this?

if ( -r ${*: -1:1}) then
    echo "You have read permissions"
else

or, same issue with:

if ( ${*: -1:1}:e != tex) then
    exit 2
endif

I tried putting

${*: -1:1} 

in extra brackets, but to no avail. I also tried setting a variable

set last = {*: -1:1}

and then calling $last in the if statement, but that gave me a "Missing }" error.

Thoughts? Thank you.

Upvotes: 0

Views: 1930

Answers (2)

fork2execve
fork2execve

Reputation: 1650

Well, you answered your own question but there is a way in tcsh (and csh) as well:

set count=$#argv
echo "There are $count arguments"
set last="${argv[$#argv]}"
echo "The last argument is $last"

If you want to extract words from lists in csh just make sure you use braces {}.

Test:

$ ./lastarg.csh 'a b' 'c d' 'e  f'
There are 3 arguments
The last argument is e  f

Upvotes: 1

user3295674
user3295674

Reputation: 913

Ended up using

set Y = `echo $* | awk -F " " '{print $NF}'`

instead for my last argument, and this is compatible with tcsh. Solved it for me!

Upvotes: 0

Related Questions