bash scripting problem
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.
This is my script "start" (with line numbering):
#!/bin/bash
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
if $reply == 0
then
echo "NFS mounted OK"
else
echo "Mounting NFS failed: $reply"
fi
reply=sudo mount LABEL="60GB" /home/dk/60GB
if $reply == 0
then
echo "60GB mounted OK"
else
echo "Mounting 60GB failed: $reply"
fi
exit 0
This is the result of running it:
$ ./scripts/start
mount: only root can do that
./start: line 3: ==: command not found
Mounting NFS failed:
mount: only root can do that
./start: line 11: ==: command not found
Mounting 60GB failed:
$
- Why did it ignore the
sudo
beforemount
? - Why did it not consider
if
to be a command ? - Why did it not print the (non-zero) value of
$reply
?
bash
add a comment |Â
up vote
1
down vote
favorite
Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.
This is my script "start" (with line numbering):
#!/bin/bash
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
if $reply == 0
then
echo "NFS mounted OK"
else
echo "Mounting NFS failed: $reply"
fi
reply=sudo mount LABEL="60GB" /home/dk/60GB
if $reply == 0
then
echo "60GB mounted OK"
else
echo "Mounting 60GB failed: $reply"
fi
exit 0
This is the result of running it:
$ ./scripts/start
mount: only root can do that
./start: line 3: ==: command not found
Mounting NFS failed:
mount: only root can do that
./start: line 11: ==: command not found
Mounting 60GB failed:
$
- Why did it ignore the
sudo
beforemount
? - Why did it not consider
if
to be a command ? - Why did it not print the (non-zero) value of
$reply
?
bash
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.
This is my script "start" (with line numbering):
#!/bin/bash
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
if $reply == 0
then
echo "NFS mounted OK"
else
echo "Mounting NFS failed: $reply"
fi
reply=sudo mount LABEL="60GB" /home/dk/60GB
if $reply == 0
then
echo "60GB mounted OK"
else
echo "Mounting 60GB failed: $reply"
fi
exit 0
This is the result of running it:
$ ./scripts/start
mount: only root can do that
./start: line 3: ==: command not found
Mounting NFS failed:
mount: only root can do that
./start: line 11: ==: command not found
Mounting 60GB failed:
$
- Why did it ignore the
sudo
beforemount
? - Why did it not consider
if
to be a command ? - Why did it not print the (non-zero) value of
$reply
?
bash
Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.
This is my script "start" (with line numbering):
#!/bin/bash
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
if $reply == 0
then
echo "NFS mounted OK"
else
echo "Mounting NFS failed: $reply"
fi
reply=sudo mount LABEL="60GB" /home/dk/60GB
if $reply == 0
then
echo "60GB mounted OK"
else
echo "Mounting 60GB failed: $reply"
fi
exit 0
This is the result of running it:
$ ./scripts/start
mount: only root can do that
./start: line 3: ==: command not found
Mounting NFS failed:
mount: only root can do that
./start: line 11: ==: command not found
Mounting 60GB failed:
$
- Why did it ignore the
sudo
beforemount
? - Why did it not consider
if
to be a command ? - Why did it not print the (non-zero) value of
$reply
?
bash
bash
edited 10 mins ago
muru
132k19280478
132k19280478
asked 32 mins ago
Dave Kimble
92
92
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
Well you're assigning output of a command incorrectly. Should be:
reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)
That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...)
, but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))
Why did it not consider "if" to be a command?
If statements operate on commands, but what you give is variable $reply
, which only exists temporarily ( see later in the answer about that) so it exists only for mount
command's environment, but the shell running the script knows nothing of it - it doesn't exist. To compare strings use [
, and yes it's a command also known as test
. So do
if test "$reply" == 0
See quotes? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.
But if you want to check exit status of command, just do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
See no sudo
? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.
I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
then
echo "Success"
else
echo "Fail"
fi
Because of 1>
normal output is suppressed (actually redirected to /dev/null
especially for that purpose) but errors will show if anything goes wrong.
Why did it ignore the "sudo" before "mount" ?
As for why you're getting error message that's because the form
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
is treated as envvariable=value command arg1 arg2
, i.e. you're putting variable result
with value sudo
into environment of mount
command. The command will know about it, but after command exits - the variable is gone too.
Why did it not print the (non-zero) value of $reply?
Because reply
doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $?
so use that as
if [ $? -eq 0 ]
Rinse and repeat the suggestions for other statements in your script.
Good luck
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Well you're assigning output of a command incorrectly. Should be:
reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)
That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...)
, but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))
Why did it not consider "if" to be a command?
If statements operate on commands, but what you give is variable $reply
, which only exists temporarily ( see later in the answer about that) so it exists only for mount
command's environment, but the shell running the script knows nothing of it - it doesn't exist. To compare strings use [
, and yes it's a command also known as test
. So do
if test "$reply" == 0
See quotes? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.
But if you want to check exit status of command, just do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
See no sudo
? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.
I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
then
echo "Success"
else
echo "Fail"
fi
Because of 1>
normal output is suppressed (actually redirected to /dev/null
especially for that purpose) but errors will show if anything goes wrong.
Why did it ignore the "sudo" before "mount" ?
As for why you're getting error message that's because the form
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
is treated as envvariable=value command arg1 arg2
, i.e. you're putting variable result
with value sudo
into environment of mount
command. The command will know about it, but after command exits - the variable is gone too.
Why did it not print the (non-zero) value of $reply?
Because reply
doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $?
so use that as
if [ $? -eq 0 ]
Rinse and repeat the suggestions for other statements in your script.
Good luck
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
add a comment |Â
up vote
3
down vote
Well you're assigning output of a command incorrectly. Should be:
reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)
That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...)
, but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))
Why did it not consider "if" to be a command?
If statements operate on commands, but what you give is variable $reply
, which only exists temporarily ( see later in the answer about that) so it exists only for mount
command's environment, but the shell running the script knows nothing of it - it doesn't exist. To compare strings use [
, and yes it's a command also known as test
. So do
if test "$reply" == 0
See quotes? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.
But if you want to check exit status of command, just do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
See no sudo
? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.
I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
then
echo "Success"
else
echo "Fail"
fi
Because of 1>
normal output is suppressed (actually redirected to /dev/null
especially for that purpose) but errors will show if anything goes wrong.
Why did it ignore the "sudo" before "mount" ?
As for why you're getting error message that's because the form
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
is treated as envvariable=value command arg1 arg2
, i.e. you're putting variable result
with value sudo
into environment of mount
command. The command will know about it, but after command exits - the variable is gone too.
Why did it not print the (non-zero) value of $reply?
Because reply
doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $?
so use that as
if [ $? -eq 0 ]
Rinse and repeat the suggestions for other statements in your script.
Good luck
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Well you're assigning output of a command incorrectly. Should be:
reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)
That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...)
, but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))
Why did it not consider "if" to be a command?
If statements operate on commands, but what you give is variable $reply
, which only exists temporarily ( see later in the answer about that) so it exists only for mount
command's environment, but the shell running the script knows nothing of it - it doesn't exist. To compare strings use [
, and yes it's a command also known as test
. So do
if test "$reply" == 0
See quotes? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.
But if you want to check exit status of command, just do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
See no sudo
? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.
I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
then
echo "Success"
else
echo "Fail"
fi
Because of 1>
normal output is suppressed (actually redirected to /dev/null
especially for that purpose) but errors will show if anything goes wrong.
Why did it ignore the "sudo" before "mount" ?
As for why you're getting error message that's because the form
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
is treated as envvariable=value command arg1 arg2
, i.e. you're putting variable result
with value sudo
into environment of mount
command. The command will know about it, but after command exits - the variable is gone too.
Why did it not print the (non-zero) value of $reply?
Because reply
doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $?
so use that as
if [ $? -eq 0 ]
Rinse and repeat the suggestions for other statements in your script.
Good luck
Well you're assigning output of a command incorrectly. Should be:
reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)
That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...)
, but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))
Why did it not consider "if" to be a command?
If statements operate on commands, but what you give is variable $reply
, which only exists temporarily ( see later in the answer about that) so it exists only for mount
command's environment, but the shell running the script knows nothing of it - it doesn't exist. To compare strings use [
, and yes it's a command also known as test
. So do
if test "$reply" == 0
See quotes? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.
But if you want to check exit status of command, just do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
See no sudo
? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.
I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do
if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
then
echo "Success"
else
echo "Fail"
fi
Because of 1>
normal output is suppressed (actually redirected to /dev/null
especially for that purpose) but errors will show if anything goes wrong.
Why did it ignore the "sudo" before "mount" ?
As for why you're getting error message that's because the form
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
is treated as envvariable=value command arg1 arg2
, i.e. you're putting variable result
with value sudo
into environment of mount
command. The command will know about it, but after command exits - the variable is gone too.
Why did it not print the (non-zero) value of $reply?
Because reply
doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $?
so use that as
if [ $? -eq 0 ]
Rinse and repeat the suggestions for other statements in your script.
Good luck
edited 2 mins ago
muru
132k19280478
132k19280478
answered 25 mins ago
Sergiy Kolodyazhnyy
66.9k9135297
66.9k9135297
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
add a comment |Â
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
â Sergiy Kolodyazhnyy
10 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
@muru Edited. Thanks for the catch
â Sergiy Kolodyazhnyy
7 mins ago
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%2faskubuntu.com%2fquestions%2f1088748%2fbash-scripting-problem%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