bash script help?

Nils Kassube kassube at gmx.net
Mon Nov 14 16:59:37 UTC 2011


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 at 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 at knutejohnson:/home/knute# if umount /dev/sdc1; then echo 1;else
> echo 0;fi
> umount: /dev/sdc1: not found
> 0

Not found -> fail -> "echo 0".

> root at knutejohnson:/home/knute# df
> Filesystem           1K-blocks      Used Available Use% Mounted on
> /dev/sda1             75865184   8764396  63246964  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 at knutejohnson:/home/knute# if mount /dev/sdb1 /media/usbdisk;
> then echo 1;else echo 0;fi
> 1

That looks right.

> root at 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
> /dev/sdb1             15618044   7102312   8515732  46%
> /media/usbdisk
> 
> root at knutejohnson:/home/knute# if umount /dev/sdb1; then echo 1;else
> echo 0;fi
> 1

That looks right.

> root at 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 at 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




More information about the ubuntu-users mailing list