csh/C shell scripts: if statement syntax

Contributor Icon Contributed by Rex  
Tag Icon Tagged: csh shell scripting  

Conditional expressions give programs life. The ability to branch makes shell scripts powerful. This recipe shows the basic if then else structure for csh, the C shell.


Basic if statement syntax:

if (condition) then
commands
endif

The addition of one or more else keywords offers additional flexibility:

if (condition) then
commands
else if (other condition) then
commands
else
commands
endif

 

6 Comments -


  1. Dan said on November 7, 2008

    test for the existence of a variable using
    if ($?LD_LIBRARY_PATH == 0) then
    setenv LD_LIBRARY_PATH /usr/lib;
    endif

  2. Dan said on March 11, 2009

    Without concrete examples of how the syntax is actually used, this is almost useless information.

  3. Dan said on March 11, 2009

    A variation of the above would be:

    if ( ! ${?LD_LIBRARY_PATH} ) then
    setenv LD_LIBRARY_PATH /usr/lib
    endif

    Use the {} around variables as a habit since the variable names can often interfere with shell syntax.

    The semicolon at the end is ignored.

  4. Paul said on December 14, 2009

    Almost? More like completely…

  5. Chisimobi said on March 23, 2011

    need more examples

  6. Chisim_obi said on March 23, 2011

    need more examples

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -