LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2018, 05:18 AM   #1
SHWE
LQ Newbie
 
Registered: Nov 2018
Posts: 3

Rep: Reputation: Disabled
Shell script with Menu and sub menu and in sub menu execute another shell script


Hi Guys,

I want to create shell script with Menu option and from menu it should navigate to sub menu and from sub menu want to execute another shell script , could anyone help me how can I do it.
 
Old 11-02-2018, 05:31 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,493

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
Is this your homework?
What kind of menu do you want? Do you know ncurses or dialog?
 
Old 11-02-2018, 05:38 AM   #3
SHWE
LQ Newbie
 
Registered: Nov 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Not homework need to for automation in our company. Eg main menu
1) env xyz
2) env abc

When click on 1 it should redirect to submenu and give list of sub env in main env and after click on one of them then that env script should execute. Hope it's clear
 
Old 11-02-2018, 05:50 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,493

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
you might want to check how select works (in bash). You can find good tutorials and examples on the net.
 
Old 11-02-2018, 05:50 AM   #5
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,255
Blog Entries: 1

Rep: Reputation: Disabled
It is to few information. You should post an example of how this script will be used. But the first: script are rather being created to automatize jobs. If you want interaction then your choice is 'dialog' utility (equivalen). It is ncurses widgets toolbox. It offers menus, list, messages etc. Say simple
Code:
$ dialog --menu menu 10 15 0 1 ola 2 ala 3 ula
 
Old 11-02-2018, 06:31 AM   #6
SHWE
LQ Newbie
 
Registered: Nov 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Something like below but what is happening when I click first option it will goes to first function in that select first then it is not working properly.

#!/bin/bash



function update_user

{

option=0

until [ "$option" = "3"]; do

echo " 1.) Update username"

echo " 2.) Update password"

echo " 3.) Return to menu"



echo -n "Enter choice: "

read option

echo ""

case $option in

1 ) update_username; press_enter ;;

2 ) update_password; press_enter ;;

3 ) main_menu; press_enter ;;

4 ) break ;;

* ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3;

esac

# }

done

}



function main_menu

{

option=0

until [ "$option" = "4" ]; do

echo " 1.) Add user"

echo " 2.) Remove user"

echo " 3.) Update user"

echo " 4.) Quit"



echo -n "Enter choice: "

read option

echo ""

case $option in

1 ) add_user ; press_enter ;;

2 ) remove_user ; press_enter ;;

3 ) update_user ; press_enter ;;

4 ) exit;;

* ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4;

esac

done

}



main_menu
 
Old 11-02-2018, 07:09 AM   #7
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,255
Blog Entries: 1

Rep: Reputation: Disabled
It really looks like homework. I can't imagine in real life someone would have a need for such solution. But crazy, poor teachers they are so willing to torture students with senseless exercises. There already exist system tools to manipulate users accounts. Shell is not particularly suitable to create interactive programs. But if you have Linux installed look at this
Code:
$ file /usr/sbin/adduser
/usr/sbin/adduser: Bourne-Again shell script, ASCII text executable
Read that script and you will find solution. Nihil novi sub sole.
 
1 members found this post helpful.
Old 11-02-2018, 07:54 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,814

Rep: Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974
Quote:
Originally Posted by SHWE
Not homework need to for automation in our company. Eg main menu
So if it's for your 'company', why did they ask you to do this, when you don't have the experience??? And you do realize that automation means that a person doesn't have to do anything, right? Anything needing human/user input isn't automated.
Quote:
Originally Posted by SHWE View Post
Something like below but what is happening when I click first option it will goes to first function in that select first then it is not working properly.
Code:
#!/bin/bash
function update_user
{
option=0
until [ "$option" = "3"]; do
echo "  1.) Update username"
echo "  2.) Update password"
echo "  3.) Return to menu"
echo -n "Enter choice: "

read option
echo ""

case $option in
1 ) update_username; press_enter ;;
2 ) update_password; press_enter ;;
3 ) main_menu; press_enter ;;
4 ) break ;;

* ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3;
 esac
#   }
 done
}

function main_menu {
option=0
until [ "$option" = "4" ]; do
echo "  1.) Add user"
echo "  2.) Remove user"
echo "  3.) Update user"
echo "  4.) Quit"
echo -n "Enter choice: "

read option
echo ""
case $option in

    1 ) add_user ; press_enter ;;
    2 ) remove_user ; press_enter ;;
    3 ) update_user ; press_enter ;;
    4 ) exit;;
    * ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4;
esac
done
}
main_menu
Use CODE tags when posting scripts, please. And you say "it is not working properly", tells us nothing. And you're 'clicking' this, or are you typing the number in? You're building a menu, but not actually putting any commands in to run after someone selects an option. I agree with igadoter totally...this has to be homework, because running "useradd", "userdel", or "usermod", isn't complicated. None of these commands are difficult to use, and the only way I can see a point to this is to prompt someone for information to pass along (like full name, user ID, etc.) But you've not defined ANY variables to pass along, nor are you reading any.
 
Old 11-03-2018, 04:10 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Here's the bash shell script I wrote to support Weather data in conky
It takes the fixed-size terminal (look at the date) and quarters it up then grabs conkyforecast data and paints the screen with the result(s)

Merely for comparative purposes. Conkyforecast has been dead'ish for sometime.
There are no decision-making processes in this script and that is what you will need to implement your solution, IMO.

Code:
#!/bin/bash
# 4 Day Weather forecast script for Boardman, OH
# Written by John Jones
# Updated  for slackware14_64 on Tue Oct 23, 2012
# Last edited: "Forecast" for Today without --startday 0 is "Current"
# Thanks to Sector11 for making me fix this ;)

# Clear the screen
tput clear

# Current Weather)
tput cup 0 0
echo "Current  for" `date | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 1 0
echo "-----------------------------"
tput cup 2 0
echo "Low:           "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --hideunits)$(printf \\260)F
tput cup 3 0
echo "High:          "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --hideunits)$(printf \\260)F
tput cup 4 0
echo "Humidity:      "$(/usr/bin/conkyForecast --location=USOH1096 -d HM --imperial --hidedegrees --hideunits)
tput cup 5 0
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --hideunits)
tput cup 6 0
tput bold; tput setaf 1;
echo "Precipitation: "$(/usr/bin/conkyForecast --location=USOH1096 -d PC)
tput sgr0

# --startday 1)
tput cup 12 0
echo "Forecast for" `date --date="1 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 13 0
echo "-----------------------------"
tput cup 14 0
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 1 --hideunits)$(printf \\260)F
tput cup 15 0
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial  --hidedegrees --startday 1 --hideunits)$(printf \\260)F
tput cup 16 0
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 1 --hideunits)
tput cup 17 0
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 1  --hideunits)
tput cup 18 0
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 1)
tput sgr0

# --startday 2)
tput cup 24 0
echo "Forecast for" `date --date="2 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 25 0
echo "-----------------------------"
tput cup 26 0
echo "Low:           "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 2 --hideunits)$(printf \\260)F
tput cup 27 0
echo "High:          "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial  --hidedegrees --startday 2 --hideunits)$(printf \\260)F
tput cup 28 0
echo "Humidity:      "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 2 --hideunits)
tput cup 29 0
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 2)
tput cup 30 0
tput bold; tput setaf 1;
echo "Precipitation: "$(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 2)
tput sgr0

# --startday 3
tput cup 0 40
echo "Forecast for" `date --date="3 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 1 40
echo "-----------------------------"
tput cup 2 40
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 3 --hideunits)$(printf \\260)F
tput cup 3 40
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial  --hidedegrees --startday 3 --hideunits)$(printf \\260)F
tput cup 4 40
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 3 --hideunits)
tput cup 5 40
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 3)
tput cup 6 40
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 3)
tput sgr0

