yatici
yatici

Reputation: 577

define variable inputs with - parameter for unix

My question is pretty straight forward but it seems like I can't seem to be able to find an answer with google :(

How do I specify parameters for a function with - parameter for a function I wrote.

For example I wrote this .sh file for bash but it will take 5-6 inputs so it is likely that a user might mess up the order. so how can I make the function run like:

function -m [email protected] -d dataset.csv -f filter ...

instead of having to write all the parameters in right order for $1, $2, etc.

Thanks

Upvotes: 1

Views: 99

Answers (1)

Gray
Gray

Reputation: 7140

Try using getopts(). I think the reason you had trouble was your search terms. You needed stuff like options, switches, and arguments rather than parameters and functions.

Here is a quick and dirty example.

while getopts "h?vf:" opt; do
    case "$opt" in
        h|\?)
            show_help
            exit 0
            ;;
        v)  verbose=1
            ;;
        f)  output_file=$OPTARG
            ;;
    esac
done

source

Upvotes: 2

Related Questions