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
  • 150,973
  • 38
  • 327
  • 406
Pham
  • 49
  • 3
  • 1
    what makes you think that `-d` doesn't work as an argument? The fact that `test` or `[` use it doesn't mean it can't be used by anything else. – cas Oct 24 '15 at 13:16
  • 2
    This depends largely on how your script is parsing arguments; can you [edit](http://unix.stackexchange.com/posts/238361/edit) your post to show a concise, repeatable example of the code? – Jeff Schaller Oct 24 '15 at 13:30
  • 1
    [Also posted on SO](http://stackoverflow.com/questions/33318625/csh-using-d-as-argument) – Martin Tournoij Oct 24 '15 at 16:26

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
  • 150,973
  • 38
  • 327
  • 406
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
  • 1
  • 7
  • 119
  • 185
  • 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
  • 234,489
  • 66
  • 447
  • 667
Pham
  • 49
  • 3
  • 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