2

I know that -d /path/to/dir can be used to test command for checking a directory. Then:

if ( $argv[1] == "-d" )

does not work because of that.

However, I need to be able to pass -d as an argument to my script. How can I disable the special property of -d so I can pass it as an argument?

cuonglm
  • 153,898
Pham
  • 49

3 Answers3

4

Just using double quote "$var" or :q operator $var:q.


In csh variants, when variable is substituted without double quote, its result will be expanded as command or file name substituted (That's also true in POSIX shell, forgetting the double quote make your script chocked and lead to many security implications). So:

if ( "$argv[1]" == "-d" ) echo 1

will work. Even better to use :q operator to work with newline:

if ( $argv[1]:q == "-d" ) echo 1
cuonglm
  • 153,898
0

-d doesn't have any special properties, so there's nothing to disable.

As cuonglm mentioned the error you are seeing is due to improper quoting of the $argv[1] variable, not because of some imaginary special property of -d. Here's proof:

$ csh 
% if ( "-d" == "-d" ) echo true
true
% if ( -d == "-d" ) echo true
if: Missing file name.
cas
  • 78,579
  • Using ./script -d to launch the script prints "If: missing file name" because it wants to test for a directory. – Pham Oct 24 '15 at 13:19
  • 1
    That would be due to a coding error in your script, not because of some imaginary special property of -d. cuonglm explains what you did wrong - you didn't use double-quotes around the variable. – cas Oct 24 '15 at 21:13
0

Solved it by adding a dummy character before the statement. Like so:

if ( X$argv[1] == "X-d" ).
terdon
  • 242,166
Pham
  • 49
  • Using X or any other letter to avoid properly quoting your variables is extremely bad practice. It is a bug waiting to happen. – cas Oct 25 '15 at 04:09