Reputation: 678
I would like to check if
the number of command line arguments passed to a bash script equals to one
the argument points to an existing file
the argument ends with .epub
(in other words, the file has .epub
extension)
I have already dealt with the first and second requirement but I cannot deal with the last. My attemp is as follows:
if [ $# -eq 1 ] && [ -f $1 ] && [ ${"$1": -5} == ".epub" ]; then
Upvotes: 3
Views: 1168
Reputation: 113834
Replace:
${"$1": -5}
With:
${1: -5}
Also, just in case the file name has spaces in it, apply double-quotes:
[ $# -eq 1 ] && [ -f "$1" ] && [ "${1: -5}" == ".epub" ]
Lastly, neither ==
not ${1: -5}
are POSIX. If you want POSIX compatibility, use:
[ $# -eq 1 ] && [ -f "$1" ] && [ "${1%.epub}" != "$1" ]
This will work not just with bash but with many other shells as well.
Upvotes: 1