Reputation:
I am creating a bash script for performing some file/folder comparisons.
I need the user to provide two files/folder paths and some options if needed (any different should close the program).
I would like to know how can I "catch" the number of input argument with the exclusion of options.
Here is some example code (file called foo):
#!/bin/bash
set -e
Help()
{
# Display Help
echo -e "I am the help menu"
}
############################################################
# Main program
# Process the input options.
while getopts ":he" option; do
case $option in
h) # display Help
Help
exit;;
e) # Exclude Files
echo -e "e option was selected";;
\?) # Invalid option
echo -e "Error: Invalid option"
exit;;
esac
done
# Code to filter the input in order to have only two user inputs + options
file1=$1;
file2=$2;
echo -e file1
echo -e file2
So, if the user does: foo -e "someparameter" file1 file2
it should work
if: foo file1
should give an error.
How can I do this?
Best regards
if: foo file1 file2 file3
should also give an error.
Upvotes: 0
Views: 97
Reputation: 23824
It is explained in the manual page.
shift $(($OPTIND - 1)) printf "Remaining arguments are: %s\n$*"
Use $#
to check the number of arguments.
Upvotes: 3