You are not logged in.
I have a script that has worked for ages just fine to mount/unmount my laptop:
#!/bin/bash
# Mount/unmount Compaq Presario A900 laptop when booted into Arch via cifs
DRIVE2="/media/cl-boblap"
if [ -f $DRIVE2/.lock ]; then
echo "Unmounting cl-boblap..." && sudo umount $DRIVE2 && echo "Done! :)"
else
echo "Mounting jay-laptop..." && sudo mount $DRIVE2 && echo "Done! :)"
fi
I always had this aliased as mlap and it was no prob to run and worked great. However I recently did a reinstall and partitioning of it and have 3 parts that I'd like to mount/unmount simultaneously. Here's what I did:
#!/bin/bash
# Mount/unmount Compaq Presario A900 laptop when booted into Arch via cifs
DRIVE2="/media/cl-boblap"
DRIVE3="/media/cl-windows"
DRIVE4="/media/cl-extra"
if [ -f $DRIVE2/.lock ]; then
echo "Unmounting cl-boblap..." && sudo umount $DRIVE2 && echo "Unmounted! :)"
else
echo "Mounting cl-boblap..." && sudo mount $DRIVE2 && echo "Mounted! :)"
fi
if [ -f $DRIVE3/.lock ]; then
echo "Unmounting cl-windows..." && sudo umount $DRIVE3 && echo "Unmounted! :)"
else
echo "Mounting cl-windows..." && sudo mount $DRIVE3 && echo "Mounted! :)"
fi
if [ -f $DRIVE4/.lock ]; then
echo "Unmounting cl-extra..." && sudo umount $DRIVE4 && echo "Unmounted! :)"
else
echo "Mounting cl-extra..." && sudo mount $DRIVE4 && echo "Mounted! :)"
fi
Now this works, however let's say for some reason I wind up with one or two of the three mounted and the other unmounted or vice versa, the script will keep reversing this, never getting all 3 to the same state. So how could I make it so that it kind of "checks" the one before it and puts it to that state? For example if the first one cl-boblap is unmounting, cl-windows checks to see and unmounts as well, or does nothing, but it does not mount. It would try and be the same as the one above.
Appreciate any help! Thanks
Last edited by colbert (2010-12-06 06:58:37)
Offline
You could pass an argument to the script, to tell it what you want (all MOUNTED or all UNMOUNTED).
eg:
mlap ON
mlap OFF
you can parse args in bash with :
$# => number of arg
$0 => script name
$1 => first arg
...
"You feel a strange vibration under your feet."
Offline
#!/bin/bash
# Mount/unmount Compaq Presario A900 laptop when booted into Arch via cifs
DRIVE2="/media/cl-boblap"
DRIVE3="/media/cl-windows"
DRIVE4="/media/cl-extra"
if [ -f $DRIVE2/.lock ]; then
echo "Unmounting cl-boblap..." && sudo umount $DRIVE2 && echo "Unmounted! :)"
else
echo "Mounting cl-boblap..." && sudo mount $DRIVE2 && echo "Mounted! :)"
fi
if [ -f $DRIVE3/.lock && ! -f $DRIVE2/.lock ]; then
# ^^^^^^^^^^^^^^^^^^^^^
# if the mount/unmount status is different,
# then make it the same.
echo "Unmounting cl-windows..." && sudo umount $DRIVE3 && echo "Unmounted! :)"
elif [ ! -f $DRIVE3/.lock && -f $DRIVE2/.lock ]; then
echo "Mounting cl-windows..." && sudo mount $DRIVE3 && echo "Mounted! :)"
fi
if [ -f $DRIVE4/.lock && ! -f $DRIVE2/.lock ]; then
echo "Unmounting cl-extra..." && sudo umount $DRIVE4 && echo "Unmounted! :)"
elif [ ! -f $DRIVE4/.lock && -f $DRIVE2/.lock ]; then
echo "Mounting cl-extra..." && sudo mount $DRIVE4 && echo "Mounted! :)"
fi
Last edited by a_neutrino (2010-12-02 23:49:03)
Offline
Thanks a lot! I got this error:
mlap
Mounting cl-boblap...
Password:
Mounted! :)
/home/bobby/scripts/mountunmount-jaylaptop.sh: line 33: [: missing `]'
/home/bobby/scripts/mountunmount-jaylaptop.sh: line 38: [: missing `]'
/home/bobby/scripts/mountunmount-jaylaptop.sh: line 41: [: missing `]'
/home/bobby/scripts/mountunmount-jaylaptop.sh: line 43: [: missing `]'
First one mounted but then that
Offline
The error message you are getting is due to your use of the '[' command. What you should use is the '[[' command instead:
# don't do this:
if [ -f $DRIVE3/.lock && ! -f $DRIVE2/.lock ]; then ...
# do this instead:
if [[ -f $DRIVE3/.lock && ! -f $DRIVE2/.lock ]]; then ...
The error is caused by the '[' command not understanding the '&&' operator - it expects to see the ending ']' instead. The '[[' is a bash builtin command and it works as you would expect.
That being said, I agree with Simplet and think that you should use a parameter to control things.
#!/bin/bash
# Mount/unmount Compaq Presario A900 laptop when booted into Arch via cifs
# store the names of your drives in an array
drives=(/media/cl-boblap /media/cl-windows /media/cl-extra)
# You could possibly use the following instead:
#drives=(/media/cl-*)
# The script will unmount all drives if any of the drives are mounted. If all
# drives are unmounted then it will mount all drives. You can force one way
# or the other by passing a parameter to the script. If the parameter starts
# with 'm' or 'M' it will mount the drives and if it starts with 'u' or 'U'
# it will unmount the drives.
[[ -n $1 && $1 =~ ^[^mMuU].* ]] && { echo "Usage: $0 [mount | umount]"; exit 1; }
action='Mounting'
[[ -z $1 ]] && for d in "${drives[@]}"; do [[ -f "$d/.lock" ]] && action='Unmounting'; done
[[ $1 =~ ^[uU].* ]] && action='Unmounting'
for d in "${drives[@]}"
do
echo -n "$action ${d##*/} ... "
case "$action" in
U*) [[ -f "$d/.lock" ]] && sudo umount "$d" ;;
*) [[ -f "$d/.lock" ]] || sudo mount "$d" ;;
esac
[[ $? -eq 0 ]] && echo 'success.' || echo 'failure.'
done
Then run
$ mlap mount -or-
$ mlap m
to mount the drives and
$ mlap umount -or-
$ mlap u
to unmount.
Just running
$ mlap
will toggle the state of the drives.
Offline
Thanks so much rockin turtle! I tried this script and it works great. Much appreciated, and I fiddled around with the brackets to understand how it works and it's sinking in alas
Offline