Quantcast
Channel: shell script options pass through to sub-command - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by mikeserv for shell script options pass through to sub-command

$
0
0

If you specify that -- marks the end of options and all following arguments will be passed through to the subcommand, you can do something like you might find in man getopts:

       aflag=
       bflag=
       while getopts ab: name
       do
           case $name in
           a)    aflag=1;;
           b)    bflag=1
                 bval="$OPTARG";;
           ?)   printf "Usage: %s: [-a] [-b value] args\n" $0
                 exit 2;;
           esac
       done
       if [ ! -z "$aflag" ]; then
           printf "Option -a specified\n"
       fi
       if [ ! -z "$bflag" ]; then
           printf 'Option -b "%s" specified\n' "$bval"
       fi
       shift $(($OPTIND - 1))
       printf "Remaining arguments are: %s\n$*"

Specifically I'm referring to the very end there - getopts stops processing options when it encounters -- so all of those arguments will remain in $@. In the example above all of getopts processed arguments are shifted away and the remaining are printed out all at once as $*. If you handle it similarly, you could make the following work:

/mysqldumpwrapper.sh \
    -u username \
    -p password \
    -h localhost \
    -- \
    -now --all of these --are passed through

And to call the wrapped application:

mysqldump "$@" \
    --host=$MYSQL_HOST \
    --user=$MYSQL_USER \
    --password=$MYSQL_PASS "$DB" \
> "$FILE_DEST"

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>