How to use OR condition inside tcsh if statement

18,434

There is some confusions:

  1. Your first script is written in bourne-shell
  2. Your script did not mention what is the shell should be used to interpret it (that may lead to some confusion depending on the way you execute it)
  3. You second script is a tcsh script but with misleading syntax
  4. It looks like you don't know what [ is.

Modify the first script like this:

#!/bin/sh
if [ "$1" == "hi"  -o   "$2" == "hello" ]; then
  echo hi
else
  echo hello
fi

This means that if executable this script will use /bin/sh to interpret it. But, if you force the shell with commands:

% sh -x test hi hi
+ '[' hi == hi -o hi == hello ']'
+ echo hi
hi
% tcsh -x test hi hi
if [ hi = hi -o hi = hello ]
if: Expression Syntax.
then
then: Command not found.
%

you can observe that using the wrong shell leads to some syntax errors.

If you want to write a tcsh script, this is a solution:

#!/bin/tcsh
if ( "$1" == "hi"  ||  "$2" == "hello" ) then
  echo hi
else
  echo hello
endif

Syntax for if is different because tcsh has internal testing features, which standard bourne shell doesn't have. In bourne shell testing is made using the external command test which has an alias [. So you may read the documentation about testsyntax in the manual:

SYNOPSIS
     test expression
     [ expression ] ...
     s1 = s2       True if the strings s1 and s2 are identical. ...
     expression1 -o expression2
                   True if either expression1 or expression2 are true.

For tcsh's if, read the manual for tcsh (or csh its parent):

   if (expr) command
           If  expr (an expression, as described under Expressions) evalu-
           ates true, then command is executed.  Variable substitution  on
           command happens early, at the same time it does for the rest of
           the if command.  command must  be  a  simple  command,  not  an
           alias,  a  pipeline,  a command list or a parenthesized command
           list, but it  may  have  arguments.   Input/output  redirection
           occurs  even if expr is false and command is thus not executed;
           this is a bug.

   if (expr) then
   ...
   else if (expr2) then
   ...
   else
   ...
   endif   If the specified expr is true then the commands  to  the  first
           else are executed; otherwise if expr2 is true then the commands
           to the second else are executed, etc.  Any  number  of  else-if
           pairs are possible; only one endif is needed.  The else part is
           likewise optional.  (The words else and endif  must  appear  at
           the  beginning  of input lines; the if must appear alone on its
           input line or after an else.)

  Logical, arithmetical and comparison operators
       These operators are similar to those of C and have the same precedence.
       They include

           ||  &&  |  ^  &  ==  !=  =~  !~  <=  >=
           <  > <<  >>  +  -  *  /  %  !  ~  (  )
...
Share:
18,434

Related videos on Youtube

monk
Author by

monk

Updated on September 22, 2022

Comments

  • monk
    monk over 1 year

    I am trying to use an OR condition inside an IF statement in tcsh shell. Same statement works in CSH.

    Example statement: (cat tesh.sh)

    if [ "$1" == "hi" -o "$2" == "hello" ];then
    echo hi
    else
    echo hello
    fi
    

    Now if I execute this sample script I get the following errors:

    [35] % sh -x test hi hi hello
    + test hi hi hello
    test[7]: hi: A test command parameter is not valid.
    + exit 1
    [36] % sh -x test hi hi
    + test hi hi
    test[7]: test: Specify a parameter with this command.
    + exit 1
    [37] % sh -x test hi hello
    + test hi hello
    test[7]: test: Specify a parameter with this command.
    + exit 1
    [38] % sh -x test hi hello
    + test hi hello
    test[7]: test: Specify a parameter with this command.
    + exit 1
    [39] %
    

    Please suggest what can be done?

    Additional Info:

    [44] % uname -s
    HP-UX
    [45] %
    [45] % echo $SHELL
    /bin/tcsh
    [46] %
    

    More example:

     cat new_test.txt
    if ([ $1 == 1 ] || [ $2 == 1 ])
    then
    echo $1 and $2
    fi
    
    ./new_test.txt 1 1
    ./new_test.txt: ==: A test command parameter is not valid.
    ./new_test.txt: ==: A test command parameter is not valid.
    

    More Example:

     cat suggested.sh
    
    if (($1 == 1) || ($2 == 1)) ; then echo "$1 and $2" ; fi
    
     ./suggested.sh 1 1
    ./suggested.sh: 1:  not found.
    ./suggested.sh: 1:  not found.