getopts in function that is called more than once in a script, getopts doesn't detect any opts after 1st...
The instant an accelerating object has zero speed, is it speeding up, slowing down, or neither?
How Linux command "mount -a" works
High-end PC graphics circa 1990?
How to prevent cables getting intertwined
Why is Skinner so awkward in Hot Fuzz?
Does knowing the surface area of all faces uniquely determine a tetrahedron?
How to ask if I can mow my neighbor's lawn
Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?
How did the European Union reach the figure of 3% as a maximum allowed deficit?
Why is gun control associated with the socially liberal Democratic party?
Leaving job close to major deadlines
Does anyone recognize these rockets, and their location?
Is there a term for someone whose preferred policies are a mix of Left and Right?
How do I run a script as sudo at boot time on Ubuntu 18.04 Server?
Using roof rails to set up hammock
Is there a risk to write an invitation letter for a stranger to obtain a Czech (Schengen) visa?
How to make a villain when your PCs are villains?
How do I become a better writer when I hate reading?
...and then she held the gun
Class to generate a pdf invoice
How to avoid offending original culture when making conculture inspired from original
How can I detect if I'm in a subshell?
How can Caller ID be faked?
Right indicator flash-frequency has increased and rear-right bulb is out
getopts in function that is called more than once in a script, getopts doesn't detect any opts after 1st function call
Can the empty spaces/background in a terminal be replaced with a random(but pretty) pattern of ASCII characters?resume running a script after function callBash: A function-like structure that doesn't require a call?Control a bash instance by a script (in a robust way)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have read the getopts man page and was not able to find this use case.
I am at a loss for why getopts is not detecting any available options the second time a function is called in the same script.
As you can see from my debug echo outputs, all of the positional params $@ are present for both function calls.
In the second create_db function call, the getopts while loop is never entered, leading my variables TYPE and ENVIRON to never be set.
Any thoughts?
FUNCTION DEFINITION (create_db)
function create_db {
local TYPE SIZE ENVIRON
TYPE=''
SIZE=''
ENVIRON=''
print_usage() {
echo -e $"nUsage: create_db -t {mysql|redis|rabbitmq|sftp|elasticsearch} -e <environment_name> -s <size_in_GB>"
echo "Required args: -t, -e"
echo "Optional args: -s"
}
echo "@: $@"
echo "0: $0"
echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
# parse flags
while getopts 't:s:e:h' flag; do
echo "flag: $flag"
echo "opt: ${OPTARG}"
case "${flag}" in
t) TYPE="${OPTARG}" ;;
s) SIZE="${OPTARG}" ;;
e) ENVIRON="${OPTARG}" ;;
h) print_usage
exit 0 ;;
*) print_usage >&2
exit 1 ;;
esac
done
shift "$(( OPTIND - 1 ))"
echo "TYPE: ${TYPE}"
echo "ENVIRON: ${ENVIRON}"
... DO WORK ...
}
CALLED SCRIPT (environment-setup-from-scratch.sh)
#!/bin/bash
# import functions from utils file
. "${0%/*}/environment-setup-utils.sh"
ENVIRONMENT="${1}"
create_db -t "elasticsearch" -e "${ENVIRONMENT}"
create_db -t "mysql" -e "${ENVIRONMENT}"
create_db -t "redis" -e "${ENVIRONMENT}"
TERMINAL OUTPUT
$ ./environment-setup-from-scratch.sh sandbox
@: -t elasticsearch -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: elasticsearch
3: -e
4: sandbox
5:
6:
flag: t
opt: elasticsearch
flag: e
opt: sandbox
TYPE: elasticsearch
ENVIRON: sandbox
@: -t mysql -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: mysql
3: -e
4: sandbox
5:
6:
TYPE:
ENVIRON:
bash shell-script scripting terminal getopts
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have read the getopts man page and was not able to find this use case.
I am at a loss for why getopts is not detecting any available options the second time a function is called in the same script.
As you can see from my debug echo outputs, all of the positional params $@ are present for both function calls.
In the second create_db function call, the getopts while loop is never entered, leading my variables TYPE and ENVIRON to never be set.
Any thoughts?
FUNCTION DEFINITION (create_db)
function create_db {
local TYPE SIZE ENVIRON
TYPE=''
SIZE=''
ENVIRON=''
print_usage() {
echo -e $"nUsage: create_db -t {mysql|redis|rabbitmq|sftp|elasticsearch} -e <environment_name> -s <size_in_GB>"
echo "Required args: -t, -e"
echo "Optional args: -s"
}
echo "@: $@"
echo "0: $0"
echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
# parse flags
while getopts 't:s:e:h' flag; do
echo "flag: $flag"
echo "opt: ${OPTARG}"
case "${flag}" in
t) TYPE="${OPTARG}" ;;
s) SIZE="${OPTARG}" ;;
e) ENVIRON="${OPTARG}" ;;
h) print_usage
exit 0 ;;
*) print_usage >&2
exit 1 ;;
esac
done
shift "$(( OPTIND - 1 ))"
echo "TYPE: ${TYPE}"
echo "ENVIRON: ${ENVIRON}"
... DO WORK ...
}
CALLED SCRIPT (environment-setup-from-scratch.sh)
#!/bin/bash
# import functions from utils file
. "${0%/*}/environment-setup-utils.sh"
ENVIRONMENT="${1}"
create_db -t "elasticsearch" -e "${ENVIRONMENT}"
create_db -t "mysql" -e "${ENVIRONMENT}"
create_db -t "redis" -e "${ENVIRONMENT}"
TERMINAL OUTPUT
$ ./environment-setup-from-scratch.sh sandbox
@: -t elasticsearch -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: elasticsearch
3: -e
4: sandbox
5:
6:
flag: t
opt: elasticsearch
flag: e
opt: sandbox
TYPE: elasticsearch
ENVIRON: sandbox
@: -t mysql -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: mysql
3: -e
4: sandbox
5:
6:
TYPE:
ENVIRON:
bash shell-script scripting terminal getopts
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have read the getopts man page and was not able to find this use case.
I am at a loss for why getopts is not detecting any available options the second time a function is called in the same script.
As you can see from my debug echo outputs, all of the positional params $@ are present for both function calls.
In the second create_db function call, the getopts while loop is never entered, leading my variables TYPE and ENVIRON to never be set.
Any thoughts?
FUNCTION DEFINITION (create_db)
function create_db {
local TYPE SIZE ENVIRON
TYPE=''
SIZE=''
ENVIRON=''
print_usage() {
echo -e $"nUsage: create_db -t {mysql|redis|rabbitmq|sftp|elasticsearch} -e <environment_name> -s <size_in_GB>"
echo "Required args: -t, -e"
echo "Optional args: -s"
}
echo "@: $@"
echo "0: $0"
echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
# parse flags
while getopts 't:s:e:h' flag; do
echo "flag: $flag"
echo "opt: ${OPTARG}"
case "${flag}" in
t) TYPE="${OPTARG}" ;;
s) SIZE="${OPTARG}" ;;
e) ENVIRON="${OPTARG}" ;;
h) print_usage
exit 0 ;;
*) print_usage >&2
exit 1 ;;
esac
done
shift "$(( OPTIND - 1 ))"
echo "TYPE: ${TYPE}"
echo "ENVIRON: ${ENVIRON}"
... DO WORK ...
}
CALLED SCRIPT (environment-setup-from-scratch.sh)
#!/bin/bash
# import functions from utils file
. "${0%/*}/environment-setup-utils.sh"
ENVIRONMENT="${1}"
create_db -t "elasticsearch" -e "${ENVIRONMENT}"
create_db -t "mysql" -e "${ENVIRONMENT}"
create_db -t "redis" -e "${ENVIRONMENT}"
TERMINAL OUTPUT
$ ./environment-setup-from-scratch.sh sandbox
@: -t elasticsearch -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: elasticsearch
3: -e
4: sandbox
5:
6:
flag: t
opt: elasticsearch
flag: e
opt: sandbox
TYPE: elasticsearch
ENVIRON: sandbox
@: -t mysql -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: mysql
3: -e
4: sandbox
5:
6:
TYPE:
ENVIRON:
bash shell-script scripting terminal getopts
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have read the getopts man page and was not able to find this use case.
I am at a loss for why getopts is not detecting any available options the second time a function is called in the same script.
As you can see from my debug echo outputs, all of the positional params $@ are present for both function calls.
In the second create_db function call, the getopts while loop is never entered, leading my variables TYPE and ENVIRON to never be set.
Any thoughts?
FUNCTION DEFINITION (create_db)
function create_db {
local TYPE SIZE ENVIRON
TYPE=''
SIZE=''
ENVIRON=''
print_usage() {
echo -e $"nUsage: create_db -t {mysql|redis|rabbitmq|sftp|elasticsearch} -e <environment_name> -s <size_in_GB>"
echo "Required args: -t, -e"
echo "Optional args: -s"
}
echo "@: $@"
echo "0: $0"
echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
# parse flags
while getopts 't:s:e:h' flag; do
echo "flag: $flag"
echo "opt: ${OPTARG}"
case "${flag}" in
t) TYPE="${OPTARG}" ;;
s) SIZE="${OPTARG}" ;;
e) ENVIRON="${OPTARG}" ;;
h) print_usage
exit 0 ;;
*) print_usage >&2
exit 1 ;;
esac
done
shift "$(( OPTIND - 1 ))"
echo "TYPE: ${TYPE}"
echo "ENVIRON: ${ENVIRON}"
... DO WORK ...
}
CALLED SCRIPT (environment-setup-from-scratch.sh)
#!/bin/bash
# import functions from utils file
. "${0%/*}/environment-setup-utils.sh"
ENVIRONMENT="${1}"
create_db -t "elasticsearch" -e "${ENVIRONMENT}"
create_db -t "mysql" -e "${ENVIRONMENT}"
create_db -t "redis" -e "${ENVIRONMENT}"
TERMINAL OUTPUT
$ ./environment-setup-from-scratch.sh sandbox
@: -t elasticsearch -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: elasticsearch
3: -e
4: sandbox
5:
6:
flag: t
opt: elasticsearch
flag: e
opt: sandbox
TYPE: elasticsearch
ENVIRON: sandbox
@: -t mysql -e sandbox
0: ./environment-setup-from-scratch.sh
1: -t
2: mysql
3: -e
4: sandbox
5:
6:
TYPE:
ENVIRON:
bash shell-script scripting terminal getopts
bash shell-script scripting terminal getopts
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
Ryan MahaffeyRyan Mahaffey
82
82
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ryan Mahaffey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Each time you call getopts, it uses $OPTIND;
If the application sets OPTIND to the value 1, a new set of
parameters can be used: either the current positional parameters or
new arg values. Any other attempt to invoke getopts multiple times in
a single shell execution environment with parameters (positional
parameters or arg operands) that are not the same in all invocations,
or with an OPTIND value modified to be a value other than 1, produces
unspecified results.
(my emphasis). You need to reset OPTIND before you call getopts each time, perhaps here:
# ...
# parse flags
OPTIND=1
while getopts 't:s:e:h' flag; do
# ...
Thank you so much, perfect explanation. Initially thought myshift "$(( OPTIND - 1 ))"would do the trick but I am guessing the scope does not work due to the subshell
– Ryan Mahaffey
42 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Ryan Mahaffey 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%2f525048%2fgetopts-in-function-that-is-called-more-than-once-in-a-script-getopts-doesnt-d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Each time you call getopts, it uses $OPTIND;
If the application sets OPTIND to the value 1, a new set of
parameters can be used: either the current positional parameters or
new arg values. Any other attempt to invoke getopts multiple times in
a single shell execution environment with parameters (positional
parameters or arg operands) that are not the same in all invocations,
or with an OPTIND value modified to be a value other than 1, produces
unspecified results.
(my emphasis). You need to reset OPTIND before you call getopts each time, perhaps here:
# ...
# parse flags
OPTIND=1
while getopts 't:s:e:h' flag; do
# ...
Thank you so much, perfect explanation. Initially thought myshift "$(( OPTIND - 1 ))"would do the trick but I am guessing the scope does not work due to the subshell
– Ryan Mahaffey
42 mins ago
add a comment |
Each time you call getopts, it uses $OPTIND;
If the application sets OPTIND to the value 1, a new set of
parameters can be used: either the current positional parameters or
new arg values. Any other attempt to invoke getopts multiple times in
a single shell execution environment with parameters (positional
parameters or arg operands) that are not the same in all invocations,
or with an OPTIND value modified to be a value other than 1, produces
unspecified results.
(my emphasis). You need to reset OPTIND before you call getopts each time, perhaps here:
# ...
# parse flags
OPTIND=1
while getopts 't:s:e:h' flag; do
# ...
Thank you so much, perfect explanation. Initially thought myshift "$(( OPTIND - 1 ))"would do the trick but I am guessing the scope does not work due to the subshell
– Ryan Mahaffey
42 mins ago
add a comment |
Each time you call getopts, it uses $OPTIND;
If the application sets OPTIND to the value 1, a new set of
parameters can be used: either the current positional parameters or
new arg values. Any other attempt to invoke getopts multiple times in
a single shell execution environment with parameters (positional
parameters or arg operands) that are not the same in all invocations,
or with an OPTIND value modified to be a value other than 1, produces
unspecified results.
(my emphasis). You need to reset OPTIND before you call getopts each time, perhaps here:
# ...
# parse flags
OPTIND=1
while getopts 't:s:e:h' flag; do
# ...
Each time you call getopts, it uses $OPTIND;
If the application sets OPTIND to the value 1, a new set of
parameters can be used: either the current positional parameters or
new arg values. Any other attempt to invoke getopts multiple times in
a single shell execution environment with parameters (positional
parameters or arg operands) that are not the same in all invocations,
or with an OPTIND value modified to be a value other than 1, produces
unspecified results.
(my emphasis). You need to reset OPTIND before you call getopts each time, perhaps here:
# ...
# parse flags
OPTIND=1
while getopts 't:s:e:h' flag; do
# ...
edited 42 mins ago
answered 1 hour ago
Jeff Schaller♦Jeff Schaller
46.9k1167152
46.9k1167152
Thank you so much, perfect explanation. Initially thought myshift "$(( OPTIND - 1 ))"would do the trick but I am guessing the scope does not work due to the subshell
– Ryan Mahaffey
42 mins ago
add a comment |
Thank you so much, perfect explanation. Initially thought myshift "$(( OPTIND - 1 ))"would do the trick but I am guessing the scope does not work due to the subshell
– Ryan Mahaffey
42 mins ago
Thank you so much, perfect explanation. Initially thought my
shift "$(( OPTIND - 1 ))" would do the trick but I am guessing the scope does not work due to the subshell– Ryan Mahaffey
42 mins ago
Thank you so much, perfect explanation. Initially thought my
shift "$(( OPTIND - 1 ))" would do the trick but I am guessing the scope does not work due to the subshell– Ryan Mahaffey
42 mins ago
add a comment |
Ryan Mahaffey is a new contributor. Be nice, and check out our Code of Conduct.
Ryan Mahaffey is a new contributor. Be nice, and check out our Code of Conduct.
Ryan Mahaffey is a new contributor. Be nice, and check out our Code of Conduct.
Ryan Mahaffey is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f525048%2fgetopts-in-function-that-is-called-more-than-once-in-a-script-getopts-doesnt-d%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