0

I have a file of name output.txt, If the file output.txt is not empty then I need to exit the script with exit code 1.

I tried the below code but not getting the desired output:

#!/bin/bash
OUT=`cat output.txt`

if [ "$OUT" -eq 0 ]
then
  echo "The script ran ok"
  exit 0
else
  echo "The script failed"
  exit 1
fi
Rama
  • 175
  • 6
  • You're testing the value of `$OUT`, not the length of it. – Barmar Jul 10 '21 at 09:39
  • @Barmar How o test the length of the file output.txt – Rama Jul 10 '21 at 09:40
  • 1
    No need to `cat` the file to a var to test if it's empty or not: `if [ -s file ] ; then echo exit 1 ; else echo exit 0 ; fi` . `man bash`: _[ -s FILE ] True if FILE exists and has a size greater than zero._ You could also: `[[ ! -s file ]] ; echo $?` – James Brown Jul 10 '21 at 10:12

1 Answers1

0

Use the -z test to test if a string is empty.

if [ -z "$OUT" ]
Barmar
  • 741,623
  • 53
  • 500
  • 612