Nick D.
Nick D.

Reputation: 59

Bash script exits unexpectedly

I am writing a wrapper script around mail. There is a function in the program that I need for looping back to the main menu, but just before the function is declared, the program just exits back to the main prompt. Here is the code:

function restart ()
{
    m
}
clear
echo Working...
echo If you are prompted for your sudo password or asked if you want to continue, then you are being
echo prompted to install mailutils. This is normal upon first-time use, or
echo use on a computer without mailutils installed.
echo 
echo Starting in 5 seconds...
sleep 5
echo Examining dependencies...
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils
echo Starting client...
function m ()
{
    clear
    echo Welcome to the Terminal GMail Client, or TGC!
    echo Please enter your gmail address:
    read acc
    name=${acc%@*}
    echo Welcome, $name! Would you like to read[R] or write[W] emails?
    read opt
    if [ $opt=="R" ] || [ $opt=="r" ]
    then
        echo Working...
        sleep 1
        clear
        mail -u $acc -p
        restart
    elif [ $opt=="W" ] || [ $opt=="w" ]
    then
        clear
            echo Working...
        sleep 1
        clear
        echo Enter the subject here:
        read sub
        echo Enter the recipients address here:
        read rec
        echo Enter carbon copy [CC] here or leave blank for none:
        read cc
        echo Enter blind carbon copy [Bcc] here or leave blank for none:
        read bcc
        echo Enter the body of the email here:
        read body
        echo Sending to $rec...
        mail -s $sub -c $cc -b $bcc --user=$acc $rec "$body"
        echo Done! Going to main menu in 2 seconds...
        sleep 2
        restart
    fi
}

You see, there is no error, and I am put back at the prompt right after line 15, after 'Starting Client...'.

Upvotes: 2

Views: 581

Answers (1)

mklement0
mklement0

Reputation: 437698

As others have pointed out in the comments: there's no need for multiple shell functions and recursion - a simple while loop will do.

The following is a revised version of your code with proper quoting and rudimentary error handling. Your script will need a lot more input validation and error checking to stand the test of real-world use.

But perhaps this will get you started.

#!/usr/bin/env bash

clear
echo 'Working...'
echo 'If you are prompted for your sudo password or asked if you want to continue, then you are being
prompted to install mailutils. This is normal upon first-time use, or
use on a computer without mailutils installed.'

echo 'Starting in 5 seconds...'
sleep 5
echo 'Examining dependencies...'
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils || exit

clear
echo 'Starting client...'

while true; do
  echo 'Welcome to the Terminal GMail Client, or TGC!'
  echo 'Please enter your gmail address:'
  read -r acc
  name=${acc%@*}
  echo "Welcome, $name! Would you like to read[R] or write[W] emails or quit[Q]?"
  read -r opt
  case $opt in
    r|R)
      echo 'Working...'
      sleep 1
      clear
      mail -u "$acc" -p || { echo "ERROR: Please try again." >&2; continue; }
      ;;
    w|W)
      clear
      echo 'Working...'
      sleep 1
      clear
      echo 'Enter the subject here:'
      read -r sub
      echo "Enter the recipient's address here:"
      read -r rec
      echo 'Enter carbon copy [CC] here or leave blank for none:'
      read -r cc
      echo 'Enter blind carbon copy [Bcc] here or leave blank for none:'
      read bcc
      echo 'Enter the body of the email here:'
      read -r body
      echo "Sending to $rec..."
      mail -s "$sub" -c "$cc" -b "$bcc" --user="$acc" "$rec" "$body"  || { echo "ERROR: Please try again." >&2; continue; }
      echo 'Done! Going to main menu in 2 seconds...'
      sleep 2
      ;;
    q|Q)
      echo 'Goodbye.'
      exit 0
      ;;
    *)
      echo 'ERROR: Unknown command. Please try again.' >&2
      ;;
  esac
done

Upvotes: 2

Related Questions