I've got a bash script that I run in cron.daily to backup some data to a
jump drive. One of the commands in my script is to umount the drive.
If I write;
if umount /dev/sdb1; then
#do something
fi
it always does it even if the umount is successful. But if I write;
umount /dev/sdb1
if [ $? -ne 0 ]; then
#do something
fi
it works as I expect and doesn't do the code in the if. mount seems to
work as I expect but something is different about umount or I don't
understand what I'm doing :-).
Thanks,
--
Knute Johnson
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 05:15 AM
Nils Kassube
bash script help?
Knute Johnson wrote:
> I've got a bash script that I run in cron.daily to backup some data
> to a jump drive. One of the commands in my script is to umount the
> drive. If I write;
>
> if umount /dev/sdb1; then
> #do something
> fi
>
> it always does it even if the umount is successful.
You mean: "if it fails"? Well, I can't confirm. Try this as root:
if umount / 2>/dev/null; then echo 1;else echo 0;fi
Here the output is "0", which I would expect. If I replace / with an
unmountable partition, the output is "1".
Nils
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 09:09 AM
Colin Watson
bash script help?
On Sun, Nov 13, 2011 at 09:40:47PM -0800, Knute Johnson wrote:
> I've got a bash script that I run in cron.daily to backup some data
> to a jump drive. One of the commands in my script is to umount the
> drive. If I write;
>
> if umount /dev/sdb1; then
> #do something
> fi
>
> it always does it even if the umount is successful.
The code above means "do something *only* if umount exits successfully".
If you want to do something if umount fails, then you need:
if ! umount /dev/sdb1; then
# do something
fi
--
Colin Watson [cjwatson@ubuntu.com]
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 02:47 PM
Knute Johnson
bash script help?
On 11/13/2011 10:15 PM, Nils Kassube wrote:
Knute Johnson wrote:
I've got a bash script that I run in cron.daily to backup some data
to a jump drive. One of the commands in my script is to umount the
drive. If I write;
if umount /dev/sdb1; then
#do something
fi
it always does it even if the umount is successful.
You mean: "if it fails"? Well, I can't confirm. Try this as root:
if umount / 2>/dev/null; then echo 1;else echo 0;fi
Here the output is "0", which I would expect. If I replace / with an
unmountable partition, the output is "1".
Nils
root@knutejohnson:/home/knute# if umount / 2>/dev/null; then echo 1;
else echo 0;fi
0
Why does this return success? The device is busy and can't be
unmounted. The unmountable disk /dev/sdc1 returns 0.
root@knutejohnson:/home/knute# if umount /dev/sdc1; then echo 1;else
echo 0;fi
root@knutejohnson:/home/knute# mount /dev/sdc1 /media/usbdisk; echo $?
mount: special device /dev/sdc1 does not exist
32
There is something here I don't understand?
Thanks,
--
Knute Johnson
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 02:51 PM
Knute Johnson
bash script help?
On 11/14/2011 2:09 AM, Colin Watson wrote:
On Sun, Nov 13, 2011 at 09:40:47PM -0800, Knute Johnson wrote:
I've got a bash script that I run in cron.daily to backup some data
to a jump drive. One of the commands in my script is to umount the
drive. If I write;
if umount /dev/sdb1; then
#do something
fi
it always does it even if the umount is successful.
The code above means "do something *only* if umount exits successfully".
If you want to do something if umount fails, then you need:
if ! umount /dev/sdb1; then
# do something
fi
Isn't something other than 0 true? umount should return false if it
fails to mount and in my example it would #do something if it failed to
unmount, yes?
Thanks,
--
Knute Johnson
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 03:12 PM
Hal Burgiss
bash script help?
On Mon, Nov 14, 2011 at 10:51 AM, Knute Johnson <ubuntu@knutejohnson.com> wrote:
Isn't something other than 0 true? *umount should return false if it fails to mount and in my example it would #do something if it failed to unmount, yes?
This is what I think is happening ... there is no actual error of mount per se happening, its doing what's designed to do in these specific situations, so it returns true. Not saying that's how I would like to see it work, but that's my guess.
I would just grep mtab and be done with it. *
--
Hal
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 03:59 PM
Nils Kassube
bash script help?
Knute Johnson wrote:
> On 11/13/2011 10:15 PM, Nils Kassube wrote:
> > Knute Johnson wrote:
> >> I've got a bash script that I run in cron.daily to backup some
> >> data to a jump drive. One of the commands in my script is to
> >> umount the drive. If I write;
> >>
> >> if umount /dev/sdb1; then
> >>
> >> #do something
> >>
> >> fi
> >>
> >> it always does it even if the umount is successful.
> >
> > You mean: "if it fails"? Well, I can't confirm. Try this as root:
> >
> > if umount / 2>/dev/null; then echo 1;else echo 0;fi
> >
> > Here the output is "0", which I would expect. If I replace / with
> > an unmountable partition, the output is "1".
>
> root@knutejohnson:/home/knute# if umount / 2>/dev/null; then echo 1;
> else echo 0;fi
> 0
>
> Why does this return success?
Does it? In case of success the command is "echo 1".
> The device is busy and can't be
> unmounted. The unmountable disk /dev/sdc1 returns 0.
>
> root@knutejohnson:/home/knute# if umount /dev/sdc1; then echo 1;else
> echo 0;fi
> umount: /dev/sdc1: not found
> 0
> root@knutejohnson:/home/knute# df
> Filesystem 1K-blocks Used Available Use% Mounted on
> /dev/sda1 75865184 8764388 63246972 13% /
> none 505520 244 505276 1% /dev
> none 512148 0 512148 0% /dev/shm
> none 512148 992 511156 1% /var/run
> none 512148 0 512148 0% /var/lock
> root@knutejohnson:/home/knute#
>
> Both of the above appear to me to be wrong, they should both be
> showing 0 not 1. Then there is this;
No, both are OK because the way I wrote the command it should echo 1 on
success and 0 on failure. I think you mixed up the return code with my
echo output. Sorry, I didn't think of that possibility - maybe I should
have used something linke words instead of numbers which were actually
the opposite of the the return code.
Nils
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 04:04 PM
Nils Kassube
bash script help?
Knute Johnson wrote:
> On 11/14/2011 2:09 AM, Colin Watson wrote:
> > On Sun, Nov 13, 2011 at 09:40:47PM -0800, Knute Johnson wrote:
> >> I've got a bash script that I run in cron.daily to backup some
> >> data to a jump drive. One of the commands in my script is to
> >> umount the drive. If I write;
> >>
> >> if umount /dev/sdb1; then
> >>
> >> #do something
> >>
> >> fi
> >>
> >> it always does it even if the umount is successful.
> >
> > The code above means "do something *only* if umount exits
> > successfully".
> >
> > If you want to do something if umount fails, then you need:
> > if ! umount /dev/sdb1; then
> >
> > # do something
> >
> > fi
>
> Isn't something other than 0 true?
No. The return code 0 means success or true. But there could be several
reasons for failure, therefore everything else than 0 is failure and the
number can give a detailed reason for the failure.
Nils
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-14-2011, 04:10 PM
Knute Johnson
bash script help?
On 11/14/2011 9:04 AM, Nils Kassube wrote:
Knute Johnson wrote:
On 11/14/2011 2:09 AM, Colin Watson wrote:
On Sun, Nov 13, 2011 at 09:40:47PM -0800, Knute Johnson wrote:
I've got a bash script that I run in cron.daily to backup some
data to a jump drive. One of the commands in my script is to
umount the drive. If I write;
if umount /dev/sdb1; then
#do something
fi
it always does it even if the umount is successful.
The code above means "do something *only* if umount exits
successfully".
If you want to do something if umount fails, then you need:
if ! umount /dev/sdb1; then
# do something
fi
Isn't something other than 0 true?
No. The return code 0 means success or true. But there could be several
reasons for failure, therefore everything else than 0 is failure and the
number can give a detailed reason for the failure.
Nils
OK, that explains why I am so confused. Thanks very much, this was
driving me crazy!
--
Knute Johnson
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users