0

Finally made the move into Linux and decided the best way of learning the ropes was to try my hand at scripting.

Trying to make a basic (started really basic and keeps growing) script that automatically mounts or unmounts partitions based on user input. Thought i was on the right path but can't work out whats going wrong. Apologies in advance if it is just something really dumb.

#!/bin/bash
# Test script to auto mount hdd based in user input

echo "Do you wish to mount or unmount?"
read origin

if [ $origin == mount ]; then
    echo "Partitions : $(lsblk)"
    echo "Please enter device name e.g. sda1"
    read device
    echo "Please enter dir location e.g. /mnt"
    read location
    mount -t ntfs /dev/$device $location
if [ $origin == unmount ]; then
    echo "Mounts : $(mount)"
    echo "Please enter mount location e.g. /mnt"
    read ulocation
    umount $ulocation
fi
  • 3
    If you paste your code into shellcheck.net, you will see that you are missing an fi statement. – John1024 Jul 25 '16 at 20:25
  • @John1024 might wanna post this as an answer . Up to you – Sergiy Kolodyazhnyy Jul 25 '16 at 22:28
6

Change this line:

if [ $origin == unmount ]; then

to this:

elif [ $origin == unmount ]; then

You're getting this error because bash is interpreting the second if as nested, instead of as a second condition. Here's a visualization with indents.

if [ $origin == mount ]; then
  # Do some things.
  if [ $origin == unmount ]; then
    # Do some things.
  fi
#fi

By the way, you should also quote your variables to protect against word-splitting and globbing:

if [ "$origin" == mount ]; then
    ...
    mount -t ntfs /dev/"$device" "$location"
elif [ "$origin" == unmount ]; then
    ...
    umount "$ulocation"
3

wjandrea seems to have covered the if issues. I would suggest a case statement:

#!/bin/bash
# Test script to auto mount hdd based in user input

while read -rp "Do you wish to mount or unmount? " origin
do
    case "$origin" in
        m*)
            echo "Partitions : $(lsblk)"
            read -rp "Please enter device name e.g. sda1: " device
            read -rp "Please enter dir location e.g. /mnt: " location
            mount "/dev/$device" "$location"
            break
            ;;
        u*)
            echo "Mounts : $(mount)"
            read -rp "Please enter mount location e.g. /mnt: " ulocation
            umount "$ulocation"
            break
            ;;
        *)
            echo "You typed nonsense.  Please try again."
            ;;
    esac
done

This allows the person to request a mount by typing mount or mo or even just m. Similarly, an unmount can be requested with an answer as short as u. (You can be more restrictive if you want.) Further, if the user fails to answer with something acceptable, he is scolded and asked again.

Unless you want word splitting and pathname expansion, all shell variables should be, as shown above, in double-quotes.

Also, for greater flexibility, I left out the -t ntfs option. mount can usually automatically select the right type.

An advantage of using case statements for pattern matching over fancy bash pattern matching with [[...]] is that case is POSIX and therefore portable.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.