Tuesday, July 19, 2016

Linux/Scripting - drawing dialog boxes in shell/tui/terminal

This post is a cookie . I m writing this in my limited free time : ) but it is interesting , as I will explain how to draw dialog boxes in standard linux shells. (note that I m not talking about X windows)

Let's start;

In order to draw a dialog box, we install dialog utility using yum. (my demo enviroment is Oracle Linux)

[root@demoorcl ~]#yum install dialog
...
.....
........
Installed:
  dialog.x86_64 0:1.1-9.20080819.1.el6                            

Then we look at the definition: via "man dialog" command.
manual says " dialog - display dialog boxes from shell scripts" :)

Next, we write a shell script to use the features of this dialog utility. 
A shell script like the one below;

#!/bin/sh
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

$DIALOG  --ok-label "Do the selected!" --cancel-label Exit --clear --title "Admin Interface" \
        --menu "Choose the action to be taken:" 20 70 15 \
        "1"  "Check the integration" \
        "2"  "Shut down the integration Services" \
        "3"  "Startup the integration Services" \
        "4"  "Check the other services" \
        "5"  "Shut down the other services" \
        "6"  "Check the processes"  2> $tempfile
retval=$?

Next, we execute the script, and here our dialog is displayed... ->


Well, that is it.. We display a standard dialog. 
In this simple dialog, when we hit exit button, the dialog is closed, but naturally;  there is no event handler for "the selections"/"Do the selected" buttons, as our script did not include those events.

But if we want to go further and put all the event handlers, we can enhance our script and make it production ready .

An example script in this context is like the following;

Note that: This script is used in a production system, so I changed some paths and commented out some action lines. However,  when you analyze it you will get idea and use the info for your own benefit.

#!/bin/sh
script=/root
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$


$DIALOG  --ok-label "Do the selected!" --cancel-label Exit --clear --title "Admin Interface" \
        --menu "Choose the action to be taken:" 20 70 15 \
        "1"  "Check the integration" \
        "2"  "Shut down the integration Services" \
        "3"  "Startup the integration Services" \
        "4"  "Check the other services" \
        "5"  "Shut down the other services" \
        "6"  "Check the processes"  2> $tempfile
retval=$?

choice=`cat $tempfile`

case $retval in
  0)
    if [ $choice == "1" ]
    then
    ps -aux | grep eventprocessor | grep -v grep | grep -v Warning > /tmp/kontrol1
    cat /tmp/kontrol1 | grep eventprocessor
    if [ $? = "0" ]
     then
     dialog --exit-label "Back to Main Menu" --title "  ENTEGRASYON KONTROL  " --textbox  $script/arayuz_durum1.txt 22 70
     sh $script/main
    else
     dialog --exit-label "Back to Main Menu" --title "  ENTEGRASYON KONTROL  " --textbox  $script/arayuz_durum2.txt 22 70
    sh $script/main
     fi
    elif [ $choice == "4" ]
    then
    ps -aux | grep tomcat | grep -v grep | grep -v Warning > /tmp/kontrol2
    cat /tmp/kontrol2 | grep tomcat
     if [ $? = "0" ]
     then
     dialog --exit-label "Back to Main Menu" --title "  ARAYUZ KONTROL  " --textbox  $script/tomcat_durum1.txt 22 70
     sh $script/main
    else
     dialog --exit-label "Back to Main Menu" --title "  ARAYUZ KONTROL  " --textbox  $script/tomcat_durum2.txt 22 70
    sh $script/main
     fi
    elif [ $choice == "2" ]
    then
    ps -aux | grep eventprocessor | grep -v grep | grep -v Warning| awk '{print $2}'  > /tmp/arayuz_pid
     arayuz_pid=`cat /tmp/arayuz_pid|grep -v Warning`
     sudo kill $arayuz_pid
    dialog --exit-label "Back to Main Menu" --title "  ENTEGRASYON KAPATILDI  " --textbox  $script/arayuz_kapatildi.txt 22 70
     sh $script/main
     elif [ $choice == "3" ]
    then
    sudo nohup sh /erm/eventprocessor/bin/run &
     dialog --exit-label "Back to Main Menu" --title "  ENTEGRASYON ACILDI  " --textbox  $script/arayuz_acildi.txt 22 70
     sh $script/main
     elif [ $choice == "5" ]
    then
   #sudo sh /erm/tomcat/apache-tomcat-5.5.17/bin/shutdown.sh &>/dev/null
 sh /erm/tomcat/apache-tomcat-5.5.17/bin/shutdown.sh &>/dev/null
dialog --exit-label "Back to Main Menu" --title "  ARAYUZ  KAPATILDI  " --textbox  $script/tomcat_kapatildi.txt 22 70
    sh $script/main
   elif [ $choice == "6" ]
    then
sudo nohup sh /erm/tomcat/apache-tomcat-5.5.17/bin/startup.sh
#sudo sh  /erm/tomcat/apache-tomcat-5.5.17/bin/startup.sh &>/dev/null
   # sudo sh /home/helpdesk/arayuz_script/tomcat_start.sh
dialog --exit-label "Back to Main Menu" --title "  ARAYUZ  ACILDI  " --textbox  $script/tomcat_acildi.txt 22 70
sh $script/main
     else
     echo ...
    fi
   ;;
   1)
    echo "Program Exited"
;;
  255)
    echo "ESC pressed."
;;
esac

Okay, here we go..
Let's execute this script (named Main)

sh Main

We press enter(equivalent to "Do the selected") while our selection line is on "Check the integration";


Well, the action is taken in the backround to check the integration control, and the status of the integration is reported in the dialog :) .. Actually, I modified the script to not to show what is done there, as it is a production code :)

Okay. Lets press enter while our selection line is on "Back to Main Menu"

Here we are in Main Menu again .. 
So that is it. You can go further and create your own dialogs, you can  even add control mechanisms and enhance your dialogs accordingly. By the help of this admin dialogs, you can ease the job of Linux and Oracle admins and even offload some works to the Operators.

That's enough for today.
See you in next blog posts :)

No comments :

Post a Comment

If you will ask a question, please don't comment here..

For your questions, please create an issue into my forum.

Forum Link: http://ermanarslan.blogspot.com.tr/p/forum.html

Register and create an issue in the related category.
I will support you from there.