LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2021, 09:35 AM   #1
elnetotaca
LQ Newbie
 
Registered: Jan 2011
Posts: 16

Rep: Reputation: 0
Bash script menu looped.


So here is what I have, and I am very happy with what I have so far, but I do want to add a loop so when a command has been executed, ask for a "ENTER" strike and it will send you back to the menu...

Considerations;
The menu already has a button for "OK" and "CANCEL", where "CANCEL" exits the menu.

Code:
#!/bin/bash
cmd=(dialog --keep-tite --menu "Welcome to Ernie's Utility Menu v1.0:" 22 76 16)

options=(1  "Hide Connection"
         2  "Disconnect from VPN"
         3  "Status of Connection"
         4  "Update the system"
         5  "Clean up post update mess" 
         6  "Deep Clean (Trojans and malware)"
         7  "Speedometer (Bandwith Monitor)"
         8  "Bmon (Bandwith Monitor)"
         9  "Test Bandwith speed (up & down)"
         10 "Snow in the terminal"
        )

choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

for choice in $choices 
    do    
        case $choice in
        1)
            expressvpn connect
            ;;
        2)
            expressvpn disconnect
            ;;
        3)
            expressvpn status && nmcli dev wifi
            ;;
        4)
            sudo apt update && sudo apt upgrade -y #!//&& sudo apt-get dist-upgrade -y not sure if I want to do this part....
            ;;
        5)
            sudo apt update && sudo apt -f install && sudo dpkg --configure -a && sudo apt clean && sudo apt autoremove && sudo -k && exit
            ;;
        6)
            sudo chkrootkit -d && sudo rkhunter -c --rwo && sudo -k
            ;;
        7)
            speedometer -l  -r wlp2s0 -t lo -m $(( 1024 * 1024 * 3 / 2 ))
            ;;
        8)
            bmon
            ;;
        9)
            speedtest
            ;;
        10)
            ./snow.sh
            ;; 
    esac
done

bash
I tried adding:
read -p "Hit enter to continue ..."
exec /bin/bash "$0" "$@"

but then I had to add an option for exit because even if I click on the "CANCEL" button, it sends me back to the menu.
What would be the most smart way to make this better?
 
Old 09-05-2021, 03:30 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,950

Rep: Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784
Just wrap in a while true loop, like so
Code:
while true
do
 your menu code
 hut enter code
 ...
done
 
Old 09-05-2021, 06:37 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,814

Rep: Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251
You do not need to run bash as the last command which just starts a subshell.
In addition to adding the while loop as posted above, one way to exit without additional prompting would be to add a default to your case after your last item number i.e.

Code:
 ...
 10) run command...
   ;;
  *)  exit
esac
When you select cancel from your menu the contents of choice is an empty string and therefore your scripts ends.

Last edited by michaelk; 09-05-2021 at 06:39 PM.
 
Old 09-05-2021, 07:16 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,832

Rep: Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089Reputation: 3089
Maybe also have a look at the bash 'select' function
 
Old 09-06-2021, 06:16 PM   #5
elnetotaca
LQ Newbie
 
Registered: Jan 2011
Posts: 16

Original Poster
Rep: Reputation: 0
I tried the;

Code:
while true
do
 your menu code
 hut enter code
 ...
done
but what this does is loops without an end the choice I selected from the menu.

So I guess I am going to leave this script looking like this at the end until I find the answer;
Code:
           speedtest
            ;;
        10)
            ./snow.sh
            ;;
        11)
            exit
            ;;        
        esac
done
read -p "Hit enter to continue ..."
exec /bin/bash "$0" "$@"
I am looking at that "select" function.
Thanks guys

Last edited by elnetotaca; 09-06-2021 at 06:18 PM.
 
Old 09-06-2021, 06:49 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,950

Rep: Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784Reputation: 784
the 'true' can be anything that you want just set a flag when you want to break the loop there are plenty of online examples, i just used true as an example
 
Old 09-06-2021, 07:15 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,814

Rep: Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251Reputation: 4251
Code:
#!/bin/bash

while true
do

cmd=(dialog --keep-tite --menu "Welcome to Ernie's Utility Menu v1.0:" 22 76 16)

options=(1  "Hide Connection"
         2  "Disconnect from VPN"
         3  "Status of Connection"
         4  "Update the system"
         5  "Clean up post update mess" 
         6  "Deep Clean (Trojans and malware)"
         7  "Speedometer (Bandwith Monitor)"
         8  "Bmon (Bandwith Monitor)"
         9  "Test Bandwith speed (up & down)"
         10 "Snow in the terminal"
        )


choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

        case $choice in
        1)
            echo "1"
            ;;
        2)
            echo "2"
            ;;
        3)
            echo "3"
            ;;
        4)
            echo "4"
            ;;
        5)
            echo "5"
            ;;
        6)
           echo "6"
            ;;
        7)
            echo "7"
            ;;
        8)
           echo "8"
            ;;
        9)
            echo "9"
            ;;
        10)
            echo "10"
            ;;
         *)
            exit 
    esac
  read -p "Hit enter to continue ..."

done
Just move the read statement before the done of the while loop. Instead of using the cancel you can add an exit option as posted #5

Since as far as I know you can not select more then one option at a time the for loop does not really do anything and can be eliminated. With a loop there is no need to keep creating a child process to run your menu again.

select is another way to create a menu but it isn't GUI like. It will not automatically "fix" your "Hit enter to continue" requirement.

Do you want a conditional to exit with the Hit enter to continue? Enter any character to quit.
Code:
read -p "Hit enter to continue ..." q

if test -n "$q"
then
   exit
fi

Last edited by michaelk; 09-06-2021 at 08:29 PM.
 
Old 09-07-2021, 12:36 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,783
Blog Entries: 11

Rep: Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383Reputation: 5383
Quote:
Originally Posted by elnetotaca View Post
I tried the;

Code:
while true
do
 your menu code
 hut enter code
 ...
done
but what this does is loops without an end the choice I selected from the menu.
It shouldn't do that if one of your choices executes "exit".
 
Old 09-08-2021, 06:29 AM   #9
elnetotaca
LQ Newbie
 
Registered: Jan 2011
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Code:
#!/bin/bash

while true
do

cmd=(dialog --keep-tite --menu "Welcome to Ernie's Utility Menu v1.0:" 22 76 16)

options=(1  "Hide Connection"
         2  "Disconnect from VPN"
         3  "Status of Connection"
         4  "Update the system"
         5  "Clean up post update mess" 
         6  "Deep Clean (Trojans and malware)"
         7  "Speedometer (Bandwith Monitor)"
         8  "Bmon (Bandwith Monitor)"
         9  "Test Bandwith speed (up & down)"
         10 "Snow in the terminal"
        )


choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

        case $choice in
        1)
            echo "1"
            ;;
        2)
            echo "2"
            ;;
        3)
            echo "3"
            ;;
        4)
            echo "4"
            ;;
        5)
            echo "5"
            ;;
        6)
           echo "6"
            ;;
        7)
            echo "7"
            ;;
        8)
           echo "8"
            ;;
        9)
            echo "9"
            ;;
        10)
            echo "10"
            ;;
         *)
            exit 
    esac
  read -p "Hit enter to continue ..."

done
Just move the read statement before the done of the while loop. Instead of using the cancel you can add an exit option as posted #5

Since as far as I know you can not select more then one option at a time the for loop does not really do anything and can be eliminated. With a loop there is no need to keep creating a child process to run your menu again.

select is another way to create a menu but it isn't GUI like. It will not automatically "fix" your "Hit enter to continue" requirement.

Do you want a conditional to exit with the Hit enter to continue? Enter any character to quit.
Code:
read -p "Hit enter to continue ..." q

if test -n "$q"
then
   exit
fi

SUPERB!!!!!!
YUSSSSSSS!!! Thank you very much!!!! I am marking this as solved, and THANKS AGAIN!
 
Old 09-08-2021, 05:57 PM   #10
elnetotaca
LQ Newbie
 
Registered: Jan 2011
Posts: 16

Original Poster
Rep: Reputation: 0
This is the end product and it works like a charm!!!!!
Code:
#!/bin/bash
cmd=(dialog --keep-tite --menu "Welcome to Ernie's Utility Menu v1.0:" 22 76 16)

options=(1  "Hide Connection"
         2  "Disconnect from VPN"
         3  "Status of Connection"
         4  "Update the system"
         5  "Clean up post update mess" 
         6  "Deep Clean (Trojans and malware)"
         7  "Speedometer (Bandwith Monitor)"
         8  "Bmon (Bandwith Monitor)"
         9  "Test Bandwith speed (up & down)"
         10 "Snow in the terminal"
#         11 "exit"
        )

choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

for choice in $choices 
    do    
        case $choice in
        1)
            expressvpn connect
            ;;
        2)
            expressvpn disconnect
            ;;
        3)
            expressvpn status && nmcli dev wifi
            ;;
        4)
            sudo apt update && sudo apt upgrade -y #!//&& sudo apt-get dist-upgrade -y not sure if I want to do this part....
            ;;
        5)
            sudo apt update && sudo apt -f install && sudo dpkg --configure -a && sudo apt clean && sudo apt autoremove && sudo -k && exit
            ;;
        6)
            sudo chkrootkit -d && sudo rkhunter -c --rwo && sudo -k
            ;;
        7)
            speedometer -l  -r wlp2s0 -t lo -m $(( 1024 * 1024 * 3 / 2 ))
            ;;
        8)
            bmon
            ;;
        9)
            speedtest
            ;;
        10)
            ./snow.sh
            ;;
         *)
            exit
      esac
read -p "Hit enter to continue ..."
exec /bin/bash "$0" "$@"
      done
 
  


Reply

Tags
bash loop, bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script with Menu and sub menu and in sub menu execute another shell script SHWE Linux - Newbie 9 11-03-2018 06:19 PM
Which Format to Create Looped Animations (like GIF, but with more than 8bit colors)? ondoho Linux - Software 2 02-09-2017 01:09 AM
[SOLVED] Bash script menu with sub menu sbrock Programming 3 01-19-2014 04:01 AM
OUCH! Perl Sendmail script looped over 14000 times to 1 addy... HELP! brokenfeet Programming 1 09-04-2003 07:51 PM
Serial line is looped back? Unknown_User Linux - Hardware 1 02-28-2003 08:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration