Friday, August 31, 2012

Login from Unix to any database and running a query

Script below is used to login by any user and to keep the password hidden (not displayed on the screen)

#!/bin/bash
#Usage
# Pass Username & Connectsting
#password will be asked at login time
#read -s is used to hide password and it will not been displayed to the user

if [ "$1" = "" ] || [ "$2" = "" ]; then
   echo "Please pass Username and Database name to connect"
else
echo "Enter Pass"
read -s password
sqlplus -s "$1/$password@$2" << EOF
select sysdate from dual
/
EOF
fi

Wednesday, August 22, 2012

Hide Password on Unix prompt

Hide Password on Unix Prompt

While we are dealing with the password. Security is very essential. There are no of ways to do the same. Storing the password's in a file and securing it by using tight access rights on the file to user's and groups.

If there are no of users using the shell script and using their own login credentials, Most effective way is to hide the password on the screen.

Below script depicts the similar use of the command :-

#!/bin/bash
#Usage
# Pass Username & Connectsting
#password will be asked at login time

if [ "$1" = "" ] || [ "$2" = "" ]; then
   echo "Please pass Username and Database name to connect"
else
echo "Enter Pass"
read -s password
sqlplus -s "$1/$password@$2" << EOF
select sysdate from dual
/
EOF
fi

Script checks for the user name and database name in case user did not provide the same it will return the message and exit.

read -s password
will read the password from the user prompt however will not show the alphabets typed by the user and keep it secure since it is not available anywhere except with the user.

Creating a Menu in Unix Shell Script

Today I learned how to create a menu to display and choose from the menu. It has became a Menu driven program. which internally function based on your choice:-

Below is the sample script.


#!/bin/sh
# Wedding guest meals
# These variables hold the counters.
NUM_CHICKEN=0
NUM_STEAK=0
ERR_MSG=""
# This will clear the screen before displaying the menu.
clear
while :
do
        # If error exists, display it
        if [ "$ERR_MSG" != "" ]; then
                echo "Error: $ERR_MSG"
                echo ""
        fi
        # Write out the menu options...
        echo "Chicken: $NUM_CHICKEN"
        echo "Steak: $NUM_STEAK"
        echo ""
        echo "Select an option:"
        echo " * 1: Chicken"
        echo " * 2: Steak"
        echo " * 3: Exit"
        # Clear the error message
        ERR_MSG=""
        # Read the user input
        read SEL
        case $SEL in
                1) NUM_CHICKEN=`expr $NUM_CHICKEN + 1` ;;
                2) NUM_STEAK=`expr $NUM_STEAK + 1` ;;
                3) echo "Bye!"; exit ;;
                *) ERR_MSG="Please enter a valid option!"
        esac
        # This will clear the screen so we can redisplay the menu.
        clear
done


Execution:-

Call the script - ./menu.sh

Below will be shown on the screen, Now choose the option.. say I choose 1 then then counter for chicken will counter to 1 and so on..

Chicken: 0
Steak: 0
Select an option:
 * 1: Chicken
 * 2: Steak
 * 3: Exit