LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-11-2013, 02:15 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Rep: Reputation: 1
shell script dialog box question


I am able to display a dialog box using following code :
Code:
dialog --clear  --help-button --backtitle "Linux Shell Script Dialog Box" --title "[ M A I N - M E N U ]" --menu "Here is the description of the menu\nSelect option" 15 50 4 first "define first function" second "Define second" Third "Define third" Exit "Exit"
It is displayed successfully, now I want to write functionality for ok & cancel button. How should I do this?

If I select first option, select OK and press return key, it should run the define function like display date but if I select cancel for same option, it should exit the dialog box.

Please suggest how to enable ok, cancel and help functionality.
 
Old 10-11-2013, 02:22 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
see man page of dialog:
Exit status is subject to being overridden by environment variables. Normally they are:

0 if dialog is exited by pressing the Yes or OK button.
1 if the No or Cancel button is pressed.
2 if the Help button is pressed.
3 if the Extra button is pressed.
-1 if errors occur inside dialog or dialog is exited by pressing the ESC key.

So you need to check the return code and act upon that value
 
Old 10-11-2013, 03:29 AM   #3
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Thanks for your reply
Till now, I have created this.
Code:
#!/bin/bash
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$

menuitem=`cat /tmp/menuitem.$$`

opt=$?

case $menuitem in
Date/time) date;;
Calendar) cal;;
Editor) vi;;
esac
Cancel button is working in this script but I want to use loop in this so it will not exit the script and it will return back on menu page, that's why I created this.
Code:
#!/bin/bash
while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$

menuitem=`cat /tmp/menuitem.$$`

opt=$?

case $menuitem in
Date/time) date;;
Calendar) cal;;
Editor) vi;;
esac
done
But it sticks on menu page and it does not display the outputs.
I want it should show the menu again when I press return key after showing output and if I press cancel, it should show the command prompt.
Only VI editor option works like I want, not the first two commands.
Please help.
 
Old 10-11-2013, 03:38 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
opt=$? should be moved above menuitem=`cat /tmp/menuitem.$$`
you can construct a loop based on $opt.
 
Old 10-11-2013, 09:51 AM   #5
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Thanks for your quick help. I want to add one more functionality to the script.

Right now it shows the date and immediately it shows the menu because it is in loop but I want it should show the date until I press the return key. I have used the sleep command with date but it does not show the menu until the time is up. I want it should show the date until return key is pressed. Can it be done in this script?

Please help.
 
Old 10-12-2013, 02:23 AM   #6
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
I am not sure I have understood the same thing what you are saying although I am trying to say my point.

I think you want, it should display the output until you press return key. For this, you can define a function so it will display the output and when you press return key, it will show the menu again. If you press any other key except return, it will take you on command prompt.

I have taken your script from third post and I have made some arrangement. You have to make the script better according to your requirement. I have made it little lengthy so it will be easier for you.

No body can make it according to your requirement except you.

Code:
#!/bin/bash
onlydate(){
date
read -s -n 1 key  
if [[ $key = "" ]]; then 
function123
else
exit
fi
}

onlycal(){
cal
read -s -n 1 key  
if [[ $key = "" ]]; then 
function123
else
exit
fi
}

function123(){
while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$
menuitem=`cat /tmp/menuitem.$$`
case $menuitem in
Date/time) onlydate ;;
Calendar) onlycal ;;
Editor) vi ;;
esac
done
}

function123
1. While loop shows the dialog box menu again and again until you press any other key except return when your output is displayed.
2) If you select first option or second option in menu and press return key, it displays the o/p and it does not show the menu until you press return key again.
3) It returns on command prompt when you press any other key except return when your o/p is displayed, if you do not want it just remove else statement from first two functions.
 
Old 10-13-2013, 03:42 AM   #7
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Your script works like I need. It sticks on the output and does not reach on menu until I press return key. That exactly what I want.
But there is very small problem, Cancel button is not working in your script. When I select cancel and press return key, it shows blank output and retuns on menu instantly.

I have searched on google that how to write function for cancel but I couldnt find this.

I did not want to bother you again but I couldnt get the solution so I am asking here again.

If I remove the loop, cancel works perfectly but it does not work with the loop.
It shows the menu again with the loop. I want it should exit the script and show the command prompt if I select cancel and press return key.

Please help to get it worked with the loop.

Thanks

Regards
Sam
 
Old 10-14-2013, 01:31 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
there are several problems with that script:
1. the return code of dialog is not handled ($opt was used before). As I wrote you this is the way to handle Cancel.
2. the loop has no exit condition at all, that's why you cannot exit that loop.
3. additionally there is a recursive call to function123 (I would say bad practice, it may cause problems, but in this particular case probably will not cause any issues).

based on 1. and 2. you can exit the loop like:
[[ "$opt" -eq 1 ]] && exit
 
