Results 1 to 3 of 3
Hello,
I am attempting to write a shell script which will accept long options and store the arguments to said options as variables for future use.
The script will be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-05-2011 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 20
bash script // long options which store their arguments as variables
Hello,
I am attempting to write a shell script which will accept long options and store the arguments to said options as variables for future use.
The script will be used as an interface for vzctl operations on OpenVZ nodes. (Will be integrated with another remote script which runs via SSH/sudo on the remote hardware node)
For instance...
usage() {
echo -e "\nActions:
--help Print command summary\n
--suspend Suspend VPS container\n
--unsuspend Unsuspend VPS container\n
--chgtmpl Change container VE template (e.g. Linux-512M --> Linux-1024M)\n
Identifiers:
-i, container IP address e.g. 10.253.9.70\n\n
e.g. In order to suspend the VPS of IP address 10.253.9.70:\n
vpsctl --suspend 10.253.9.70\n
e.g. In order to upgrade the VPS template of the VPS of IP address 10.253.9.70:\n
vpsctl -i 10.253.9.70 --chgtmpl Linux-1024M\n"
}
Using getopts, this would be absolutely simple.
while getopts hs:u:i: ARGS; do
case $ARGS in
h) usage && exit 0 ;;
u) unsuspend_ct=$OPTARG ;;
s) suspend_ct=$OPTARG ;;
i) ct_ip=$OPTARG ;;
esac
done
That way, I could proceed with the script now that my variables are stored.
The issue is that I can't use getopts with long options, and all the examples I've seen with 'set / getopt -l' have not been storing option arguments as variables, but rather executing actions when certain flags are called.
What would be the easiest way for me to accomplish long options with '$OPTARG' equivalents? It's absolutely necessary that I use long options and store the arguments to those options as variables.
- 09-06-2011 #2Just Joined!
- Join Date
- Aug 2005
- Posts
- 5
Hi,
I use the following custom parser for command line arguments, it however does not parse combined short args, but for single arg, it works file, also it parses long opts.
Code:function startWithDash() { echo "$1" | egrep -q '^-' && return 0 || return 1 } ######################### in_input_file= in_cred_file= in_dest_dir= in_interactive= in_quiet= in_just_print= in_print_mkdir= in_print_download= in_print_echo= while [ -n "$*" ]; do _dd= case "x$1" in x-f) [ "$2" = "--" ] && { shift; _dd=y; } [ -z "$2" ] || ( [ -z "$_dd" ] && [ "$2" != "-" ] && startWithDash "$2" ) && { echo "missing '$1' parameter" >&2; usage; } in_input_file="$2" shift ;; x-f*) in_input_file="$(echo "$1" | sed 's#^-.##')" ;; #============= x-d) [ "$2" = "--" ] && { shift; _dd=y; } [ -z "$2" ] || ( [ -z "$_dd" ] && startWithDash "$2" ) && { echo "missing '$1' parameter" >&2; usage; } in_dest_dir="$2" shift ;; x-d*) in_dest_dir="$(echo "$1" | sed 's#^-.##')" ;; #============= x-c|x--cred-file) [ "$2" = "--" ] && { shift; _dd=y; } [ -z "$2" ] || ( [ -z "$_dd" ] && startWithDash "$2" ) && { echo "missing '$1' parameter" >&2; usage; } in_cred_file="$2" shift ;; x-c*) in_cred_file="$(echo "$1" | sed 's#^-.##')" ;; #============= x-i) in_interactive=y ;; #============= x-q) in_quiet=y ;; #============= x--print) in_print_mkdir=y in_print_download=y in_print_echo=y in_just_print=y ;; #============= x--print-mkdir) in_print_mkdir=y in_just_print=y ;; #============= x--print-download) in_print_download=y in_just_print=y ;; #============= x--print-echo) in_print_echo=y in_just_print=y ;; #============= *) echo "un recognized command line option: [$1]" >&2 exit 1 ;; esac shift done ########################################
- 09-08-2011 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
There is an "advanced" version of option parser getopt on some distributions. There are example scripts provided, for example:
when this is executed from a file run2 as:Code:#!/bin/bash # /usr/share/doc/util-linux/examples/getopt-parse.bash # A small example program for using the new getopt(1) program. # This program will only work with bash(1) # An similar program using the tcsh(1) script language can be found # as parse.tcsh # Example input and output (from the bash prompt): # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> `par1' # --> `another arg' # --> `wow!*\?' # Note that we use `"$@"' to let each command-line parameter expand to a # separate word. The quotes around `$@' are essential! # We need TEMP as the `eval set --' would nuke the return value of getopt. TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \ -n "$0" -- "$@"` # -n 'example.bash' -- "$@"` if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! eval set -- "$TEMP" while true ; do case "$1" in -a|--a-long) echo "Option a" ; shift ;; -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;; -c|--c-long) # c has an optional argument. As we are in quoted mode, # an empty parameter will be generated if its optional # argument is not found. case "$2" in "") echo "Option c, no argument"; shift 2 ;; *) echo "Option c, argument \`$2'" ; shift 2 ;; esac ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done echo "Remaining arguments:" for arg do echo '--> '"\`$arg'" ; done
it produces:Code:% ./run2 -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
The context for this run is:Code:Option a Option c, no argument Option c, argument `more' Option b, argument ` very long ' Remaining arguments: --> `par1' --> `another arg' --> `wowrun2\?'
There is also a script that can handle GNU-style long options at: http://stchaz.free.fr/getopts_long.sh but I have not tried that.Code:Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) GNU bash 3.2.39 getopt (enhanced) 1.1.4
Good luck ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
