Here is the code, I want to run this command
Getting the test.log input, then convert it to text type, then store it on the output.txt file
./small.sh test.log -t text -o output.txt
#!/usr/bin/env bash
usage() { echo "$0 usage:" && grep " .)\ #" $0; exit 0; }
[ $# -eq 0 ] && usage
parse()
{
local file=$1
local type=$2
local output=$3
echo "file: ${file}, type: ${type}, output: ${output}"
}
while getopts ":ht:o:" arg; do
case $arg in
t) # specify type.
type=${OPTARG} ;;
o) # specify directory.
output=${OPTARG} ;;
h | *) # Display help.
usage
exit 0
;;
esac
done
shift $(( OPTIND - 1))
parse "${file}" "${type}" "${output}"
with this code i can only get filename when i put it in unsort order like this
./small.sh -t text -o output.txt test.log
How can I get filename and argument with getopts
./small.sh test.log -t text -o output.txt
It is not possible. getopts only supports positional arguments after option arguments.
You can:
accept the limitation
use something elsem, like GNU getopt.
write your own option parsing code
Related
Hi all I think I am going insane. I have the following at the beginning of a bash script:
#!/bin/bash
while getopts i:o:c:p:g: flag
do
case "${flag}" in
g) genome=${OPTARG};;
p) poscol=${OPTARG};;
c) chromcol=${OPTARG};;
i) inf=${OPTARG};;
o) outf=${OPTARG};;
esac
done
echo ""
echo "This should be a list of the arguments:"
echo $inf
echo $outf
echo $poscol
echo $chromcol
echo $genome
echo "-----------------------"
Yet when I run a test of the code, eg:
bash Code.sh -i test.txt -o test.txt -c 0 -p 1 -g testgenome
bash returns:
This should be a list of the arguments:
-----------------------
I literally just copied this section of code from a script where the arguments work fine. Does anyone have any idea what could be going on here? Thanks in advance.
In the following script:
#!/usr/bin/env bash
func_usage ()
{
cat <<EOF \
USAGE: ${0} \
EOF
}
## Defining_Version
version=1.0
## Defining_Input
options=$(getopt -o "t:" -l "h,help,v,version,taxonomy:" -a -- "$#")
eval set -- "$options"
while true;do
case $1 in
-h|--h|-help|--help)
func_usage
exit 0
;;
-v|--v|-version|--version)
echo $version
;;
-t|--t|-taxonomy|--taxonomy)
echo "Option t = $2 ";
Taxonomy_ID=$2
echo $Taxonomy_ID
shift
;;
--)
shift
break;;
esac
shift
done
## Defining Taxonomy Default Value (in case is not provided)
TaxonomyID=${Taxonomy_ID:=9606};
echo $TaxonomyID
exit 0
The commands:
./script.sh -v
./script.sh --v
./script.sh -version
./script.sh --version
Work as expected. But what I do not understand is why the commands:
./script.sh -ver
./script.sh --ver
work at all. An equivalent unexpected behavior is also observed for the commands:
./script.sh -tax 22
./script.sh --tax 22
I would be grateful to get an explanation and/or a way to correct this unexpected behavior.
Note that getopt is an external utility unrelated to Bash.
what I do not understand is why the commands: .. work at all.
Because getopt was designed to support it, there is no other explanation. From man getopt:
[...] Long options may be abbreviated, as long as the abbreviation is not ambiguous.
Unambiguous abbreviations of long options are converted to long options.
Based on the comments I have received, specially from #CharlesDuffy, I have modified my code to what I believe is a more robust and compatible version. Importantly, the code below addresses the pitfalls of the original code
#!/usr/bin/env bash
func_usage ()
{
cat <<EOF
USAGE: ${0}
EOF
## Defining_Version
version=1.0
## Defining_Input
while true;do
case $1 in
-h|--h|-help|--help|-\?|--\?)
func_usage
exit 0
;;
-v|--v|-version|--version)
echo $version
;;
-t|--t|-taxonomy|--taxonomy)
echo "Option t = $2 ";
Taxonomy_ID=$2
echo $Taxonomy_ID
shift
;;
--)
shift
break;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break
esac
shift
done
TaxonomyID=${Taxonomy_ID:=9606};
echo $TaxonomyID
exit 0
The code above behaves as expected in that the commands:
./script -tax 22
Gives the warning:
WARN: Unknown option (ignored): -tax
9606
As expected
I have a script that starts with getopts and looks as follows:
USAGE() { echo -e "Usage: bash $0 [-w <in-dir>] [-o <out-dir>] [-c <template1>] [-t <template2>] \n" 1>&2; exit 1; }
if (($# == 0))
then
USAGE
fi
while getopts ":w:o:c:t:h" opt
do
case $opt in
w ) BIGWIGS=$OPTARG
;;
o ) OUTDIR=$OPTARG
;;
c ) CONTAINER=$OPTARG
;;
t ) TRACK=$OPTARG
;;
h ) USAGE
;;
\? ) echo "Invalid option: -$OPTARG exiting" >&2
exit
;;
: ) echo "Option -$OPTARG requires an argument" >&2
exit
;;
esac
done
more commands etc
echo $OUTDIR
echo $CONTAINER
I am fairly new to getopts. I was doing some testing on this script and at some stage, I didn't need/want to use the -c argument [-c ]. In other words, I was trying to test another specific part of the script not involving the $CONTAINER variable at all. Therefore, I simply added # in front of all commands with the $CONTAINER and did some testing which was fine.
When testing the script without using $CONTAINER, I typed:
bash script.bash -w mydir -o myoutdir -t mywantedtemplate
However, I was wondering, given my getopts command I didn't get a warning. In other words, why did I not get a warning asking for -c argument. Is this possible? Does the warning only occur if I type:
bash script.bash -w mydir -o myoutdir -t mywantedtemplate -c
UPDATE
After doing some testing, I think that is it:
If you don't explicitly write "-c", getopts won't "ask" you for it and give you an error (unless your script is doing something with it - i.e. if you haven't put # in front of each command using this argument)
You only get an error if you put "-c "
Is this correct?
getopts does not warn when some options are not used (i.e. they are optional). Usually that's a good thing because some options (e.g. -h) are not used with other options. There is no way to specify mandatory options directly with the Bash builtin getopts. If you want mandatory options then you will need to write code to check that they have been used. See bash getopts with multiple and mandatory options. Also (as you have found), you won't get an error if you fail to write code to handle options specified in the optstring (first) argument to getopts.
You could get a kind of automatic warning for mandatory arguments by using the nounset setting in your Bash code (with set -o nounset or set -u). That would cause warnings to be issued for code like echo $CONTAINER if the -c option is not specified so $CONTAINER is not set. However, using the nounset option would mean that all of your code needs to be written more carefully. See How can I make bash treat undefined variables as errors?, including the comments and "Linked" answers, for more information.
You might use this script:
printf captures the stdout and put into the stderr and then back to the stdout also captures the exit code, so we can handle if the code is greater than 0.
some_command() {
echo 'this is the stdout'
echo 'this is the stderr' >&2
exit 1
}
run_command() {
{
IFS=$'\n' read -r -d '' stderr;
IFS=$'\n' read -r -d '' stdout;
(IFS=$'\n' read -r -d '' _ERRNO_; exit ${_ERRNO_});
} < <((printf '\0%s\0%d\0' "$(some_command)" "${?}" 1>&2) 2>&1)
}
echo 'Run command:'
if ! run_command; then
## Show the values
typeset -p stdout stderr
else
typeset -p stdout stderr
fi
Just replace some_command with getopts ":w:o:c:t:h"
Without introducing getopts, I'd like to parse expressions like so:
./cli.sh data i -f -f="./path/to/file.txt" --flags="--a --b"
And variations/combinations thereof:
./cli.sh -file="./path/to/file.txt" data --flags="--a --b" -f i
Unfortunately, I am stuck with reading all optional values of the long options submitted, for example: --flags="--a --b" only returns --a as value for the long option --flags.
I have created a minimalized version of a parser which exhibits my current issue:
#!/usr/bin/env bash
__init_system () {
CMD_SED="sed"
CMD_SED_EXT="sed -E"
}
__init_system_Darwin () {
__init_system
CMD_SED="gsed"
CMD_SED_EXT="gsed -E"
}
invoke_func () {
SYSTEM=$(uname)
FUNC=$1 && shift 1
declare -f ${FUNC}_${SYSTEM} >/dev/null
[ $? -eq 0 ] && { ${FUNC}_${SYSTEM} "$#"; return $?; } || { ${FUNC} "$#"; return $?; }
}
handle_cli_harmonizing () {
cli_adjusted=$(echo "$#" | ${CMD_SED_EXT} \
-e 's#([ ])+# #g'\
-e 's#\<h\>#help#g'\
-e 's#\<e\>#enable#g'\
-e 's#\<d\>#disable#g'\
-e 's#\<i\>#import#g'\
-e 's#\<sl\>#showlog#g'\
-e 's#\<st\>#status#g'\
| tr ' ' '\n' | sort -u | xargs
)
#echo ">>> Original: <$#>"
#echo ">>> Adjusted: <$cli_adjusted>"
}
handle_cli_parsing () {
for param in $#; do
case ${param} in
start|stop|status|enable|disable|data|showlog)
CMD=$param
;;
help|app|stats|import|export|dbinfo|exec)
SPEC=$param
;;
--*|-*)
#echo ">>> Found param: <$param>"
OPT="$OPT $param"
;;
*)
echo ">>> Parsing mismatch: $param"
;;
esac
done
}
handle_cli_optparams () {
for opt in $OPT; do
case "$opt" in
--force|-f)
OPT_FORCE=1
;;
--file=*|-f=*)
OPT_FILE=1
FILE=${opt#*=}
;;
--flags=*)
OPT_FLAGS=1
FLAGS=${opt#*=}
;;
*)
echo ">>> Unknown/unimplemented option specifier: $opt"
;;
esac
done
}
invoke_func __init_system
invoke_func handle_cli_harmonizing "$#"
invoke_func handle_cli_parsing ${cli_adjusted}
invoke_func handle_cli_optparams
echo "DEBUG[ CLI]: CMD=$CMD SPEC=$SPEC"
echo "DEBUG[TOGGLE]: FORCE=$OPT_FORCE FILE=$OPT_FILE FLAGS=$OPT_FLAGS"
echo "DEBUG[ OPTS]: FILE=$FILE FLAGS=$FLAGS"
Expected ouput:
$ ./cli.sh data i -f -f="./path/to/file.txt" --flags="--a --b"
DEBUG[ CLI]: CMD=data SPEC=import
DEBUG[TOGGLE]: FORCE=1 FILE=1 FLAGS=1
DEBUG[ OPTS]: FILE=./path/to/file.txt FLAGS=--a --b
Current output:
$ ./cli.sh data i -f -f="./path/to/file.txt" --flags="--a --b"
>>> Unknown/unimplemented option specifier: --b
DEBUG[ CLI]: CMD=data SPEC=import
DEBUG[TOGGLE]: FORCE=1 FILE=1 FLAGS=1
DEBUG[ OPTS]: FILE=./path/to/file.txt FLAGS=--a
It's probably some quoting issue or a missing set, however I seem to be stuck here, and I'd really prefer to keep the simplistic approach I have taken.
I'd be especially stocked if someone would additionally offer a shell-only (no bashims) version for portability concerns, but that's icing on the cake. Mind you, this is only a very simple part of the whole parser I have written; enough to exhibit my current challenge.
I have read all the suggested linked issues while writing this question, and I have pondered for a while on How do I parse command line arguments in Bash?.
Update (2020/09/12): Even though this question has prematurely been downvoted, I found an elegant and more flexible solution, and have posted it as an answer below. It still beats getopt or any other approach I have seen concerning my specific requirements to command line parsing.
The root of the problem is here:
OPT="$OPT $param"
This is where the spaces embedded in $param become indistinguishable from the space used to append to the end of $OPT.
You can avoid this problem by making OPT an array, append values to it correctly:
OPT+=("$param")
... and then use array syntax to iterate over the values of the array:
for opt in "${OPT[#]}"; do
... and so on, consistently, everywhere, and watch out for correct double-quoting.
Even after years of abandonment, I'd like to describe the solution to the question asked above.
While janos gave me the pointer, the solution is actually much simpler and maintains the original flexibility envisioned:
#!/usr/bin/env bash
handle_cli_parsing () {
while [ $# -ne 0 ]; do
param=$1
[ x"$param" = x"sl" ] && param=showlog
[ x"$param" = x"sh" ] && param=showlog
[ x"$param" = x"h" ] && param=help
[ x"$param" = x"i" ] && param=import
[ x"$param" = x"e" ] && param=enable
[ x"$param" = x"d" ] && param=disable
case ${param} in
start|stop|status|enable|disable|data|showlog)
CMD=$param
;;
help|app|stats|import|export|dbinfo|exec)
SPEC=$param
;;
--force|-f)
OPT_FORCE=1
;;
--file=*|-f=*)
OPT_FILE=1
FILE=${param#*=}
;;
--flags=*)
OPT_FLAGS=1
FLAGS=${param#*=}
;;
--*|-*)
echo ">>> New param: <$param>"
;;
*)
echo ">>> Parsing mismatch: $param"
;;
esac
shift 1
done
}
handle_cli_parsing "$#"
echo "DEBUG[ CLI]: CMD=$CMD SPEC=$SPEC"
echo "DEBUG[TOGGLE]: FORCE=$OPT_FORCE FILE=$OPT_FILE FLAGS=$OPT_FLAGS"
echo "DEBUG[ OPTS]: FILE=$FILE FLAGS=$FLAGS"
The output is now as expected and the parsing as flexible as intended:
$ ./cli.sh data i -f -f="./path/to/file.txt" --flags="--a --b"
DEBUG[ CLI]: CMD=data SPEC=import
DEBUG[TOGGLE]: FORCE=1 FILE=1 FLAGS=1
DEBUG[ OPTS]: FILE=./path/to/file.txt FLAGS=--a --b
I want to design a shell script as a wrapper for a couple of scripts. I would like to specify parameters for myshell.sh using getopts and pass the remaining parameters in the same order to the script specified.
If myshell.sh is executed like:
myshell.sh -h hostname -s test.sh -d waittime param1 param2 param3
myshell.sh param1 param2 -h hostname param3 -d waittime -s test.sh
myshell.sh param1 -h hostname -d waittime -s test.sh param2 param3
All of the above should be able to call as
test.sh param1 param2 param3
Is it possible to utilize the options parameters in the myshell.sh and post remaining parameters to underlying script?
I wanted to do something similar to the OP, and I found the relevant information I required here and here
Essentially if you want to do something like:
script.sh [options] ARG1 ARG2
Then get your options like this:
while getopts "h:u:p:d:" flag; do
case "$flag" in
h) HOSTNAME=$OPTARG;;
u) USERNAME=$OPTARG;;
p) PASSWORD=$OPTARG;;
d) DATABASE=$OPTARG;;
esac
done
And then you can get your positional arguments like this:
ARG1=${#:$OPTIND:1}
ARG2=${#:$OPTIND+1:1}
More information and details are available through the link above.
myshell.sh:
#!/bin/bash
script_args=()
while [ $OPTIND -le "$#" ]
do
if getopts h:d:s: option
then
case $option
in
h) host_name="$OPTARG";;
d) wait_time="$OPTARG";;
s) script="$OPTARG";;
esac
else
script_args+=("${!OPTIND}")
((OPTIND++))
fi
done
"$script" "${script_args[#]}"
test.sh:
#!/bin/bash
echo "$0 $#"
Testing the OP's cases:
$ PATH+=:. # Use the cases as written without prepending ./ to the scripts
$ myshell.sh -h hostname -s test.sh -d waittime param1 param2 param3
./test.sh param1 param2 param3
$ myshell.sh param1 param2 -h hostname param3 -d waittime -s test.sh
./test.sh param1 param2 param3
$ myshell.sh param1 -h hostname -d waittime -s test.sh param2 param3
./test.sh param1 param2 param3
What's going on:
getopts will fail if it encounters a positional parameter. If it's used as a loop condition, the loop would break prematurely whenever positional parameters appear before options, as they do in two of the test cases.
So instead, this loop breaks only once all parameters have been processed. If getopts doesn't recognize something, we just assume it's a positional parameter, and stuff it into an array while manually incrementing getopts's counter.
Possible improvements:
As written, the child script can't accept options (only positional parameters), since getopts in the wrapper script will eat those and print an error message, while treating any argument like a positional parameter:
$ myshell.sh param1 param2 -h hostname -d waittime -s test.sh -a opt1 param3
./myshell.sh: illegal option -- a
./test.sh param1 param2 opt1 param3
If we know the child script can only accept positional parameters, then myshell.sh should probably halt on an unrecognized option. That could be as simple as adding a default last case at the end of the case block:
\?) exit 1;;
$ myshell.sh param1 param2 -h hostname -d waittime -s test.sh -a opt1 param3
./myshell.sh: illegal option -- a
If the child script needs to accept options (as long as they don't collide with the options in myshell.sh), we could switch getopts to silent error reporting by prepending a colon to the option string:
if getopts :h:d:s: option
Then we'd use the default last case to stuff any unrecognized option into script_args:
\?) script_args+=("-$OPTARG");;
$ myshell.sh param1 param2 -h hostname -d waittime -s test.sh -a opt1 param3
./test.sh param1 param2 -a opt1 param3
Mix opts and args :
ARGS=""
echo "options :"
while [ $# -gt 0 ]
do
unset OPTIND
unset OPTARG
while getopts as:c: options
do
case $options in
a) echo "option a no optarg"
;;
s) serveur="$OPTARG"
echo "option s = $serveur"
;;
c) cible="$OPTARG"
echo "option c = $cible"
;;
esac
done
shift $((OPTIND-1))
ARGS="${ARGS} $1 "
shift
done
echo "ARGS : $ARGS"
exit 1
Result:
bash test.sh -a arg1 arg2 -s serveur -c cible arg3
options :
option a no optarg
option s = serveur
option c = cible
ARGS : arg1 arg2 arg3
getopts won't parse the mix of param1 and -n options.
It is much better to put param1-3 into options like others.
Furthermore you can use already existing libraries such as shflags. It is pretty smart and it is easy to use.
And the last way is to write your own function to parse params without getopts, just iterating all params through case construction. It is the hardest way but it is the only way to match your expectations exactly.
I thought up one way that getopts can be extended to truly mix options and positional parameters. The idea is to alternate between calling getopts and assigning any positional parameters found to n1, n2, n3, etc.:
parse_args() {
_parse_args 1 "$#"
}
_parse_args() {
local n="$1"
shift
local options_func="$1"
shift
local OPTIND
"$options_func" "$#"
shift $(( OPTIND - 1 ))
if [ $# -gt 0 ]; then
eval test -n \${n$n+x}
if [ $? -eq 0 ]; then
eval n$n="\$1"
fi
shift
_parse_args $(( n + 1 )) "$options_func" "$#"
fi
}
Then in the OP's case, you could use it like:
main() {
local n1='' n2='' n3=''
local duration hostname script
parse_args parse_main_options "$#"
echo "n1 = $n1"
echo "n2 = $n2"
echo "n3 = $n3"
echo "duration = $duration"
echo "hostname = $hostname"
echo "script = $script"
}
parse_main_options() {
while getopts d:h:s: opt; do
case "$opt" in
d) duration="$OPTARG" ;;
h) hostname="$OPTARG" ;;
s) script="$OPTARG" ;;
esac
done
}
main "$#"
Running it shows the output:
$ myshell.sh param1 param2 -h hostname param3 -d waittime -s test.sh
n1 = param1
n2 = param2
n3 = param3
duration = waittime
hostname = hostname
script = test.sh
Just a proof of concept, but maybe it's useful to someone.
Note: there's a gotcha if one function that uses parse_args calls another function that uses parse_args and the outer function declares e.g. local n4='', but the inner one doesn't and 4 or more positional parameters are passed to the inner function
Just mashed up a quickie, which easily handles a mixture of options and positional-parameters (leaving only positional-params in $#):
#!/bin/bash
while [ ${#} -gt 0 ];do OPTERR=0;OPTIND=1;getopts "p:o:hvu" arg;case "$arg" in
p) echo "Path: [$OPTARG]" ;;
o) echo "Output: [$OPTARG]" ;;
h) echo "Help" ;;
v) echo "Version" ;;
\?) SET+=("$1") ;;
*) echo "Coding error: '-$arg' is not handled by case">&2 ;;
esac;shift;[ "" != "$OPTARG" ] && shift;done
[ ${#SET[#]} -gt 0 ] && set "" "${SET[#]}" && shift
echo -e "=========\nLeftover (positional) parameters (count=$#) are:"
for i in `seq $#`;do echo -e "\t$i> [${!i}]";done
Sample output:
[root#hots:~]$ ./test.sh 'aa bb' -h -v -u -q 'cc dd' -p 'ee ff' 'gg hh' -o ooo
Help
Version
Coding error: '-u' is not handled by case
Path: [ee ff]
Output: [ooo]
=========
Leftover (positional) parameters (count=4) are:
1> [aa bb]
2> [-q]
3> [cc dd]
4> [gg hh]
[root#hots:~]$
Instead of using getopts, you can directly implement your own bash argument parser. Take this as a working example. It can handle simultaneously name and position arguments.
#!/bin/bash
function parse_command_line() {
local named_options;
local parsed_positional_arguments;
yes_to_all_questions="";
parsed_positional_arguments=0;
named_options=(
"-y" "--yes"
"-n" "--no"
"-h" "--help"
"-s" "--skip"
"-v" "--version"
);
function validateduplicateoptions() {
local item;
local variabletoset;
local namedargument;
local argumentvalue;
variabletoset="${1}";
namedargument="${2}";
argumentvalue="${3}";
if [[ -z "${namedargument}" ]]; then
printf "Error: Missing command line option for named argument '%s', got '%s'...\\n" "${variabletoset}" "${argumentvalue}";
exit 1;
fi;
for item in "${named_options[#]}";
do
if [[ "${item}" == "${argumentvalue}" ]]; then
printf "Warning: Named argument '%s' got possible invalid option '%s'...\\n" "${namedargument}" "${argumentvalue}";
exit 1;
fi;
done;
if [[ -n "${!variabletoset}" ]]; then
printf "Warning: Overriding the named argument '%s=%s' with '%s'...\\n" "${namedargument}" "${!variabletoset}" "${argumentvalue}";
else
printf "Setting '%s' named argument '%s=%s'...\\n" "${thing_name}" "${namedargument}" "${argumentvalue}";
fi;
eval "${variabletoset}='${argumentvalue}'";
}
# https://stackoverflow.com/questions/2210349/test-whether-string-is-a-valid-integer
function validateintegeroption() {
local namedargument;
local argumentvalue;
namedargument="${1}";
argumentvalue="${2}";
if [[ -z "${2}" ]];
then
argumentvalue="${1}";
fi;
if [[ -n "$(printf "%s" "${argumentvalue}" | sed s/[0-9]//g)" ]];
then
if [[ -z "${2}" ]];
then
printf "Error: The %s positional argument requires a integer, but it got '%s'...\\n" "${parsed_positional_arguments}" "${argumentvalue}";
else
printf "Error: The named argument '%s' requires a integer, but it got '%s'...\\n" "${namedargument}" "${argumentvalue}";
fi;
exit 1;
fi;
}
function validateposisionaloption() {
local variabletoset;
local argumentvalue;
variabletoset="${1}";
argumentvalue="${2}";
if [[ -n "${!variabletoset}" ]]; then
printf "Warning: Overriding the %s positional argument '%s=%s' with '%s'...\\n" "${parsed_positional_arguments}" "${variabletoset}" "${!variabletoset}" "${argumentvalue}";
else
printf "Setting the %s positional argument '%s=%s'...\\n" "${parsed_positional_arguments}" "${variabletoset}" "${argumentvalue}";
fi;
eval "${variabletoset}='${argumentvalue}'";
}
while [[ "${#}" -gt 0 ]];
do
case ${1} in
-y|--yes)
yes_to_all_questions="${1}";
printf "Named argument '%s' for yes to all questions was triggered.\\n" "${1}";
;;
-n|--no)
yes_to_all_questions="${1}";
printf "Named argument '%s' for no to all questions was triggered.\\n" "${1}";
;;
-h|--help)
printf "Print help here\\n";
exit 0;
;;
-s|--skip)
validateintegeroption "${1}" "${2}";
validateduplicateoptions g_installation_model_skip_commands "${1}" "${2}";
shift;
;;
-v|--version)
validateduplicateoptions branch_or_tag "${1}" "${2}";
shift;
;;
*)
parsed_positional_arguments=$((parsed_positional_arguments+1));
case ${parsed_positional_arguments} in
1)
validateposisionaloption branch_or_tag "${1}";
;;
2)
validateintegeroption "${1}";
validateposisionaloption g_installation_model_skip_commands "${1}";
;;
*)
printf "ERROR: Extra positional command line argument '%s' found.\\n" "${1}";
exit 1;
;;
esac;
;;
esac;
shift;
done;
if [[ -z "${g_installation_model_skip_commands}" ]];
then
g_installation_model_skip_commands="0";
fi;
}
You would call this function as:
#!/bin/bash
source ./function_file.sh;
parse_command_line "${#}";
Usage example:
./test.sh as 22 -s 3
Setting the 1 positional argument 'branch_or_tag=as'...
Setting the 2 positional argument 'skip_commands=22'...
Warning: Overriding the named argument '-s=22' with '3'...
References:
example_installation_model.sh.md
Checking for the correct number of arguments
https://unix.stackexchange.com/questions/129391/passing-named-arguments-to-shell-scripts
An example of how to use getopts in bash
There are some standards for unix option processing, and in shell programming, getopts is the best way of enforcing them. Almost any modern language (perl, python) has a variant on getopts.
This is just a quick example:
command [ options ] [--] [ words ]
Each option must start with a dash, -, and must consist of a single character.
The GNU project introduced Long Options, starting with two dashes --,
followed by a whole word, --long_option. The AST KSH project has a getopts that also supports long options, and long options starting with a single dash, -, as in find(1) .
Options may or may not expect arguments.
Any word not starting with a dash, -, will end option processing.
The string -- must be skipped and will end option processing.
Any remaining arguments are left as positional parameters.
The Open Group has a section on Utility Argument Syntax
Eric Raymond's The Art of Unix Programming has a chapter on traditional unix choices for option letters and their meaning.
You can try this trick: after while loop with optargs, just use this snippet
#shift away all the options so that only positional agruments
#remain in $#
for (( i=0; i<OPTIND-1; i++)); do
shift
done
POSITIONAL="$#"
However, this approach has a bug:
all the options after the first positional argument are ingored by getopts and are considered as positional arguments - event those that are correct (see sample output: -m and -c are among positional arguments)
Maybe it has even more bugs...
Look at the whole example:
while getopts :abc opt; do
case $opt in
a)
echo found: -a
;;
b)
echo found: -b
;;
c)
echo found: -c
;;
\?) echo found bad option: -$OPTARG
;;
esac
done
#OPTIND-1 now points to the first arguments not beginning with -
#shift away all the options so that only positional agruments
#remain in $#
for (( i=0; i<OPTIND-1; i++)); do
shift
done
POSITIONAL="$#"
echo "positional: $POSITIONAL"
Output:
[root#host ~]# ./abc.sh -abc -de -fgh -bca haha blabla -m -c
found: -a
found: -b
found: -c
found bad option: -d
found bad option: -e
found bad option: -f
found bad option: -g
found bad option: -h
found: -b
found: -c
found: -a
positional: haha blabla -m -c