1 members found this post helpful.
Old 10-14-2013, 02:38 AM   #9
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
there are several problems with that script:
1. the return code of dialog is not handled ($opt was used before). As I wrote you this is the way to handle Cancel.
2. the loop has no exit condition at all, that's why you cannot exit that loop.
3. additionally there is a recursive call to function123 (I would say bad practice, it may cause problems, but in this particular case probably will not cause any issues).
Thanks for your expert advice. Now I have made the script according to your advice. I have removed the recursive call, I have given the exit condition for the loop. Now it is working exactly as I want, but as you said it was a bad script and there are several problems with it.

What can I do more to make it good and better script? Please give your suggestions. It will really helpful. Right now the script looks like this.

Code:
#!/bin/bash
onlydate(){
date
read -s -n 1 key  
if [[ $key = "" ]]; then 
function123
else
exit
fi
}

onlycal(){
cal
read -s -n 1 key  
if [[ $key = "" ]]; then 
function123
else
exit
fi
}


while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$
opt=$?
[[ "$opt" -eq 1 ]] && exit
menuitem=`cat /tmp/menuitem.$$`
case $menuitem in
Date/time) onlydate ;;
Calendar) onlycal ;;
Editor) vi ;;
esac
done

Last edited by unclesamcrazy; 10-14-2013 at 03:15 AM.
 
Old 10-14-2013, 02:53 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
if you have problem(s) just tell us and we will help you to fix them, but I don't want to find your problems.
Actually there is a call to a non-existent function function123
 
Old 10-14-2013, 03:17 AM   #11
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Sorry i didn't mean that. You will not take more than a second to debug it just like you found undefined function.
But it will be a whole day for me.

But I am not able to understand how is it working with undefined function but if I remove the function, the script does not work.

I wanted it should show the output until I press return key and if I press any other key, it should exit.

Please suggest.
 
Old 10-14-2013, 03:39 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
You need to post what you changed otherwise it will be really hard to tell anything useful. It looks like a tutorial for you and the goal is to understand how it works (if/then/else, while loop, functions ...). I suggest you to read man page and read error message and try to fix yourself.
 
Old 10-14-2013, 03:59 AM   #13
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
I have made these changes to the script.
Code:
#!/bin/bash
onlydate(){
date
read -s -n 1 key  
if [[ $key = "" ]]; then 
echo ""
else
exit
fi
}

onlycal(){
cal
read -s -n 1 key  
if [[ $key = "" ]]; then 
echo ""
else
exit
fi
}


while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$
opt=$?
[[ "$opt" -eq 1 ]] && exit
menuitem=`cat /tmp/menuitem.$$`
case $menuitem in
Date/time) onlydate ;;
Calendar) onlycal ;;
Editor) vi ;;
esac
done
 
Old 10-14-2013, 04:24 AM   #14
karim.ouda
Member
 
Registered: Jun 2013
Posts: 41

Rep: Reputation: 3
Try this :
Code:
#!/bin/bash
while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$
opt=$?
[[ "$opt" -eq 1 ]] && exit
menuitem=`cat /tmp/menuitem.$$`
case $menuitem in
Date/time) date && read -s -n 1 key && [[ $key = "" ]] ;;
Calendar) cal && read -s -n 1 key && [[ $key = "" ]] ;;
Editor) vi ;;
esac
done
 
Old 10-14-2013, 05:58 AM   #15
unclesamcrazy
Member
 
Registered: May 2013
Posts: 188

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by karim.ouda View Post
Try this :
Code:
#!/bin/bash
while true
do
dialog --backtitle "Linux Shell Script Tutorial " --title "Main Menu" --menu "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Date/time "Shows Date and Time" Calendar "To see calendar " Editor "To start vi editor " 2>/tmp/menuitem.$$
opt=$?
[[ "$opt" -eq 1 ]] && exit
menuitem=`cat /tmp/menuitem.$$`
case $menuitem in
Date/time) date && read -s -n 1 key && [[ $key = "" ]] ;;
Calendar) cal && read -s -n 1 key && [[ $key = "" ]] ;;
Editor) vi ;;
esac
done
If I do not use && [[ $key = "" ]] in the script, it gives the same result and it does not exit the script if I press any other key while displaying output. It shows menu again. Menu should be appeared only if I press return key.
It should return on command prompt if I press any other key while displaying output.
 
  


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
dialog and other GUIs in shell script!!! a.toraby Solaris / OpenSolaris 1 10-12-2008 06:19 AM
Using Dialog in Shell Script kushalkoolwal Programming 30 06-04-2007 03:52 AM
shell script using dialog box sharma.kashyap Linux - General 2 04-13-2007 09:28 AM
Need help creating/using a password dialog box for an Expect script crazygyhrous Linux - General 1 12-13-2005 12:48 PM
shell script with dialog sqn Programming 3 04-08-2004 03:25 PM


All times are GMT -5. The time now is 08: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration