Lagot
Lagot

Reputation: 765

BASH - getting invalid response when using until or while loop

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

Upvotes: 0

Views: 159

Answers (2)

Lagot
Lagot

Reputation: 765

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

Upvotes: 0

agc
agc

Reputation: 8406

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

Upvotes: 2

Related Questions