I have a bash script which processes an input file with optional arguments. The script looks like this
#!/bin/bash while getopts a:b:i: option do case "${option}" in a) arg1=${OPTARG};; b) arg2=${OPTARG};; i) file=${OPTARG};; esac done [ -z "$file" ] && { echo "No input file specified" ; exit; } carry out some stuff
The script runs fine, but I need to specify the input file like so
sh script.sh -a arg1 -b arg2 -i filename
I would prefer to be able to call the script without the -i
option, like so
sh script.sh -a arg1 -b arg2 filename
while still having the error message when no input file is specified. Is there a way to do this?
script.sh -i file1 -i file2 file3 file4
?sh script.sh -a arg1
orsh script.sh -a arg1 -b arg2
.