Questions about understanding a template of using bash's getopts
up vote
0
down vote
favorite
A stackoverflow post has a template for handling command line arguments.
Does the test [ $# == 0 ]
mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.
In the case
statement, how different are the two cases *)
and "?")
? They seem the same.
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
bash shell-script getopts
New contributor
add a comment |
up vote
0
down vote
favorite
A stackoverflow post has a template for handling command line arguments.
Does the test [ $# == 0 ]
mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.
In the case
statement, how different are the two cases *)
and "?")
? They seem the same.
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
bash shell-script getopts
New contributor
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
A stackoverflow post has a template for handling command line arguments.
Does the test [ $# == 0 ]
mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.
In the case
statement, how different are the two cases *)
and "?")
? They seem the same.
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
bash shell-script getopts
New contributor
A stackoverflow post has a template for handling command line arguments.
Does the test [ $# == 0 ]
mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.
In the case
statement, how different are the two cases *)
and "?")
? They seem the same.
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
bash shell-script getopts
bash shell-script getopts
New contributor
New contributor
New contributor
asked Nov 22 at 12:32
Ben
2789
2789
New contributor
New contributor
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43
add a comment |
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2
as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i
.
"?", vs *
Yes they are different:
"?"
says to case to look for a?
. This is what getopts returns when it finds an option that it does not expect (invalid option).
*
says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).
add a comment |
up vote
2
down vote
They should be the same.
However that code has its own share of problems:
- failure to quote arithmetic expansions; should be
shift "$(($OPTIND - 1))"
- usage of the unportable
==
operator - error messages written to stdout
- exit with 0 (success) status in case of error
- failure to quote variables; should be
"$#"
andecho "$USAGE"
- bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by
script --
- useless quoting of
v
,h
,i
and:i:vh
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2
as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i
.
"?", vs *
Yes they are different:
"?"
says to case to look for a?
. This is what getopts returns when it finds an option that it does not expect (invalid option).
*
says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).
add a comment |
up vote
2
down vote
accepted
This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2
as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i
.
"?", vs *
Yes they are different:
"?"
says to case to look for a?
. This is what getopts returns when it finds an option that it does not expect (invalid option).
*
says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2
as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i
.
"?", vs *
Yes they are different:
"?"
says to case to look for a?
. This is what getopts returns when it finds an option that it does not expect (invalid option).
*
says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).
This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2
as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i
.
"?", vs *
Yes they are different:
"?"
says to case to look for a?
. This is what getopts returns when it finds an option that it does not expect (invalid option).
*
says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).
answered Nov 22 at 12:48
ctrl-alt-delor
10.2k41955
10.2k41955
add a comment |
add a comment |
up vote
2
down vote
They should be the same.
However that code has its own share of problems:
- failure to quote arithmetic expansions; should be
shift "$(($OPTIND - 1))"
- usage of the unportable
==
operator - error messages written to stdout
- exit with 0 (success) status in case of error
- failure to quote variables; should be
"$#"
andecho "$USAGE"
- bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by
script --
- useless quoting of
v
,h
,i
and:i:vh
add a comment |
up vote
2
down vote
They should be the same.
However that code has its own share of problems:
- failure to quote arithmetic expansions; should be
shift "$(($OPTIND - 1))"
- usage of the unportable
==
operator - error messages written to stdout
- exit with 0 (success) status in case of error
- failure to quote variables; should be
"$#"
andecho "$USAGE"
- bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by
script --
- useless quoting of
v
,h
,i
and:i:vh
add a comment |
up vote
2
down vote
up vote
2
down vote
They should be the same.
However that code has its own share of problems:
- failure to quote arithmetic expansions; should be
shift "$(($OPTIND - 1))"
- usage of the unportable
==
operator - error messages written to stdout
- exit with 0 (success) status in case of error
- failure to quote variables; should be
"$#"
andecho "$USAGE"
- bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by
script --
- useless quoting of
v
,h
,i
and:i:vh
They should be the same.
However that code has its own share of problems:
- failure to quote arithmetic expansions; should be
shift "$(($OPTIND - 1))"
- usage of the unportable
==
operator - error messages written to stdout
- exit with 0 (success) status in case of error
- failure to quote variables; should be
"$#"
andecho "$USAGE"
- bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by
script --
- useless quoting of
v
,h
,i
and:i:vh
edited Nov 23 at 16:38
answered Nov 22 at 16:13
mosvy
4,831322
4,831322
add a comment |
add a comment |
Ben is a new contributor. Be nice, and check out our Code of Conduct.
Ben is a new contributor. Be nice, and check out our Code of Conduct.
Ben is a new contributor. Be nice, and check out our Code of Conduct.
Ben is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483438%2fquestions-about-understanding-a-template-of-using-bashs-getopts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43