8

When I run this script it displays options in a menu style on my terminal and then runs the relavent command in the script.

Script Screen

#!/bin/sh
TEMP=/tmp/answer$$
dialog --ascii-lines --title "Administrative tasks"  --menu  "Tasks :" 20 0 0 1 "Display firewall settings" 2 "Restore firewall settings" 3 "Flush Firewall settings" 2>$TEMP
choice=`cat $TEMP`
case $choice in
        1)      iptables -L
                ;;
        2)      iptables-restore </etc/iptables.firewall.rules
                iptables -L
                ;;
        3)      iptables --flush
                iptables -L
                ;;
esac
echo Selected $choice

But when it exits the screen is messed up.

Is there a way to "save" the state of screen before I ran this and restore it?

Is there a better "Windows" scripting program that runs in a text screen?

Messed up Screen

3
  • How about using screen Aug 13, 2014 at 11:49
  • @ElefantPhace I ran this is a "screen' session and thought that was the issue, nah, it still messes the screen. Unless you mean piping the command into screen? Can that be done? (PS: When screen exits, it too clears the screen)
    – Meer Borg
    Aug 13, 2014 at 11:53
  • 1
    I was thinking more along the lines of keeping your main terminal in a screen then starting a new one for the dialog then returning to your main one after done with dialog. Can't test this ATM though Aug 13, 2014 at 12:01

4 Answers 4

16

I know it's late, but maybe you want to switch to the alternate screen (like nano, vim, and others do), so you could give a try to the --keep-tite option.

From the dialog's man page:

--keep-tite: Normally dialog checks to see if it is running in an xterm, and in that case tries to suppress the initialization strings that would make it switch to the alternate screen. Switching between the normal and alternate screens is visually distracting in a script which runs dialog several times. Use this option to allow dialog to use those initialization strings.

Here's an example:

echo "Write something before invoking dialog."
dialog --keep-tite --msgbox "Hello world!" 0 0

After the user hits OK, the output printed before invoking dialog ... is restored.

3
  • nope, problem persists (dialog 1.2 in gnome terminal 3.14)
    – vladkras
    Jan 14, 2016 at 8:38
  • 2
    This worked for me using dialog 1.2, bash 4.3 and several terminal emulators. Thank you.
    – Enrico
    Mar 19, 2016 at 18:31
  • This was it! By using the --keep-tite switch, the previous screen comes back upon exiting the dialog command. This is definitely the cleanest option, thank you. Mar 20 at 13:02
3
+100

The dialog manpage mentions whiptail (in a rather deprecating fashion). It does not have the --ascii-lines option, but it does not mess up the screen either:

terminal screenshot with aftermaths of dialog and whiptail

The script:

#!/bin/sh
TEMP=/tmp/answer$$
whiptail --title "Administrative tasks"  --menu  "Tasks :" 20 0 0 1 "Display firewall settings" 2 "Restore firewall settings" 3 "Flush Firewall settings" 2>$TEMP
choice=`cat $TEMP`
case $choice in
        1)      echo 1 #iptables -L
                ;;
        2)      echo 2 #iptables-restore </etc/iptables.firewall.rules
                #iptables -L
                ;;
        3)      echo 3 #iptables --flush
                #iptables -L
                ;;
esac
echo Selected $choice

The display from whiptail: screenshot of whiptail in action

Among other things, whiptail is based on newt instead of ncurses. It is also a dependency of ubuntu-minimal, so it should be installed on all Ubuntu systems by default (at least, as of 14.04).

2
  • Very nice, fits the bill nicely, will do some more testing but looks like this is the solution I need. Thanks.
    – Meer Borg
    Aug 24, 2014 at 7:33
  • Perfect seems it doesn't need the --ascii-lines parameter as it works. Was easy to update script, straight replace of commands.
    – Meer Borg
    Aug 24, 2014 at 16:39
1

Just add clear after the dialog line:

...
dialog --ascii-lines --title "Administrative tasks"  --menu  "Tasks :" 20 0 0 1 "Display firewall settings" 2 "Restore firewall settings" 3 "Flush Firewall settings" 2>$TEMP
clear #clears the terminal screen
choice=`cat $TEMP`
case $choice in
...
1
  • 4
    That clears the screen, all previous text is lost. Hence the need to "restore" the screen state. I had the clear in there myself but I would like to restore the screen state. That way I can still see the text that was on the screen before I executed the command.
    – Meer Borg
    Aug 13, 2014 at 11:43
1

This is possible with the screen command.

run this using screen as below,

screen bash yourcommand.sh

This would show the dialog screen then collect the choice. Also this would restore the existing screen on exit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .