Shell - how to access arguments passed as flags - bash

I want a shell script that can take advantage of a few optional flags in this style:
script.sh --foo "foo message" --bar "bar message"
script.sh --foo "foo message"
script.sh --bar "bar message"
script.sh --bar "bar message" --foo "foo message"
What would be the simplest methods of accessing these values (ex: "foo message" and "bar message" by their flag names (ex: foo and bar) ?

With bash, you can do something like:
#!/bin/bash
args=()
while test $# -gt 0; do
case $1 in
--foo) foo_msg=$2; shift;;
--bar) bar_msg=$2; shift;;
*) args+=($1);;
esac
shift
done
set -- "${args[#]}"
echo "remainging args: $#"
echo "foo_msg=$foo_msg"
echo "bar_msg=$bar_msg"
If you're using a shell without arrays, it's more difficult to retain the other arguments, but you can usually just consume them in a similar loop.

Related

Why do shortened versions of long options work with getopt?

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

It is possible to mix options and arguments?

Is it possible to mix options (with getopts) and arguments ($1....$10)?
getopt (singular) can handle options and arguments intermixed, as well as short -s and long --long options and -- to end options processing.
See here how file1 and file2 are mixed with options and it separates them out:
$ args=(-ab opt file1 -c opt file2)
$ getopt -o ab:c: -- "${args[#]}"
-a -b 'opt' -c 'opt' -- 'file1' 'file2'
Typical usage looks like:
#!/bin/bash
options=$(getopt -o ab:c: -l alpha,bravo:,charlie: -- "$#") || exit
eval set -- "$options"
# Option variables.
alpha=0
bravo=
charlie=
# Parse each option until we hit `--`, which signals the end of options.
# Don't actually do anything yet; just save their values and check for errors.
while [[ $1 != -- ]]; do
case $1 in
-a|--alpha) alpha=1; shift 1;;
-b|--bravo) bravo=$2; shift 2;;
-c|--charlie) charlie=$2; shift 2;;
*) echo "bad option: $1" >&2; exit 1;;
esac
done
# Discard `--`.
shift
# Here's where you'd actually execute the options.
echo "alpha: $alpha"
echo "bravo: $bravo"
echo "charlie: $charlie"
# File names are available as $1, $2, etc., or in the "$#" array.
for file in "$#"; do
echo "file: $file"
done

Pass options received in bash script to a called script, command or builtin

I have a bash script myscript.sh.
I mean to call another script, command or builtin from within it, e.g., diff.
I mean to pass options to myscript.sh, some of which would be passed to diff when calling it.
The way I implemented this is by setting up an option string optstring via getopt, and then using
eval "diff ${optstring} ${file} ${TRG_DIR}/${filebase2}"
So far, it worked, but I do not know if this is prone to issues when passing arguments with wildcards, etc. At any rate, ...
Is there a better way to do it?
The way I set up optstring is
set -o errexit -o noclobber -o nounset -o pipefail
params="$(getopt -o qy --long brief,side-by-side,suppress-common-lines --name "$0" -- "$#")"
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo params=$params
echo params=$#
eval set -- "$params"
optstring=""
# These variables are likely not needed
brief=false
sbs=false
scl=false
#while false ; do
while true ; do
case "$1" in
-q|--brief)
optstring=${optstring}" -q"
brief=true
echo "brief"
shift
;;
-y|--side-by-side)
optstring=${optstring}" -y"
sbs=true
echo "side-by-side"
shift
;;
--suppress-common-lines)
optstring=${optstring}" --suppress-common-lines"
scl=true
echo "suppress-common-lines"
shift
;;
--)
shift
break
;;
*)
echo "Not implemented: $1" >&2
exit 1
;;
esac
done
echo optstring=${optstring}
Use an array. Arrays can handle multi-word arguments with whitespace. Initialize a blank array with:
options=()
To append an option, do:
options+=(--suppress-common-lines)
Then finally you can get rid of the eval when you call diff and just call it normally. Make sure to quote all of the variable expansions in case they have whitespace:
diff "${options[#]}" "$file" "$TRG_DIR/$filebase2"

Pass $# to a function in a shellscript

Problem description
In a shell script, I want to iterate over all command line arguments ("$#") from inside a function. However, inside a function, $# refers to the function arguments, not the command line arguments. I tried passing the arguments to the function using a variable, but that doesn't help, since it breaks arguments with whitespaces.
How can I pass $# to a function in such a way that it does not break whitespace? I am sorry if this has been asked before, I tried searching for this question and there are a lot similar ones, but I didn't find an answer nevertheless.
Illustration
I made a shell script to illustrate the problem.
print_args.sh source listing
#!/bin/sh
echo 'Main scope'
for arg in "$#"
do
echo " $arg"
done
function print_args1() {
echo 'print_args1()'
for arg in "$#"
do
echo " $arg"
done
}
function print_args2() {
echo 'print_args2()'
for arg in $ARGS
do
echo " $arg"
done
}
function print_args3() {
echo 'print_args3()'
for arg in "$ARGS"
do
echo " $arg"
done
}
ARGS="$#"
print_args1
print_args2
print_args3
print_args.sh execution
$ ./print_args.sh foo bar 'foo bar'
Main scope
foo
bar
foo bar
print_args1()
print_args2()
foo
bar
foo
bar
print_args3()
foo bar foo bar
As you can see, I can't get the last foo bar to appear as a single argument. I want a function that gives the same output as the main scope.
You can use this BASH function:
#!/bin/bash
echo 'Main scope'
for arg in "$#"
do
echo " $arg"
done
function print_args1() {
echo 'print_args1()'
for arg in "$#"; do
echo " $arg"
done
}
function print_args3() {
echo 'print_args3()'
for arg in "${ARGS[#]}"; do
echo " $arg"
done
}
ARGS=( "$#" )
print_args1 "$#"
print_args3
You can see use of bash shebang at top:
#!/bin/bash
required be able to use BASH arrays.
Output:
bash ./print_args.sh foo bar 'foo bar'
Main scope
foo
bar
foo bar
print_args1()
foo
bar
foo bar
print_args3()
foo
bar
foo bar

Can't assign a value to a variable in a case statement in bash

I just ran into a problem while I was trying to assign a value to a variable inside a case statement, here is my code:
#!/bin/bash
while getopts ":m:n:::" opt; do
case $opt in
n)
echo "-n was triggered, Parameter: $OPTARG " >&2
case $OPTARG in
t)
echo threads
r=threads
;;
p)
echo processes
r="something"
;;
esac
;;
m)
echo "-m was triggered, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo $r
echo No thread/processes: $2 P/T: $4 IF: $5 OF: $6
I'd like to use the variable $r later, but I can't. When I try to print it using echo (as it is in my script), it does not return a thing.
I've been trying to spot my mistake but I couldn't.
There is a similar post that suggested to remove blank spaces before and after the =, but as you can see, there are no blank spaces in mine.
Here is what I get from the console when I run it:
$ ./friendfind -n 2 -m p IN OUT
-n was triggered, Parameter: 2
-m was triggered, Parameter: p
No thread/processes: 2 P/T: p IF: IN OF: OUT
The purpose of the script was to run a c file with the option to run it with threads or processes, so it asks for the number of processes/threads you want to use, if you want to tu use processes or threads and the input and output file.
I think you were aiming at something like this:
#!/bin/bash
# Print usage message:
usage() {
echo "Usage: $0 [-n N] [-t|-p] INPUT OUTPUT" >> /dev/stdout
}
# Set default values
n_threads=1
use_threads=1
while getopts "n:pth" opt; do
case $opt in
n) n_threads=$OPTARG;;
t) use_threads=1;;
p) use_threads=0;;
h) usage; exit 0;;
*) usage; exit 1;;
esac
done
# Get rid of scanned options
shift $((OPTIND-1))
if (($# != 2)); then usage; exit 1; fi
if ((use_threads)); then
echo "Using $n_threads threads. IF: $1; OF: $2"
# ...
else
echo "Using $n_threads processes. IF: $1; OF: $2"
# ...
fi
Here's some example invocations, including a couple of errors:
$ ./ff -p foo bar
Using 1 processes. IF: foo; OF: bar
$ ./ff foo bar
Using 1 threads. IF: foo; OF: bar
$ ./ff -n 7 foo bar
Using 7 threads. IF: foo; OF: bar
$ ./ff -n 7 -p foo bar
Using 7 processes. IF: foo; OF: bar
$ ./ff -p -n7 foo bar
Using 7 processes. IF: foo; OF: bar
$ ./ff -q -n7 foo bar
./ff: illegal option -- q
Usage: ./ff [-n N] [-t|-p] INPUT OUTPUT
# Note: The error message here could be more informative.
# Exercise left for the reader
$ ./ff -n 7 foo
Usage: ./ff [-n N] [-t|-p] INPUT OUTPUT

Resources