3

I am writing a csh shell script, run where the user is expected to pass a path into it as the $1 variable. How can I determine if $1 is missing and define a default value instead for it?

2 Answers 2

3

This is know to work on tcsh version 6.15.00

#! /bin/csh

if( $#argv == 0 ) then
        set filename="(default file)"
else
    set filename=$argv[1]
endif

echo "The file is $filename."

NOTE
There is no standard with csh as there is with the POSIX shell; but at least basic if-then-else and $#argv should be portable.

See Also
POSIX and Korn Shell Versus Other Shells
Csh Programming Considered Harmful
Bash

Sign up to request clarification or add additional context in comments.

1 Comment

can I do this in one line ? because I need to use it in alias
-1

You can do something like:

if [ $# -eq 0 ];then
        dir='default' # argument not passed use default.
else
        dir=$1        # argument passed..use it.
fi

# use $dir 
ls $dir

1 Comment

That's Bourne and family. The question is tagged [csh].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.