How to Handle Bash with Multiple Arguments for Multiple Options

how to handle bash with multiple arguments for multiple options

Could something like this work for you?

#!/bin/bash

while getopts ":a:p:" opt; do
case $opt in
a) arg1="$OPTARG"
;;
p) arg2="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done

printf "Argument 1 is %s\n" "$arg1"
printf "Argument 2 is %s\n" "$arg2"

You can then call your script like this:

./script.sh -p 'world' -a 'hello'

The output for the above will be:

Argument 1 is hello
Argument 2 is world

Update

You can use the same option multiple times. When parsing the argument values, you can then add them to an array.

#!/bin/bash

while getopts "c:" opt; do
case $opt in
c) currs+=("$OPTARG");;
#...
esac
done
shift $((OPTIND -1))

for cur in "${currs[@]}"; do
echo "$cur"
done

You can then call your script as follows:

./script.sh -c USD -c CAD

The output will be:

USD
CAD

Reference: BASH: getopts retrieving multiple variables from one flag

Bash CLI that accepts multiple arguments

Example how to use GNU getopt to simplify command line parsing:

#! /bin/bash

options=$(getopt -q -o '' -l domain:,ssl:,wp: -- "$@") || {
printf 'ERROR: Invalid argument\n' >&2
exit 1
}
eval set -- "$options"

while true; do
case "$1" in
--domain) DOMAIN="$2"; shift 2;;
--ssl) SSL="$2"; shift 2;;
--wp) WP="$2"; shift 2;;
--) shift; break;;
*) break;;
esac
done

COMMAND=$1; shift

case ${COMMAND:?missing} in
create|delete)
echo "$COMMAND" "${DOMAIN:?missing}" "${SSL:-yes}" "${WP:-yes}";;
*)
printf 'COMMAND: invalid\n' >&2; exit 1;;
esac

Usage:

./cli.sh create --domain http://example.com

The default for --ssl and --wp is "yes".

how to get multiple arguments from option in bash script

Use multiple -ds

./script -d /dev -d /home/work -b /backup

otherwise, the first non-option (i.e. /home/work) would stop getopts option processing and -b won't be considered.

The other alternative would be to use some delimiter like , and parse it yourself

./script -d /dev,/home/work -b /backup

Storing bash script argument with multiple values

One conventional practice (if you're going to do this) is to shift multiple arguments off. That is:

variables=( )
case $key in
--variables)
while (( "$#" >= 2 )) && ! [[ $2 = --* ]]; do
variables+=( "$2" )
shift
done
;;
esac

That said, it's more common to build your calling convention so a caller would pass one -V or --variable argument per following variable -- that is, something like:

myscript --casename obstacle1 --output en -V=v -V=p -V=pResidualTT

...in which case you only need:

case $key in
-V=*|--variable=*) variables+=( "${1#*=}" );;
-V|--variable) variables+=( "$2" ); shift;;
esac

Pass multiple parameters to function, one being an array, another being a variable with spaces

Double quote the variable. Use shift to remove the first argument from the positional parameters.

#! /bin/bash
output(){
echo "$1"

shift
for i in "$@" ; do
echo "$i"
done
}

arr_conf=( "a=1" "b=2" "c=3" )
name="Mr. Test"
output "$name" "${arr_conf[@]}"

Bash, pass multiple values in one argument

Your "question" is actually 2 separate questions:

  1. How do I pass options passed to a shell script through to an awk script, and
  2. How to I make my awk script only print specific values based on strings passed in.

Here's the answer to the first question, i.e. get a shell script to take arguments and pass them through to an awk script (and I added tracing print statements so you can see the values):

$ cat sample4.sh
#!/usr/bin/env bash

while getopts ':c:s:t' opt; do
case $opt in
s) samps="$OPTARG" ;;
c) chans="$OPTARG" ;;
t) totReq=1 ;;
*) printf 'Unrecognized option "%s"\n' "$opt" >&2
esac
done
shift $(( OPTIND - 1 ))

printf 'samps="%s"\n' "$samps" >&2
printf 'chans="%s"\n' "$chans" >&2
printf 'totreq="%d"\n' "$totReq" >&2

hexdump -v -e '8/1 "%02x " "\n"' samples.bin |
awk -v samps="$samps" -v chans="$chans" -v totReq="$totReq" '
BEGIN {
printf "\t\tCh0 Ch1 Ch2 Ch3\n" # print the header line
}
{
printf("Sample %d:", NR - 1) # print the sample number
printf(substr(" ", 1, 8 - length(NR - 1))) # adjust the length of the spaces
for (i = 1; i <= NF; i+=2) { # print every two nibbles
j = i + 1
printf("0x%s%s%s", $i, $j, j == NF ? ORS : OFS)
}
}
'

I'll let you figure out the answer to the second question of what to do with them inside the awk script and then you can ask another question some other time if you get stuck.



Related Topics



Leave a reply



Submit