Skip to content
Advertisement

Looping a bash script

this is my first time dabbling in bash scripting and first time using this site. I’m working on script that’s meant to provide the user with a list of software packages to install and then output their choices into a 2nd script file that can be run later to actually install their choices. So far my script is semi working, but I need help figuring out how to; A) Loop the script so once they’ve chosen a package it restarts and allows them to select another instead of ending the script B) When they select “No” or “nN” to the confirmation it takes them back to choice list instead of wigging out and if they input anything other than yes/no it prompts for valid input

Here is my current script, I understand it’s probably formatted horribly and most likely inefficient but it’s my first time and only for a small school project I’m working on. Any help would be appreciated, thanks!

#!/bin/bash

#bash script to present list of packages for customer install output to txt

if [[ ! -e /home/aarone/pkglist.txt ]]; then
           echo "Making package list script"
           echo "#!/bin/bash" > /home/aarone/pkglist 
           chmod -R 777 /home/aarone/pkglist
fi

# Declare variable choice and assign value 4
choice=4


# print to stdout
  echo "1. Antivirus GUI" 
  echo "2. Firewall GUI" 
  echo "3. MariaDB" 
  echo -n "Please choose a A package [1,2 or 3]? "
# Loop while the variable choice is equal 4
# bash while loop
while [ $choice -eq 4 ]; do


#read user input
read choice

# bash nested if/else
if [ $choice -eq 1 ] 
then

       echo "You have chosen word: Antivirus GUI" 
       apt show clamtk 2>/dev/null | egrep '^Description|^Download' 
       read -r -p "Are you sure? [y/N] " response
       if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]] 
       then
           echo "apt-get clamtk" >> pkglist 
       else
           echo "Input not understood"  
           continue
       fi


else

       if [ $choice -eq 2 ] ; then

              echo "You have chosen package: Firewall GUI"
              apt show gufw 2>/dev/null | egrep '^Description|^Download'
              read -r -p "Are you sure? [y/N] " response
              if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
              then
                  echo "apt-get gufw" >> pkglist 
              else 
                read choice

              fi
       else

              if [ $choice -eq 3 ] ; then

              echo "You have chosen package: Office"
              apt show libreoffice 2>/dev/null | egrep '^Description|^Download'
              read -r -p "Are you sure? [y/N] " response
              if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
              then
                  echo "apt-get libreoffice" >> pkglist 
fi

              else
                     echo "Please make a choice between 1-3 !"
                     echo "1. Antivirus GUI"
                     echo "2. Firewall GUI"
                     echo "3. Office application" 
                     echo -n "Please choose a word [1,2 or 3]? "
                     choice=4
              fi
       fi
fi
done

Thank you @janos that’s pretty much exactly what I wanted! 🙂 The only other thing I’d like to change is the dir the script is created in (to something more default) so I can use it on any system. I also made a small adjustment to each choice so the “No” prompt now works as well.

 1)
            echo "You have chosen package: Antivirus GUI"
            apt show clamtk 2>/dev/null | egrep '^Description|^Download'
            while true; do
                read -r -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                    echo "apt-get install -y clamtk" >> "$pkglist"
                    break
                elif [[ "$response" =~ ^([nN][oO]|[nN])+$ ]]
                then
                    echo "Cancelled"
                    break
                else
                    echo "Input not understood"
                fi
            done
            ;;

Advertisement

Answer

The case statement can help a lot to simplify the implementation of the menu.

To repeat steps, you can use an infinite loop:

while true; do
    # ...
done

Applying the above suggestions, and then some, the script can be corrected and improved:

#!/bin/bash

pkglist=/home/aarone/pkglist.txt 
if [[ ! -e "$pkglist" ]]; then
    echo "Making package list script"
    echo "#!/bin/bash" > "$pkglist"
    chmod -R 777 "$pkglist"
else
    echo Package list script already exists. Exiting.
    exit 1
fi

while true; do
    echo "1. Antivirus GUI" 
    echo "2. Firewall GUI" 
    echo "3. MariaDB" 
    echo "x. Exit"
    printf "Please choose a A package [1, 2 or 3]? "
    read choice

    case "$choice" in
        1)
            echo "You have chosen word: Antivirus GUI" 
            apt show clamtk 2>/dev/null | egrep '^Description|^Download' 
            while true; do
                read -r -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]] 
                then
                    echo "apt-get clamtk" >> "$pkglist" 
                    break
                else
                    echo "Input not understood"  
                fi
            done
            ;;

        2)
            echo "You have chosen package: Firewall GUI"
            apt show gufw 2>/dev/null | egrep '^Description|^Download'
            while true; do
                read -r -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                    echo "apt-get gufw" >> "$pkglist" 
                    break
                else 
                    echo "Input not understood"  
                fi
            done
            ;;

        3)
            echo "You have chosen package: Office"
            apt show libreoffice 2>/dev/null | egrep '^Description|^Download'
            while true; do
                read -r -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                    echo "apt-get libreoffice" >> "$pkglist" 
                    break
                else 
                    echo "Input not understood"  
                fi
            done
            ;;

        *)
            break
    esac
done
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement