#!/bin/bash mount | grep 'sda[1-5]' if [ $? = 0 ] then echo -e "its already mounted ! type u to unmount :\c" read word if [ $word = "u" || $word = "U" ] then umount /home/shady/Desktop/mountpoint1 umount /home/shady/Desktop/mountpoint2 fi fi if [ $? = 1 ] then mount /dev/sda1 /home/shady/Desktop/mountpoint1 mount /dev/sda5 /home/shady/Desktop/mountpoint2 echo -e "all mounted !!" fi its not even entering the first if statement
92 Answers
This line was your main problem:
if [ $word = "u" || $word = "U" ] I've corrected the conditional, and tidied the control structure below:
#!/bin/bash mount | grep 'sda[1-5]' if [ $? -eq 0 ] then echo "Volumes are already mounted." read -p "Please type 'u' to unmount:" word if [ "$word" = "u" ] || [ "$word" = "U" ] then echo 'User wishes unmount. Attempt umount here.' fi else echo 'Volumes unmounted. Attempt mount here.' fi See this bash operators page for examples.
There's couple of issues. One [ $? = 0 ] should be [ $? -eq 0 ] because $? returns number, but = is the comparison for strings ( text). Second, use [ $word = "u" ] || [ $word = "U" ] or use [ $word = "u" -o $word = "U" ]. Read more on these options in man test
However, I'd suggest a simpler solution: use udisksctl utility instead of media. It's simpler and does a lot of stuff automatically for you. In fact this is the back-end for Ubuntu's default file manager, Nautilus.
For instance, if I wanted to mount /dev/sda5
$ udisksctl mount -b /dev/sdb5 Mounted /dev/sdb5 at /media/xieerqi/0ca7543a-5463-4a07-8bbe-233a7b0bd625 Get info where is volume mounted:
$ udisksctl info -b /dev/sdb5 | awk '/MountPoints/' MountPoints: /media/xieerqi/0ca7543a-5463-4a07-8bbe-233a7b0bd625 And here's unmounting
$ udisksctl unmount -b /dev/sdb5 Unmounted /dev/sdb5. 1