简体   繁体   中英

Multiple conditions on IF with Shell Script

A coworker of mine made this check for a file processing script which extracts data from our database and put it on some.txt files.

if [ $File_ID < 4 ] || [ $File_ID > 64 ] || [ $File_ID -eq 60 ] && [ $File_ID != 0 ] && [ $File_ID != 1 ]; then

Rules:

  1. If File_ID = 0, it extracts all files.
  2. For 2 and 3 it should fail.
  3. Between 4 and 64 it should pass, with the exception being 60.

Right now, I'm trying to generate the file with File_ID equal to 11, but I'm getting false. What's wrong in this statement?

I would use a case statement instead along with an if statement:

if [ "$File_ID" -lt 0 ] || [ "$File_ID" -gt 64 ]; then
  printf 'File_ID %d out of range\n' "$File_ID"
  exit 1;
fi

case $File_ID in
   0) extract-files ;;
   1) printf "You didn't say what to do with 1\n" >&2; exit 1 ;;
   2|3|60) printf 'Error\n' >&2; exit 1 ;;
   *) printf 'Pass\n' ;;
esac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM