Robert Siemer
- 35.2k
- 11
- 89
- 101
- Use enhanced
getoptfrom util-linux or formerly GNU glibc.1 - It works with
getopt_long()the C function of GNU glibc. - no other solution on this page can do all this:
- handles spaces, quoting characters and even binary in arguments2 (nonthis rules non-enhanced
getoptcan’t do thisout) - it can handle options at the end:
script.sh -o outFile file1 file2 -v(this rulesgetoptsdoesn’t do thisout) - allows
=-style long options:script.sh --outfile=fileOut --infile fileIn(allowing both isat the same time makes it really lengthy ifwhen self parsing) - allows combined short options, e.g.
-vfd(real work iftogether with the one before this practically rules out self parsing) - allows touching option-arguments, e.g.
-oOutfileor-vfdoOutfile(you still want to program it yourself?)
- handles spaces, quoting characters and even binary in arguments2 (nonthis rules non-enhanced
- Is so old already3 that it comes preinstalled on any GNU system (i.e. Linux mostly); see footnote1
- You can test for its existence with:
getopt --test→ return value 4. - Other
getoptor shell-builtingetoptsare of limited use.
- Use enhanced
getoptfrom util-linux or formerly GNU glibc.1 - It works with
getopt_long()the C function of GNU glibc. - no other solution on this page can do all this:
- handles spaces, quoting characters and even binary in arguments2 (non-enhanced
getoptcan’t do this) - it can handle options at the end:
script.sh -o outFile file1 file2 -v(getoptsdoesn’t do this) - allows
=-style long options:script.sh --outfile=fileOut --infile fileIn(allowing both is lengthy if self parsing) - allows combined short options, e.g.
-vfd(real work if self parsing) - allows touching option-arguments, e.g.
-oOutfileor-vfdoOutfile
- handles spaces, quoting characters and even binary in arguments2 (non-enhanced
- Is so old already3 that it comes preinstalled on any GNU system (i.e. Linux mostly); see footnote1
- You can test for its existence with:
getopt --test→ return value 4. - Other
getoptor shell-builtingetoptsare of limited use.
- Use enhanced
getoptfrom util-linux or formerly GNU glibc.1 - It works with
getopt_long()the C function of GNU glibc. - no other solution on this page can do all this:
- handles spaces, quoting characters and even binary in arguments2 (this rules non-enhanced
getoptout) - it can handle options at the end:
script.sh -o outFile file1 file2 -v(this rulesgetoptsout) - allows
=-style long options:script.sh --outfile=fileOut --infile fileIn(allowing both at the same time makes it really lengthy when self parsing) - allows combined short options, e.g.
-vfd(together with the one before this practically rules out self parsing) - allows touching option-arguments, e.g.
-oOutfileor-vfdoOutfile(you still want to program it yourself?)
- handles spaces, quoting characters and even binary in arguments2 (this rules non-enhanced
- Is so old already3 that it comes preinstalled on any GNU system (i.e. Linux mostly); see footnote1
- You can test for its existence with:
getopt --test→ return value 4. - Other
getoptor shell-builtingetoptsare of limited use.
#!/bin/bash
# More safety, by turning some bugs into errors.
set -o errexit -o pipefail -o noclobber -o nounset
# ignore errexit with `&& true`
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
# -if getopt fails, it complains itself to stdoutstderr
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@") || exit 2
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
#!/bin/bash
# More safety, by turning some bugs into errors.
set -o errexit -o pipefail -o noclobber -o nounset
# ignore errexit with `&& true`
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
# -if getopt fails, it complains itself to stdout
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@") || exit 2
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
#!/bin/bash
# More safety, by turning some bugs into errors.
set -o errexit -o pipefail -o noclobber -o nounset
# ignore errexit with `&& true`
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
# -if getopt fails, it complains itself to stderr
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@") || exit 2
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
Robert Siemer
- 35.2k
- 11
- 89
- 101
#!/bin/bash
# More safety, by turning some bugs into errors.
# Without `errexit` you don’t need ! and can replace
# ${PIPESTATUS[0]} with a simple $?, but I prefer safety.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !’s side effect onignore errexit
# -use return value from ${PIPESTATUS[0]}, because !with hosed`&& $?true`
! getopt --test > /dev/null && true
if [[ ${PIPESTATUS[0]}? -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
!# -if getopt fails, it complains itself to stdout
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
|| exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
#!/bin/bash
# More safety, by turning some bugs into errors.
# Without `errexit` you don’t need ! and can replace
# ${PIPESTATUS[0]} with a simple $?, but I prefer safety.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
#!/bin/bash
# More safety, by turning some bugs into errors.
set -o errexit -o pipefail -o noclobber -o nounset
# ignore errexit with `&& true`
getopt --test > /dev/null && true
if [[ $? -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
# option --output/-o requires 1 argument
LONGOPTS=debug,force,output:,verbose
OPTIONS=dfo:v
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out "--options")
# -pass arguments only via -- "$@" to separate them correctly
# -if getopt fails, it complains itself to stdout
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "0ドル" -- "$@") || exit 2
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
d=n f=n v=n outFile=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "1ドル" in
-d|--debug)
d=y
shift
;;
-f|--force)
f=y
shift
;;
-v|--verbose)
v=y
shift
;;
-o|--output)
outFile="2ドル"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "0ドル: A single input file is required."
exit 4
fi
echo "verbose: $v, force: $f, debug: $d, in: 1,ドル out: $outFile"
Loading
Make the answer to be an answer, not a criticism of other answers. Make the text clearer.
VasiliNovikov
- 10.5k
- 5
- 53
- 65
Loading
rectifying defacing edit: OS X stays in the footnote exclusively, links have the same style incl. in markdown, do not spread OS X install instructions in multiple places, the preinstalled advantage does not apply to OS X either
Robert Siemer
- 35.2k
- 11
- 89
- 101
Loading
adding https://formulae.brew.sh/formula/util-linux to appropriate footnote area
Johnny Utahh
- 2.5k
- 3
- 29
- 46
Loading
Johnny Utahh
- 2.5k
- 3
- 29
- 46
Loading
Loading
Loading
reinstate and improve _how_ this answer compares to the others and _why_ it is better
Robert Siemer
- 35.2k
- 11
- 89
- 101
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
improve script by including errexit and fixing bugs detected by that option
Robert Siemer
- 35.2k
- 11
- 89
- 101
Loading
Loading
Loading
Loading
Robert Siemer
- 35.2k
- 11
- 89
- 101
Loading
add equal-sign for option arguments for clarity (that the option eats an argument)
Robert Siemer
- 35.2k
- 11
- 89
- 101
Loading
Loading
lang-bash