0

In bash scripting the if condition statement is not working properly with using "&&"

ARGCOUNT=$#

if (( "$ARGCOUNT" != "2" )) ;then
  echo "number of arguments must be two"
fi

DFLAG=$1

HFLAG=$2
if (((( $DFLAG = "Mon" )) || (( $DFLAG = "MON" )) || (( $DFLAG = "mon" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
  echo " CS599 "
  cd CS599
elif (((( $DFLAG = "Wed" )) || (( $DFLAG = "WED" )) || (( $DFLAG = "wed" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
  cd CS699
  echo " CS699 "
elif (((( $DFLAG = "Fri" )) || (( $DFLAG = "FRI" )) || (( $DFLAG = "fri" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
  cd CS799
  echo " CS799 "
else
  echo "."
fi

my program is executing only else statement irrespective of arguments. means it evaluating if block false.

What is the problem ?

1
  • For integer comparison if (( $ARGCOUNT != 2 )) ;then enough. Commented Aug 29, 2014 at 10:10

3 Answers 3

2

The parenthesis you use are for arithmetic evaluation. I think you are over using them, and it makes your script complicated.

This snippet below does work:

#!/bin/bash
ARGCOUNT=$#

if [ "$ARGCOUNT" -ne 2 ] ;then echo "number of arguments must be two"; fi

# put DFLAG in lower case (see man bash).
DFLAG=${1,,}
HFLAG=$2

if [ "$DFLAG" = 'mon' -a "$HFLAG" -ge 2 -a "$HFLAG" -le 4 ]; then
  echo ok
else
  echo failed
fi

As you can see, I optimized your expression:

  • Except for the case of $ARGCOUNT (which is safe because you initialized it to $#), don't forget to encase variable with double quote to avoid expansion.
  • In the declaration of DFLAG, I used the convert to lower case string operator (?). With that you won't have to check for each permutation of case in DFLAG. This might not work in bash3.
  • If you use the test or [ builtin, you can use -a between each expression to do a and.
  • Arithmetic evaluation with the test/[ builtin use the following operators: -ne (inequality), -eq (equality)-ge (greater or equal), -le (lesser or equals), -lt (lesser), -gt (greater).
  • As said in another answer, you can replace "$DFLAG" = 'mon' by "$DFLAG" == 'mon'. But this is not POSIX conformant (as said in my comment below) and I'm not enough knowledgeable on that to know if it's a good idea or not.

On a side note, if $HFLAG condition should always be the same, you can write your code like this:

if [ "$HFLAG" -ge 2 -a "$HFLAG" -le 4 ]; then
  case "$DFLAG" in
    mon|Mon|MON) 
      echo "monday";
    ;;
    fry|Fry|FRY) 
      echo "friday";
    ;;
    *)
      echo "other"
    ;;
  esac
fi

If that case, I putted back all permutation of case in case you were in bash3, to show you an example to do without ${DFLAG,}.

Sign up to request clarification or add additional context in comments.

3 Comments

If you read the man page of bash, you get True if the strings are equal. = should be used with the test command for POSIX conformance.. Since [ is mapped to test, I won't fix that part unless there's is a good reason.
@NoDataFound DFLAG=${1,} only lowercase the first character in bash, if you want to lower-case the whole string you need two comma: DFLAG=${1,,}. Otherwise nice answer!
I took that into account, thanks :) And now, I remember that it does not work in Bash 3 otherwise I would already used it :o
0

If you are looking for what mistake you did which is making condition to go to else part ...then it's simple mistake which almost every programmer do once in life ... using single "=" instead of "==" during comparison. Modify it accordingly and you should get expected flow/result in your script.

One eg.

$DFLAG = "Mon"

change to below notice the double equal sign 

"$DFLAG" == "Mon"

Comments

0

First, you should go easy on the parenthesis. Those are fragile things.

Using Bash syntax (non-POSIX, less portable), you can write:

ARGCOUNT=$#
DFLAG=${1,,} # lower case
HFLAG=$2

 # don't need the quotes in (( )) as we test arithmetic value
(( $ARGCOUNT != 2 )) && echo "number of arguments must be two"

shopt -s extglob # for @(1|2|3) below, see http://mywiki.wooledge.org/glob#extglob
if [[ $DFLAG = "mon" && $HFLAG == @(2|3|4) ]]; then
  echo " CS599 "
  cd CS599
if [[ $DFLAG = "wed" && $HFLAG == @(2|3|4) ]]; then
  cd CS699
  echo " CS699 "
if [[ $DFLAG = "fri" && $HFLAG == @(2|3|4) ]]; then
  cd CS799
  echo " CS799 "
else
  echo "."
fi

Now you see how much you repeat yourself and can improve the algorithm. For instance, test HFLAG, if valid test DFLAG otherwise …

Read

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.