# --startday 4
tput cup 12 40
echo "Forecast for" `date --date="4 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 13 40
echo "-----------------------------"
tput cup 14 40
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 4 --hideunits)$(printf \\260)F
tput cup 15 40
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --startday 4 --hideunits)$(printf \\260)F
tput cup 16 40
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 4 --hideunits)
tput cup 17 40
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 4)
tput cup 18 40
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 4)
tput sgr0

# --startday 5
tput cup 24 40
echo "Forecast for" `date --date="5 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 25 40
echo "-----------------------------"
tput cup 26 40
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 5 --hideunits)$(printf \\260)F
tput cup 27 40
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --startday 5 --hideunits)$(printf \\260)F
tput cup 28 40
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 5 --hideunits)
tput cup 29 40
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 5)
tput cup 30 40
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 5)
tput sgr0

# --startday 6
tput cup 0 78
echo "Forecast for" `date --date="6 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 1 78
echo "-----------------------------"
tput cup 2 78
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 6 --hideunits)$(printf \\260)F
tput cup 3 78
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --startday 6 --hideunits)$(printf \\260)F
tput cup 4 78
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 6 --hideunits)
tput cup 5 78
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 6)
tput cup 6 78
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 6)
tput sgr0

# --startday 7
tput cup 12 78
echo "Forecast for" `date --date="7 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 13 78
echo "-----------------------------"
tput cup 14 78
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 7 --hideunits)$(printf \\260)F
tput cup 15 78
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --startday 7 --hideunits)$(printf \\260)F
tput cup 16 78
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 7 --hideunits)
tput cup 17 78
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 7)
tput cup 18 78
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 7)
tput sgr0

# --startday 8
tput cup 24 78
echo "Forecast for" `date --date="8 day" | awk '{print $1 ", " $2 " "$3 "  "$6}'`
tput cup 25 78
echo "-----------------------------"
tput cup 26 78
echo "Low:            "$(/usr/bin/conkyForecast --location=USOH1096 -d LT --imperial --hidedegrees --startday 8 --hideunits)$(printf \\260)F
tput cup 27 78
echo "High:           "$(/usr/bin/conkyForecast --location=USOH1096 -d HT --imperial --hidedegrees --startday 8 --hideunits)$(printf \\260)F
tput cup 28 78
echo "Humidity:       "$(/usr/bin/conkyForecast --location=USOH1096 -d HM  --imperial --hidedegrees --startday 8 --hideunits)
tput cup 29 78
echo $(/usr/bin/conkyForecast --location=USOH1096 -d CT --startday 8)
tput cup 30 78
tput bold; tput setaf 1;
echo "Precipitation: " $(/usr/bin/conkyForecast --location=USOH1096 -d PC --startday 8)
tput sgr0
tput cup 36 0
Just a reference. IRL stuff that work{s,ed}
Shell Scripting Part 3: Decision Control Structures in Shell Scripts

Good Luck, and oh yeah...
Have fun.

John out.
 
Old 11-03-2018, 06:19 PM   #10
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,887

Rep: Reputation: 467Reputation: 467Reputation: 467Reputation: 467Reputation: 467
Ideally, you would use configuration file(s) for flexibility and it would be nice to use a language that can do recursive calls,
So you would only code for a single level of menus - no submenus.

It is possible to do this in BASH (with some care).

You could then have sub-sub-sub menus or whatever...
 
  


Reply


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
create a shell script with a menu tux'sbestfriend Programming 5 11-09-2015 08:24 AM
Create a menu in a shell script upnort Programming 12 08-13-2015 04:42 PM
Menu bashed shell script for users shell 0.o Linux - Server 6 02-14-2013 06:00 AM
[SOLVED] I need help with a CLI menu shell script BobTruck Programming 8 11-17-2009 06:41 PM
shell script menu marsIV Programming 1 10-09-2007 08:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:03 PM.

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