5

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

  • How about using screen – ElefantPhace Aug 13 '14 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 '14 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 – ElefantPhace Aug 13 '14 at 12:01
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).

| improve this answer | |
  • 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 '14 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 '14 at 16:39
8

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.

| improve this answer | |
  • nope, problem persists (dialog 1.2 in gnome terminal 3.14) – vladkras Jan 14 '16 at 8:38
  • 1
    This worked for me using dialog 1.2, bash 4.3 and several terminal emulators. Thank you. – Enrico Mar 19 '16 at 18:31
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
...
| improve this answer | |
  • 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 '14 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.