0

This is a C shell script that checks the number of arguments and if the file/directory exists. When the arguments are valid, it should go to the last else statement and print "hi", but when I run this script with the correct arguments it doesn't print anything. (but the exit status is 0)
Everything else works fine when I have invalid arguments.

if ($#argv != 2) then
        echo "Two arguments required"
else
        if (! -f $argv[1]) then
                echo "File does not exist"
                exit 1
        else
                if (! -d $argv[2]) then
                        echo "Not a valid directory"
                        exit 2
                endif
        endif
else    
        echo "hi"
endif

Maybe it's my syntax that is wrong? Any ideas?

  • You have two consecutive else statements, how would you expect this to work? – terdon Mar 31 '14 at 17:52
  • Yes, your syntax is wrong... if .. else .. *else* .. endif? the shell shouldn't even accept that. – Ricky Beam Mar 31 '14 at 17:52
  • Well, your first mistake was writing #!/bin/csh. It's all downhill from there. – cjm Mar 31 '14 at 19:16
1

This else case can't be reached, since you already have an else case for this if. What you want to accomplish could e.g. be done the following way:

if ($#argv != 2) then
    echo "Two arguments required"
    exit 1
else if (! -f $argv[1]) then
    echo "File does not exist"
    exit 1
else if (! -d $argv[2]) then
    echo "Not a valid directory"
    exit 2
endif

echo "hi"
  • 1
    Please expand. As it is, you are simply stating the obvious which has already been mentioned twice in the comments. If you want to make an answer out of it, please show a working script that can help the OP. – terdon Mar 31 '14 at 18:00
  • Sorry, I was in a hurry and didn't look at the comments. :/ – Andreas Wiese Mar 31 '14 at 18:10
  • Much better, thanks! And posting an answer from info in the comments is fine, comments are fair game. Just if you do so, expand on it :) – terdon Mar 31 '14 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.