简体   繁体   中英

Bash script: get number of input parameters excluding options

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.

It is explained in the manual page .

 shift $(($OPTIND - 1)) printf "Remaining arguments are: %s\\n$*"

Use $# to check the number of arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM