0

I was able to create the if-elif-then-else code block on a separate script and validated working fine. However when I added the UNTIL or WHILE logic I'm getting the invalid response. I'm NOT expecting to get this response = "invalid operation" if I manually created the VALIDATE.txt file with either "proceed" or "rollback" inside the file. Please check my code below

WHILE SCRIPT

#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
while [ ! -f "$FILE" ]
do
  echo "not exist"
  sleep 60
done

  if [ "${ACTION}" = "proceed" ];
    then
      echo "performing proceed"
      rm -rf $FILE
  elif [ "${ACTION}" = "rollback" ];
    then
      echo "performing rollback"
      rm -rf $FILE
  else
      echo "invalid operation"
      rm -rf $FILE
  fi

UNTIL SCRIPT

#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
   echo "not exist"
   sleep 60
done
if [ "${ACTION}" = "proceed" ];
  then
    echo "performing proceed"
    rm -rf $FILE
elif [ "${ACTION}" = "rollback" ];
  then
    echo "performing rollback"
    rm -rf $FILE
else
    echo "invalid operation"
    rm -rf $FILE
fi

I created the VALIDATE.txt on a separate window and checked myself

lagot-pc:BASH-TEST lagot$ echo proceed >> VALIDATE.txt
lagot-pc:BASH-TEST lagot$ pwd
/Users/lagot/Documents/BASH-TEST
lagot-pc:BASH-TEST lagot$ cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt
proceed

I'm getting the invalid response "invalid operation" instead of "performing proceed" same also if I try to create the VALIDATE.txt and echo the "rollback" inside of the file

lagot-pc:BASH-TEST lagot$ ./while-script.sh
not exist
not exist
not exist
not exist
not exist
invalid operation
Lagot
  • 639
  • 1
  • 9
  • 26
  • 1
    `elif [[ "${ACTION}"= "rollback" ]];` should be `elif [[ "${ACTION}" == "rollback" ]];` `=` means assignment and `==` means compare also need to put space between variable and operator – Digvijay S May 28 '20 at 04:07
  • Check this thread https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash – Digvijay S May 28 '20 at 04:08
  • 3
    @DigvijayS That's incorrect. Within a `[[ ]]` conditional expression, `=` and `==` are equivalent. You're thinking of a `(( ))` arithmetic expression. And BTW, in a `[ ]` test expression, only `=` is standard for comparison (although some shells allow `==` as a synonym). – Gordon Davisson May 28 '20 at 04:37
  • 1
    @GordonDavisson Thank you. Then only issue is no white space between variable and operator – Digvijay S May 28 '20 at 04:39
  • I just modified the bash script according to suggestion from Digvijay S and Gordon Davisson. I'm still getting the same response while testing it – Lagot May 28 '20 at 04:44
  • In the `ACTION= ... >>/dev/null 2>&1` commands, the redirects will mean there's no output to capture and store in `ACTION`, so `ACTION` will never contain any of the strings you're checking for. Also, you read (/try to read) the file *before* waiting for it to exist, which makes no sense; I'm pretty sure you want that *after* the loop. Try putting `set -x` at the beginning of the script, so it prints what it's doing as it runs, and you can see more about what's going on. – Gordon Davisson May 28 '20 at 05:17
  • Thanks Gordon Davisson, I already have the script working. I also added the fixes on the script above – Lagot May 28 '20 at 05:45
  • @GordonDavisson : AFIK, not exactly correct: Inside a `[[ ... ]]`, `=` tests string equality, and `==` interprets the rhs as a glob-pattern. – user1934428 May 28 '20 at 06:35
  • @user1934428 Actually, in `[[ ]]`, both `=` and `==` will treat the RHS as a glob if it's not quoted. Try `[[ foo = *o* ]] && echo "Pattern matched"`. This applies in zsh and ksh93 as well as in bash. – Gordon Davisson May 28 '20 at 07:05
  • Indeed. Thank you for pointing this out. It is indeed also documented well in the man page. – user1934428 May 28 '20 at 07:12

2 Answers2

2

The first part of the original code assigns $ACTION incorrectly, and worse, it wrongly attempts the assignment before making sure $FILE exists. So change these lines:

FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
ACTION=`cat /Users/lagot/Documents/BASH-TEST/VALIDATE.txt >>/dev/null 2>&1`
until [ -f "$FILE" ]
do
   echo "not exist"
   sleep 60
done

To:

FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until [ -f "$FILE" ]
do
   echo "not exist"
   sleep 60
done
ACTION=$(<$FILE)

Or, (since $(<$FILE) returns an error code), with one line less:

FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt
until { ACTION=$(<$FILE); } 2> /dev/null
do
   echo "not exist"
   sleep 60
done

After that, the rest of the original code that tests ${ACTION} should work.


Don't confuse the >> redirect (append to existing file, or create if file doesn't exist) with > (overwrite file, or create if file doesn't exist). So code like this echo proceed >> VALIDATE.txt should be echo proceed > VALIDATE.txt, otherwise if VALIDATE.txt already exists and contains "rollback" the result will be a two line VALIDATE.txt file:

rollback
proceed
agc
  • 7,973
  • 2
  • 29
  • 50
0

This is now fixed, I also added the new format of UNTIL and WHILE scripts below

FIX IN WHILE SCRIPT

#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt

while [ ! -f "$FILE" ]
do
  echo "not exist"
  sleep 60
done

  if [ `cat "$FILE"` = "proceed" ];
    then
      echo "performing proceed"
      rm -rf $FILE
  elif [ `cat "$FILE"` = "rollback" ];
    then
      echo "performing rollback"
      rm -rf $FILE
  else
      echo "invalid operation"
      rm -rf $FILE
  fi

FIX IN UNTIL SCRIPT

#!/bin/bash
FILE=/Users/lagot/Documents/BASH-TEST/VALIDATE.txt

until [ -f "$FILE" ]
do
   echo "not exist"
   sleep 60
done
if [ `cat $FILE` = "proceed" ];
  then
    echo "performing proceed"
    rm -rf $FILE
elif [ `cat $FILE` = "rollback" ];
  then
    echo "performing rollback"
    rm -rf $FILE
else
    echo "invalid operation"
    rm -rf $FILE
fi
Lagot
  • 639
  • 1
  • 9
  • 26