Austin Lewis
Austin Lewis

Reputation: 1

I am trying to check that user input is 1 of 4 accepted file types in bash script

I am new to bash scripting and writing code in general. I am writing a script that should accept 1 of 4 different file types. There is an if elif condition to check if the file exist and if the file is the correct file type. It makes it through this check, but if I give it the correct file type it thinks I gave it the wrong type and echos my error message.

I have tried running it on all file types and get the same error message every time. When I wrote a similar bit of code but checked for only one file type, I did not have this issue. I believe I am doing something incorrect with || possibly.

# Check if there is a valid file
if [[ ! -f $FILE_INPUT ]]; then
  echo ""
  echo "  FILE ERROR:   "$FILE_INPUT" does not exist!"
  echo ""
  echo ""
  echo "  Script must now terminate..."
  echo ""
  exit $EXIT_ERROR_FILE
elif [[ $FILE_INPUT != *.abc ]] || [[ $FILE_INPUT != *.def ]] || [[ $FILE_INPUT != *.ghi ]] || [[ $FILE_INPUT != *.jkl ]]; then
  echo ""
  echo "FILE ERROR:  "$FILE_INPUT" is not an accepted file type"
  echo ""
  echo ""
  echo "  Script must now terminate..."
  echo ""
  exit $EXIT_ERROR_FILE
fi

Upvotes: 0

Views: 31

Answers (1)

0x5453
0x5453

Reputation: 13589

Your logic is wrong; you want &&, not ||.

e.g. right now, if you pass 123.abc it will still hit the *.def case and flag an error.

Upvotes: 1

Related Questions