Bash throws error, line 8: $1: unbound variable

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I feel really stupid, but I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage percentages. The problem is that one of my bash functions does not seem to like that I reference $1 as an variable within the function. The reason I reference $1 is because the get_percent function can be passed a mount point as an optional argument to display instead of all of the mount points.
The script
#!/usr/bin/bash
set -e
set -u
set -o pipefail
get_percent()
if [ -n "$1" ]
then
df -h $1
usage()
echo "script usage: $(basename $0) [-h] [-p] [-m mount_point]" >&2
# If the user doesn't supply any arguments, we run the script as normal
if [ $# -eq 0 ];
then
get_percent
exit 0
fi
# ...
The Output
$ bash thing.sh
thing.sh: line 8: $1: unbound variable
$ bash -x thing.sh
+ set -e
+ set -u
+ set -o pipefail
+ '[' 0 -eq 0 ']'
+ get_percent
thing.sh: line 8: $1: unbound variable
bash shell-script scripting
add a comment |Â
up vote
0
down vote
favorite
I feel really stupid, but I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage percentages. The problem is that one of my bash functions does not seem to like that I reference $1 as an variable within the function. The reason I reference $1 is because the get_percent function can be passed a mount point as an optional argument to display instead of all of the mount points.
The script
#!/usr/bin/bash
set -e
set -u
set -o pipefail
get_percent()
if [ -n "$1" ]
then
df -h $1
usage()
echo "script usage: $(basename $0) [-h] [-p] [-m mount_point]" >&2
# If the user doesn't supply any arguments, we run the script as normal
if [ $# -eq 0 ];
then
get_percent
exit 0
fi
# ...
The Output
$ bash thing.sh
thing.sh: line 8: $1: unbound variable
$ bash -x thing.sh
+ set -e
+ set -u
+ set -o pipefail
+ '[' 0 -eq 0 ']'
+ get_percent
thing.sh: line 8: $1: unbound variable
bash shell-script scripting
I don't think this has anything to do withgetopts, does it? Your script exits due to-ubefore callinggetopts.
â ilkkachu
Aug 16 at 18:31
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I feel really stupid, but I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage percentages. The problem is that one of my bash functions does not seem to like that I reference $1 as an variable within the function. The reason I reference $1 is because the get_percent function can be passed a mount point as an optional argument to display instead of all of the mount points.
The script
#!/usr/bin/bash
set -e
set -u
set -o pipefail
get_percent()
if [ -n "$1" ]
then
df -h $1
usage()
echo "script usage: $(basename $0) [-h] [-p] [-m mount_point]" >&2
# If the user doesn't supply any arguments, we run the script as normal
if [ $# -eq 0 ];
then
get_percent
exit 0
fi
# ...
The Output
$ bash thing.sh
thing.sh: line 8: $1: unbound variable
$ bash -x thing.sh
+ set -e
+ set -u
+ set -o pipefail
+ '[' 0 -eq 0 ']'
+ get_percent
thing.sh: line 8: $1: unbound variable
bash shell-script scripting
I feel really stupid, but I am trying to learn how to use getopts so that I can have scripts with parsed input (although I think getopts could be better). I am trying to just write a simple script to return partition usage percentages. The problem is that one of my bash functions does not seem to like that I reference $1 as an variable within the function. The reason I reference $1 is because the get_percent function can be passed a mount point as an optional argument to display instead of all of the mount points.
The script
#!/usr/bin/bash
set -e
set -u
set -o pipefail
get_percent()
if [ -n "$1" ]
then
df -h $1
usage()
echo "script usage: $(basename $0) [-h] [-p] [-m mount_point]" >&2
# If the user doesn't supply any arguments, we run the script as normal
if [ $# -eq 0 ];
then
get_percent
exit 0
fi
# ...
The Output
$ bash thing.sh
thing.sh: line 8: $1: unbound variable
$ bash -x thing.sh
+ set -e
+ set -u
+ set -o pipefail
+ '[' 0 -eq 0 ']'
+ get_percent
thing.sh: line 8: $1: unbound variable
bash shell-script scripting
bash shell-script scripting
edited Aug 30 at 21:51
Rui F Ribeiro
36.7k1271116
36.7k1271116
asked Aug 16 at 18:00
Timothy Pulliam
1,041617
1,041617
I don't think this has anything to do withgetopts, does it? Your script exits due to-ubefore callinggetopts.
â ilkkachu
Aug 16 at 18:31
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36
add a comment |Â
I don't think this has anything to do withgetopts, does it? Your script exits due to-ubefore callinggetopts.
â ilkkachu
Aug 16 at 18:31
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36
I don't think this has anything to do with
getopts, does it? Your script exits due to -u before calling getopts.â ilkkachu
Aug 16 at 18:31
I don't think this has anything to do with
getopts, does it? Your script exits due to -u before calling getopts.â ilkkachu
Aug 16 at 18:31
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, causing $1 to be unset.
Either check for this before invoking your function, or use default expansions ($1-default will expand to default if not already set to something else).
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use[ -n "$1-" ](that is, with an empty default value) to see if the parameter is set and non-empty; or[ "$1+x" = x ]to see if it's set, even if empty.
â ilkkachu
Aug 16 at 18:30
add a comment |Â
up vote
0
down vote
This is the effect of set -u.
You could check $# inside the function and avoid referencing $1 if it is not set.
add a comment |Â
up vote
0
down vote
Since this is bash you can sidestep the check for $1 being set and just use "$@" (when double-quoted, this parameter disappears completely if it has no values, which avoids it being caught by set -u):
get_percent() awk 'NR>1 printf "%st%sn", $1, $5 '
I've also tweaked the rest of the line slightly so that you don't get spacetabspace between the two values you output but insead you get just a tab. If you really want the two invisible spaces then change the awk to use printf "%s t %sn", $1, $5.
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, causing $1 to be unset.
Either check for this before invoking your function, or use default expansions ($1-default will expand to default if not already set to something else).
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use[ -n "$1-" ](that is, with an empty default value) to see if the parameter is set and non-empty; or[ "$1+x" = x ]to see if it's set, even if empty.
â ilkkachu
Aug 16 at 18:30
add a comment |Â
up vote
4
down vote
accepted
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, causing $1 to be unset.
Either check for this before invoking your function, or use default expansions ($1-default will expand to default if not already set to something else).
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use[ -n "$1-" ](that is, with an empty default value) to see if the parameter is set and non-empty; or[ "$1+x" = x ]to see if it's set, even if empty.
â ilkkachu
Aug 16 at 18:30
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, causing $1 to be unset.
Either check for this before invoking your function, or use default expansions ($1-default will expand to default if not already set to something else).
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, causing $1 to be unset.
Either check for this before invoking your function, or use default expansions ($1-default will expand to default if not already set to something else).
answered Aug 16 at 18:04
DopeGhoti
41k55080
41k55080
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use[ -n "$1-" ](that is, with an empty default value) to see if the parameter is set and non-empty; or[ "$1+x" = x ]to see if it's set, even if empty.
â ilkkachu
Aug 16 at 18:30
add a comment |Â
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use[ -n "$1-" ](that is, with an empty default value) to see if the parameter is set and non-empty; or[ "$1+x" = x ]to see if it's set, even if empty.
â ilkkachu
Aug 16 at 18:30
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
I suspected this, but I couldn't think of a way around it. Default expansion seems to have fixed it. Thank you very much!
â Timothy Pulliam
Aug 16 at 18:10
In particular, one could use
[ -n "$1-" ] (that is, with an empty default value) to see if the parameter is set and non-empty; or [ "$1+x" = x ] to see if it's set, even if empty.â ilkkachu
Aug 16 at 18:30
In particular, one could use
[ -n "$1-" ] (that is, with an empty default value) to see if the parameter is set and non-empty; or [ "$1+x" = x ] to see if it's set, even if empty.â ilkkachu
Aug 16 at 18:30
add a comment |Â
up vote
0
down vote
This is the effect of set -u.
You could check $# inside the function and avoid referencing $1 if it is not set.
add a comment |Â
up vote
0
down vote
This is the effect of set -u.
You could check $# inside the function and avoid referencing $1 if it is not set.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This is the effect of set -u.
You could check $# inside the function and avoid referencing $1 if it is not set.
This is the effect of set -u.
You could check $# inside the function and avoid referencing $1 if it is not set.
answered Aug 16 at 18:04
RalfFriedl
3,7001523
3,7001523
add a comment |Â
add a comment |Â
up vote
0
down vote
Since this is bash you can sidestep the check for $1 being set and just use "$@" (when double-quoted, this parameter disappears completely if it has no values, which avoids it being caught by set -u):
get_percent() awk 'NR>1 printf "%st%sn", $1, $5 '
I've also tweaked the rest of the line slightly so that you don't get spacetabspace between the two values you output but insead you get just a tab. If you really want the two invisible spaces then change the awk to use printf "%s t %sn", $1, $5.
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
add a comment |Â
up vote
0
down vote
Since this is bash you can sidestep the check for $1 being set and just use "$@" (when double-quoted, this parameter disappears completely if it has no values, which avoids it being caught by set -u):
get_percent() awk 'NR>1 printf "%st%sn", $1, $5 '
I've also tweaked the rest of the line slightly so that you don't get spacetabspace between the two values you output but insead you get just a tab. If you really want the two invisible spaces then change the awk to use printf "%s t %sn", $1, $5.
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Since this is bash you can sidestep the check for $1 being set and just use "$@" (when double-quoted, this parameter disappears completely if it has no values, which avoids it being caught by set -u):
get_percent() awk 'NR>1 printf "%st%sn", $1, $5 '
I've also tweaked the rest of the line slightly so that you don't get spacetabspace between the two values you output but insead you get just a tab. If you really want the two invisible spaces then change the awk to use printf "%s t %sn", $1, $5.
Since this is bash you can sidestep the check for $1 being set and just use "$@" (when double-quoted, this parameter disappears completely if it has no values, which avoids it being caught by set -u):
get_percent() awk 'NR>1 printf "%st%sn", $1, $5 '
I've also tweaked the rest of the line slightly so that you don't get spacetabspace between the two values you output but insead you get just a tab. If you really want the two invisible spaces then change the awk to use printf "%s t %sn", $1, $5.
answered Aug 16 at 18:14
roaima
40.4k547110
40.4k547110
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
add a comment |Â
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
I will have to look into this. I'm not familiar with that variable type. Thanks
â Timothy Pulliam
Aug 16 at 18:29
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%2f463034%2fbash-throws-error-line-8-1-unbound-variable%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
I don't think this has anything to do with
getopts, does it? Your script exits due to-ubefore callinggetopts.â ilkkachu
Aug 16 at 18:31
@ikkachu no I guess it doesn't. But I'm not sure I can change the title now.
â Timothy Pulliam
Aug 16 at 18:33
There should be that small "edit" text under the post, just beneath the tags in a question
â ilkkachu
Aug 16 at 18:36