mercredi 7 juin 2017

Breaking bash script execution if passed wile is of wrong extension

I have a small bash file that I used to compress PDFs and undertake some other file manipulations. I would like to break execution of this bash script if the passed file name does not have .pdf or .PDF extension.

Current approach:

# Take file name
fle_nme=$1

# Check if passed PDF file
# -ne - not equal
if ["${fle_nme: -4}" -ne ".pdf"] ||  ["${fle_nme: -4}" -ne ".PDF"]; then
        echo "Incorrect file."
        exit
fi

Current output

The script fails but without the desired message:

[me@onthebox bashStuff ERR]$ compressPDF ~/bashFun/failWithThisExtension.bad
compressPDF.sh: line 16: [.bad: command not found
compressPDF.sh: line 16: [.bad: command not found

Desired results

  • Execution of the script should finish
  • Message Incorrect file should be printed to console

Aucun commentaire:

Enregistrer un commentaire