bash script // long options which store their arguments as variables
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)
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
-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 ;;
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.