Getopts not working inside of function
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to use getops
in a function yet it doesn't seem to work:
#!/bin/bash
function main()
while getopts ":p:t:c:b:" o; do
case "$o" in
p)
echo "GOt P"
p=$OPTARG
;;
t)
echo "GOt T"
t=$OPTARG
;;
c)
echo "GOt C"
c=$OPTARG
;;
b)
echo "GOt b"
b=$OPTARG
;;
*)
#usage
echo "Unknown Option"
return
;;
esac
done
echo $p
echo $t
echo $c
echo $b
main
And then running it like this:
$ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1
I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?
shell-script
add a comment |Â
up vote
0
down vote
favorite
I'm trying to use getops
in a function yet it doesn't seem to work:
#!/bin/bash
function main()
while getopts ":p:t:c:b:" o; do
case "$o" in
p)
echo "GOt P"
p=$OPTARG
;;
t)
echo "GOt T"
t=$OPTARG
;;
c)
echo "GOt C"
c=$OPTARG
;;
b)
echo "GOt b"
b=$OPTARG
;;
*)
#usage
echo "Unknown Option"
return
;;
esac
done
echo $p
echo $t
echo $c
echo $b
main
And then running it like this:
$ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1
I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?
shell-script
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to use getops
in a function yet it doesn't seem to work:
#!/bin/bash
function main()
while getopts ":p:t:c:b:" o; do
case "$o" in
p)
echo "GOt P"
p=$OPTARG
;;
t)
echo "GOt T"
t=$OPTARG
;;
c)
echo "GOt C"
c=$OPTARG
;;
b)
echo "GOt b"
b=$OPTARG
;;
*)
#usage
echo "Unknown Option"
return
;;
esac
done
echo $p
echo $t
echo $c
echo $b
main
And then running it like this:
$ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1
I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?
shell-script
I'm trying to use getops
in a function yet it doesn't seem to work:
#!/bin/bash
function main()
while getopts ":p:t:c:b:" o; do
case "$o" in
p)
echo "GOt P"
p=$OPTARG
;;
t)
echo "GOt T"
t=$OPTARG
;;
c)
echo "GOt C"
c=$OPTARG
;;
b)
echo "GOt b"
b=$OPTARG
;;
*)
#usage
echo "Unknown Option"
return
;;
esac
done
echo $p
echo $t
echo $c
echo $b
main
And then running it like this:
$ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1
I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?
shell-script
edited Mar 15 at 14:05
galoget
36319
36319
asked Mar 15 at 7:49
Dean
1084
1084
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
You're not passing any argument to your main
function. If you want that function to get the same arguments as passed to the script, pass them along with:
main "$@"
Instead of:
main
Also relevant to your script:
- difference between âÂÂfunction foo() â and âÂÂfoo() âÂÂ
- Why is printf better than echo?
- Security implications of forgetting to quote a variable in bash/POSIX shells
- you'd want to output errors on stderr:
echo >&2 Unknown option
- you'd want to return a non-zero exit status upon error (
return 1
) - when calling
getopts
in a function, it's a good habit to setOPTIND
to 1 initially, in casegetopts
has been called before (for instance in a previous invocation of the function).
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
add a comment |Â
up vote
1
down vote
In a function, the parameters are those passed to the function, not those passed to the script:
$ cat foo.sh
function main ()
echo "$@"
echo "$@"
main
$ bash foo.sh bar
bar
$
You need to pass "$@"
to main
:
main "$@"
Though I find a main
function in scripts rather useless, unless you plain to call main
again and again in the same script.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You're not passing any argument to your main
function. If you want that function to get the same arguments as passed to the script, pass them along with:
main "$@"
Instead of:
main
Also relevant to your script:
- difference between âÂÂfunction foo() â and âÂÂfoo() âÂÂ
- Why is printf better than echo?
- Security implications of forgetting to quote a variable in bash/POSIX shells
- you'd want to output errors on stderr:
echo >&2 Unknown option
- you'd want to return a non-zero exit status upon error (
return 1
) - when calling
getopts
in a function, it's a good habit to setOPTIND
to 1 initially, in casegetopts
has been called before (for instance in a previous invocation of the function).
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
add a comment |Â
up vote
5
down vote
accepted
You're not passing any argument to your main
function. If you want that function to get the same arguments as passed to the script, pass them along with:
main "$@"
Instead of:
main
Also relevant to your script:
- difference between âÂÂfunction foo() â and âÂÂfoo() âÂÂ
- Why is printf better than echo?
- Security implications of forgetting to quote a variable in bash/POSIX shells
- you'd want to output errors on stderr:
echo >&2 Unknown option
- you'd want to return a non-zero exit status upon error (
return 1
) - when calling
getopts
in a function, it's a good habit to setOPTIND
to 1 initially, in casegetopts
has been called before (for instance in a previous invocation of the function).
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You're not passing any argument to your main
function. If you want that function to get the same arguments as passed to the script, pass them along with:
main "$@"
Instead of:
main
Also relevant to your script:
- difference between âÂÂfunction foo() â and âÂÂfoo() âÂÂ
- Why is printf better than echo?
- Security implications of forgetting to quote a variable in bash/POSIX shells
- you'd want to output errors on stderr:
echo >&2 Unknown option
- you'd want to return a non-zero exit status upon error (
return 1
) - when calling
getopts
in a function, it's a good habit to setOPTIND
to 1 initially, in casegetopts
has been called before (for instance in a previous invocation of the function).
You're not passing any argument to your main
function. If you want that function to get the same arguments as passed to the script, pass them along with:
main "$@"
Instead of:
main
Also relevant to your script:
- difference between âÂÂfunction foo() â and âÂÂfoo() âÂÂ
- Why is printf better than echo?
- Security implications of forgetting to quote a variable in bash/POSIX shells
- you'd want to output errors on stderr:
echo >&2 Unknown option
- you'd want to return a non-zero exit status upon error (
return 1
) - when calling
getopts
in a function, it's a good habit to setOPTIND
to 1 initially, in casegetopts
has been called before (for instance in a previous invocation of the function).
edited Mar 15 at 8:03
answered Mar 15 at 7:58
Stéphane Chazelas
280k53515847
280k53515847
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
add a comment |Â
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
@Dean No, functions may have their own command line parsing.
â Kusalananda
Mar 15 at 8:05
add a comment |Â
up vote
1
down vote
In a function, the parameters are those passed to the function, not those passed to the script:
$ cat foo.sh
function main ()
echo "$@"
echo "$@"
main
$ bash foo.sh bar
bar
$
You need to pass "$@"
to main
:
main "$@"
Though I find a main
function in scripts rather useless, unless you plain to call main
again and again in the same script.
add a comment |Â
up vote
1
down vote
In a function, the parameters are those passed to the function, not those passed to the script:
$ cat foo.sh
function main ()
echo "$@"
echo "$@"
main
$ bash foo.sh bar
bar
$
You need to pass "$@"
to main
:
main "$@"
Though I find a main
function in scripts rather useless, unless you plain to call main
again and again in the same script.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In a function, the parameters are those passed to the function, not those passed to the script:
$ cat foo.sh
function main ()
echo "$@"
echo "$@"
main
$ bash foo.sh bar
bar
$
You need to pass "$@"
to main
:
main "$@"
Though I find a main
function in scripts rather useless, unless you plain to call main
again and again in the same script.
In a function, the parameters are those passed to the function, not those passed to the script:
$ cat foo.sh
function main ()
echo "$@"
echo "$@"
main
$ bash foo.sh bar
bar
$
You need to pass "$@"
to main
:
main "$@"
Though I find a main
function in scripts rather useless, unless you plain to call main
again and again in the same script.
answered Mar 15 at 7:59
Olorin
1,15711
1,15711
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430337%2fgetopts-not-working-inside-of-function%23new-answer', 'question_page');
);
Post as a guest
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
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
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