Friday, November 14, 2014

Linux -- Using Linux Dialog and a handy Bash Script for Database Interaction

This script is written to manipulate a single table data.. It works like a dialog, but it is not a dialog actually, it is a script in which we use case,functions and other commands like sqlplus..
It is a good example for orderly writing bash scripts  , since it is function based..
The script is written to add , remove and read the data in an oracle database table.
It takes the password from the user, but it does not display it on the terminal for security reasons.

In past years , I have done these kind of works using Linux dialog. 
Using Linux Dialog, I could provide a form on the terminal . The users can take actions using selections on the form ; like the following;


Above script was written for easing the system administration in EBS apps Tier..
Users of this interface should not be System administrators. Everyone can use this application and make administration operations.
User will connect to the system using ssh, and this apps tech interface menu will be displayed in its terminal( putty or ssh secure shell or zoc does not matter)..
Whenever the user quits from the application , the connection will be closed too. So linux will act like a kiosk machine.


So , when you compare the following script with the script above, you can say that the following script is a primitive one.
On the other hand ,I want to share it because it is well ordered and it is a good example for beginners.

The script is as follows;

#START OF THE SCRIPT
echo "Welcome to Table Data management"

mainmenu () {
echo "What would you want to do?"
echo "Enter 1 to add data to the  list"
echo "Enter 2 to delete data from the exception list"
echo "Enter 3 to display data in the exception list"
echo "Enter 4 to exit"
read selection
processtheselection
}

insertuser()
{
echo "Please enter the username that you want to add to the exception list"
read username
echo "Please supply me the APPS schema password for this operation":
read  -s apps_schema_password
sqlplus -s apps/$apps_schema_password <<EOF
insert into blabla_table values (upper('$username'));
commit;
EOF
}

deleteuser()
{
echo "Please enter the username that you want to delete from the exception list"
read username
echo "Please supply me the APPS schema password for this operation":
read  -s apps_schema_password
sqlplus -s apps/$apps_schema_password <<EOF
delete from blabla_table where upper(username)=upper('$username');
commit;
EOF
}

displaylist()
{
echo "Please supply me the APPS schema password for this operation":
read  -s apps_schema_password
echo "Users in the Exception list:"
sqlplus -s apps/$apps_schema_password <<EOF
set pagesize 100
set linesize 120
select upper(username) as "EXCEPTION LIST USERS" from blabla_table;
EOF
}

exitapp()
{
echo "Exiting from the Application"
exit;
}

processtheselection()
{
case $selection in
    1) echo "You have selected  --add an user--..."
        insertuser
        mainmenu
      ;;
    2)  echo "You have selected --delete an user--..."
        deleteuser
        mainmenu
      ;;
    3)  echo "You have seleted --display the exception list--..."
        displaylist
        mainmenu
      ;;
    4) exitapp
        echo "Exiting from application"
        exit
      ;;
    *)  echo "Please enter a valid number"
     mainmenu
     ;;
esac
}

mainmenu;
##END OF THE SCRIPT


Example Run:



Welcome to SSO User Management
What would you want to do?
Enter 1 to add user to the exception list
Enter 2 to delete a user from the exception list
Enter 3 to display the users in the exception list
Enter 4 to exit
3
You have seleted --display the exception list--...
Please supply me the APPS schema password for this operation:
Users in the Exception list:

EXCEPTION LIST USERS
----------------------------------------------------------------------------------------------------
ERBANT
ALI
2 rows selected.

What would you want to do?
Enter 1 to add user to the exception list
Enter 2 to delete a user from the exception list
Enter 3 to display the users in the exception list
Enter 4 to exit
1
You have selected --add an user--...
Please enter the username that you want to add to the exception list
ERMAN
Please supply me the APPS schema password for this operation:

1 row created.
Commit complete.

What would you want to do?
Enter 1 to add data to the exception list
Enter 2 to delete data from the exception list
Enter 3 to display data in the exception list
Enter 4 to exit
3
You have seleted --display the exception list--...
Please supply me the APPS schema password for this operation:
Users in the Exception list:

EXCEPTION LIST USERS
----------------------------------------------------------------------------------------------------
ERBANT
ALI
ERMAN
3 rows selected.

What would you want to do?
Enter 1 to add data to the exception list
Enter 2 to delete data from the exception list
Enter 3 to display data in the exception list
Enter 4 to exit
2
You have selected --delete an user--...
Please enter the username that you want to delete from the exception list
ERMAN
Please supply me the APPS schema password for this operation:

1 row deleted.
Commit complete.

What would you want to do?
Enter 1 to data to the exception list
Enter 2 to delete data from the exception list
Enter 3 to display data in the exception list
Enter 4 to exit
3
You have seleted --display the exception list--...
Please supply me the APPS schema password for this operation:
Users in the Exception list:

EXCEPTION LIST USERS
----------------------------------------------------------------------------------------------------
ERBANT
ALI
2 rows selected.

What would you want to do?
Enter 1 to add data to the exception list
Enter 2 to delete data from the exception list
Enter 3 to display data in the exception list
Enter 4 to exit
4
Exiting from the Application

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.