Bash parameters not detectedGet the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Difference between sh and bash
How to denote matrix elements succinctly?
Does a large simulator bay have standard public address announcements?
Function pointer with named arguments?
How to not starve gigantic beasts
Multiple options vs single option UI
How to fry ground beef so it is well-browned
Discriminated by senior researcher because of my ethnicity
How did Captain America manage to do this?
"Hidden" theta-term in Hamiltonian formulation of Yang-Mills theory
Contradiction proof for inequality of P and NP?
Why did some of my point & shoot film photos come back with one third light white or orange?
Which big number is bigger?
"The cow" OR "a cow" OR "cows" in this context
How can I print the prosodic symbols in LaTeX?
A Note on N!
Is the claim "Employers won't employ people with no 'social media presence'" realistic?
What happens to Mjolnir (Thor's hammer) at the end of Endgame?
What's the polite way to say "I need to urinate"?
How do I reattach a shelf to the wall when it ripped out of the wall?
How can I get this effect? Please see the attached image
Rivers without rain
Is it idiomatic to construct against `this`
Can we say “you can pay when the order gets ready”?
Could the terminal length of components like resistors be reduced?
Bash parameters not detected
Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Difference between sh and bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When I attempt to execute my bash script like this:
bin/provision-pulsar-commands -t development -n provisioning
the outputs print:
TENANT =
NAMESPACE =
Here is my script (provision-pulsar-commands):
#!/bin/bash
function display_usage
echo "You may override the namespace and tenant for all components for testing, if desired."
echo "usage: bin/provision-pulsar-commands [-t tenant] [-n namespace]"
echo " -t Override tenant (e.g. development) for all components"
echo " -n Override namespace (e.g. provisioning) for all components"
echo " -h display help"
exit 1
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
while getopts tn option
do
case "$option"
in
t) TENANT=$OPTARG;;
n) NAMESPACE=$OPTARG;;
esac
done
echo "TENANT = $TENANT"
echo "NAMESPACE = $NAMESPACE"
Why are my parameter values not getting picked up?
I'm basing my code on these examples:
- https://www.lifewire.com/pass-arguments-to-bash-script-2200571
- https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
Clarification: My parameter values are optional. Also, when I pass -h or --help, my display_usage function is not called. It's not clear to me if that's related to the problem or not.
bash shell sh
add a comment |
When I attempt to execute my bash script like this:
bin/provision-pulsar-commands -t development -n provisioning
the outputs print:
TENANT =
NAMESPACE =
Here is my script (provision-pulsar-commands):
#!/bin/bash
function display_usage
echo "You may override the namespace and tenant for all components for testing, if desired."
echo "usage: bin/provision-pulsar-commands [-t tenant] [-n namespace]"
echo " -t Override tenant (e.g. development) for all components"
echo " -n Override namespace (e.g. provisioning) for all components"
echo " -h display help"
exit 1
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
while getopts tn option
do
case "$option"
in
t) TENANT=$OPTARG;;
n) NAMESPACE=$OPTARG;;
esac
done
echo "TENANT = $TENANT"
echo "NAMESPACE = $NAMESPACE"
Why are my parameter values not getting picked up?
I'm basing my code on these examples:
- https://www.lifewire.com/pass-arguments-to-bash-script-2200571
- https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
Clarification: My parameter values are optional. Also, when I pass -h or --help, my display_usage function is not called. It's not clear to me if that's related to the problem or not.
bash shell sh
The test[[ ( $# == "--help") || $# == "-h" ]]will not work --$#is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want$1instead. Also, the parentheses are not needed here.
– Gordon Davisson
Mar 22 at 17:45
add a comment |
When I attempt to execute my bash script like this:
bin/provision-pulsar-commands -t development -n provisioning
the outputs print:
TENANT =
NAMESPACE =
Here is my script (provision-pulsar-commands):
#!/bin/bash
function display_usage
echo "You may override the namespace and tenant for all components for testing, if desired."
echo "usage: bin/provision-pulsar-commands [-t tenant] [-n namespace]"
echo " -t Override tenant (e.g. development) for all components"
echo " -n Override namespace (e.g. provisioning) for all components"
echo " -h display help"
exit 1
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
while getopts tn option
do
case "$option"
in
t) TENANT=$OPTARG;;
n) NAMESPACE=$OPTARG;;
esac
done
echo "TENANT = $TENANT"
echo "NAMESPACE = $NAMESPACE"
Why are my parameter values not getting picked up?
I'm basing my code on these examples:
- https://www.lifewire.com/pass-arguments-to-bash-script-2200571
- https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
Clarification: My parameter values are optional. Also, when I pass -h or --help, my display_usage function is not called. It's not clear to me if that's related to the problem or not.
bash shell sh
When I attempt to execute my bash script like this:
bin/provision-pulsar-commands -t development -n provisioning
the outputs print:
TENANT =
NAMESPACE =
Here is my script (provision-pulsar-commands):
#!/bin/bash
function display_usage
echo "You may override the namespace and tenant for all components for testing, if desired."
echo "usage: bin/provision-pulsar-commands [-t tenant] [-n namespace]"
echo " -t Override tenant (e.g. development) for all components"
echo " -n Override namespace (e.g. provisioning) for all components"
echo " -h display help"
exit 1
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
while getopts tn option
do
case "$option"
in
t) TENANT=$OPTARG;;
n) NAMESPACE=$OPTARG;;
esac
done
echo "TENANT = $TENANT"
echo "NAMESPACE = $NAMESPACE"
Why are my parameter values not getting picked up?
I'm basing my code on these examples:
- https://www.lifewire.com/pass-arguments-to-bash-script-2200571
- https://www.poftut.com/how-to-pass-and-parse-linux-bash-script-arguments-and-parameters/
Clarification: My parameter values are optional. Also, when I pass -h or --help, my display_usage function is not called. It's not clear to me if that's related to the problem or not.
bash shell sh
bash shell sh
edited Mar 22 at 17:33
devinbost
asked Mar 22 at 17:22
devinbostdevinbost
1,8821926
1,8821926
The test[[ ( $# == "--help") || $# == "-h" ]]will not work --$#is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want$1instead. Also, the parentheses are not needed here.
– Gordon Davisson
Mar 22 at 17:45
add a comment |
The test[[ ( $# == "--help") || $# == "-h" ]]will not work --$#is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want$1instead. Also, the parentheses are not needed here.
– Gordon Davisson
Mar 22 at 17:45
The test
[[ ( $# == "--help") || $# == "-h" ]] will not work -- $# is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want $1 instead. Also, the parentheses are not needed here.– Gordon Davisson
Mar 22 at 17:45
The test
[[ ( $# == "--help") || $# == "-h" ]] will not work -- $# is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want $1 instead. Also, the parentheses are not needed here.– Gordon Davisson
Mar 22 at 17:45
add a comment |
2 Answers
2
active
oldest
votes
You have to tell getopts that -t and -n take arguments.
while getopts t:n: option; do
Without the colons, -t is recognized as an option, but OPTARG isn't set. The next argument (development) is neither -t nor -n, so terminates the loop.
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getoptsdoes not support options that take optional values. For example,-teither allows no argument, or it requires a single argument.
– chepner
Mar 22 at 17:35
$#is the number of arguments; it is not a particular argument in$@. You need to addhto your optstring and treat it like any other option.
– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment aboutgetopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.
– devinbost
Mar 22 at 17:37
add a comment |
Add : to options that take arguments.
getopts t:n: option
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);
);
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%2fstackoverflow.com%2fquestions%2f55304824%2fbash-parameters-not-detected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to tell getopts that -t and -n take arguments.
while getopts t:n: option; do
Without the colons, -t is recognized as an option, but OPTARG isn't set. The next argument (development) is neither -t nor -n, so terminates the loop.
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getoptsdoes not support options that take optional values. For example,-teither allows no argument, or it requires a single argument.
– chepner
Mar 22 at 17:35
$#is the number of arguments; it is not a particular argument in$@. You need to addhto your optstring and treat it like any other option.
– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment aboutgetopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.
– devinbost
Mar 22 at 17:37
add a comment |
You have to tell getopts that -t and -n take arguments.
while getopts t:n: option; do
Without the colons, -t is recognized as an option, but OPTARG isn't set. The next argument (development) is neither -t nor -n, so terminates the loop.
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getoptsdoes not support options that take optional values. For example,-teither allows no argument, or it requires a single argument.
– chepner
Mar 22 at 17:35
$#is the number of arguments; it is not a particular argument in$@. You need to addhto your optstring and treat it like any other option.
– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment aboutgetopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.
– devinbost
Mar 22 at 17:37
add a comment |
You have to tell getopts that -t and -n take arguments.
while getopts t:n: option; do
Without the colons, -t is recognized as an option, but OPTARG isn't set. The next argument (development) is neither -t nor -n, so terminates the loop.
You have to tell getopts that -t and -n take arguments.
while getopts t:n: option; do
Without the colons, -t is recognized as an option, but OPTARG isn't set. The next argument (development) is neither -t nor -n, so terminates the loop.
answered Mar 22 at 17:30
chepnerchepner
265k36255346
265k36255346
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getoptsdoes not support options that take optional values. For example,-teither allows no argument, or it requires a single argument.
– chepner
Mar 22 at 17:35
$#is the number of arguments; it is not a particular argument in$@. You need to addhto your optstring and treat it like any other option.
– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment aboutgetopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.
– devinbost
Mar 22 at 17:37
add a comment |
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getoptsdoes not support options that take optional values. For example,-teither allows no argument, or it requires a single argument.
– chepner
Mar 22 at 17:35
$#is the number of arguments; it is not a particular argument in$@. You need to addhto your optstring and treat it like any other option.
– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment aboutgetopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.
– devinbost
Mar 22 at 17:37
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
Does that approach work for optional parameter values? (I clarified my question.)
– devinbost
Mar 22 at 17:33
getopts does not support options that take optional values. For example, -t either allows no argument, or it requires a single argument.– chepner
Mar 22 at 17:35
getopts does not support options that take optional values. For example, -t either allows no argument, or it requires a single argument.– chepner
Mar 22 at 17:35
$# is the number of arguments; it is not a particular argument in $@. You need to add h to your optstring and treat it like any other option.– chepner
Mar 22 at 17:35
$# is the number of arguments; it is not a particular argument in $@. You need to add h to your optstring and treat it like any other option.– chepner
Mar 22 at 17:35
Thanks for the help! Based on your comment about
getopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.– devinbost
Mar 22 at 17:37
Thanks for the help! Based on your comment about
getopts, it appears that the article I was following (lifewire.com/pass-arguments-to-bash-script-2200571) was incorrect about how to handle optional parameters.– devinbost
Mar 22 at 17:37
add a comment |
Add : to options that take arguments.
getopts t:n: option
add a comment |
Add : to options that take arguments.
getopts t:n: option
add a comment |
Add : to options that take arguments.
getopts t:n: option
Add : to options that take arguments.
getopts t:n: option
answered Mar 22 at 17:29
John KugelmanJohn Kugelman
250k54407461
250k54407461
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55304824%2fbash-parameters-not-detected%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
The test
[[ ( $# == "--help") || $# == "-h" ]]will not work --$#is the number of arguments (e.g. "2"), not the content of any of the arguments. You probably want$1instead. Also, the parentheses are not needed here.– Gordon Davisson
Mar 22 at 17:45