Monday, 14 March 2016

Unix/Linux Shell Variables

Variables in Unix/Linux Shell

To process our data/information, data must be kept in computers RAM memory. RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable (Its a named storage location that may take different values, but only one at a time).

In Unix/Linux(Shell), there are two types of variable:

1)  System variables : 

    Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
    You can see system variables by giving command like $ set, There is a long list of system variable
    so if you interested to see all the system variable then you should redirect the all system variable
    into a text file by using command as "set > systemVariable.txt". Now check systemVariable.txt in
    your current directory.

Some of the important System variables are as following:
   
System Variable Description
BASH=/bin/bash System Shell Name
BASH_VERSION=4.2.25(1)-release System Shell Version Name
COLUMNS=80 Number of Column for your screen
LINES=34 Number of Lines for your screen
HOME=/home/mukesh Your Shell's Home Directory
LOGNAME=mukesh Logging Name of Current System
OSTYPE=Linux Our Operating System Type
PATH=/usr/bin:/sbin:/bin:/usr/sbin Our System's Path Setting
PS1=[\u@\h \W]\$ Our Prompt Settings
SHELL=/bin/bash Our Shell Name
PWD=/home/mukesh/Documents Our Current Working Directory
USERNAME=mukesh Username who is currently log in into system
   
   
    Note :Some of the above settings can be different in your Unix/Linux environment as per your system.
    You can print any of the above variables contains as follows:

    $ echo $USERNAME
    $ echo $BASH_VERSION



2) User defined variables : 
  •   Created and maintained by user. This type of variable defined in lower letters.
  •   User-defined variable can be assigned a value.
  •   The value of a named variable can be retrieved by preceding the variable name with a ‘$’ sign
  •   Used extensively in shell-scripts.
  •   Used for reading data, storing and displaying it.

    Accepting Data and Displaying It on Screen:

  •  To accept input data from user you can use read command.
  •  In shell scripts , read is used for accepting data from the user and storing it for other processing.
  •  Example : $ read yourname

  • Type command "read yourname" and hit enter. Type your name and hit enter again. So now your name has been stored into user defined varaible name yourname.

    Displaying Value:
    To display the value you can use the echo command.
    So to display your name which you have stored into a variable yourname type the following command.

      echo $yourname



Wednesday, 9 March 2016

Shell Script Programs For Beginners - SET 01

1). Writing your first script and getting it to work .
2). Script which shows a demo of command line arguments.
3). Script to print user login information with current date and time.
4). To read input from user and showing demo of user defined variable.
5). Script to show an example of user defined variable.
6). Script to sum 2 numbers given by user.
7). Script to show sum of numbers with decimal points
8). Script to find the biggest no from the 3 numbers
9). Script to print 1 to 10 numbers using while loop
10). Script to show a demo of for loop

1). Writing your first script and getting it to work .

Step 1 : writing the script
You can write script either on note pad or by using vi editor.Let us use vi editor. Open your Linux terminal and create a file helloWorld.sh for this type "vi helloWorld.sh" and hit enter . Now hit ' i ' to go in insert mode and write the following code in your file.
#!/bin/bash
#
# Script name : helloWorld.sh

echo "Hello world !"
To save helloWorld.sh first hit 'esc' to come out from insert mode and then type ':wq!' and hit enter. Now type 'ls' command to see your file.

Step 2 : Give the execute permission to the file helloWorld.sh ,run the following command
mukesh@ubuntu:~$ chmod 755 helloWorld.sh 
            (or)
mukesh@ubuntu:~$ chmod +x helloWorld.sh 
Step 3 : Run the helloWorld.sh
mukesh@ubuntu:~$ ./helloWorld.sh
Hello world !
             (or)
mukesh@ubuntu:~$ sh helloWorld.sh 
Hello world !
2).Script which shows a demo of command line arguments.
#!/bin/bash
#
#Script Name : commandLineArgument.sh
#Description : The Script will show ,command line argument demo
#
clear
if [ $# -le 0 ]
then
        echo "usage :- $0 arg1 arg2 arg3 ...argN"
        echo "where arg1,arg2...argN are command line argument"
else
        echo "Total no of command line argument are $#"
        echo "$0 is the Script Name"
        echo "$1 is the first argument"
        echo "$2 is the second argument"
        echo "all of them are $* or $@"
fi
3).Script to print user login information with current date and time.
#!/bin/bash
#
#Script name : userLoginDEtail.sh
#Author      : mukesh kumar
#Description : Script to print user information who currently login ,current date & time
#
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry is `pwd`"
echo "==========Another way==========="
clear
echo "Hello $USER"
echo -e "Today is \c ";date
echo -e "Number of user login :\c ";who | wc -l
echo "Calendar"
cal
exit 0 
4).To read input from user and showing demo of user defined variable.
#!/bin/bash
#
#Script Name : readUserInputandUDV.sh
#Description : To read input from user and showing demo of user defined variable.
#
clear
echo  "Please ! Enter your Name :"
read Name
echo "hello $Name ,How are you ?"
myname=Mukesh
myos=linux
echo "My name is $myname and my operating system is $myos"
5).Script to show an example of user defined variable.
#!/bin/bash
#
#Script Name : userDefinedVariable.sh
#Description : Script to demonstrate the UDV.
#
myname=Mukesh
myos=linux
nyno=242100
echo "My Name is $myname"
echo my os is $myos"
echo "My number is myno, can u see this number ?"
6).Script to show sum of two numbers.
#!/bin/bash
#
#Script Name : sumofNumber.sh
#Description : Script to sum of two numbers.
#
clear
if [ $# -ne 2 ]
then
        echo "usage - $0 x y"
        echo "          where x and y are 2 numbers"
        exit 1
else
        echo "sum of $1 and $2 is `expr $1 + $2`"
        exit 0
fi
7). Script to show sum of numbers with decimal points.
#!/bin/bash
#
#Script Name : sumDecimalPoint.sh
#Description : Script to show sum of numbers with decimal points
#

if [ $# -ne 2 ]
then
        echo "Usage : $0 num1 num2 "
        echo "give 2 nos with argument with decimal point,i will sum then"
        exit 1
fi
n1=$1
n2=$2
sum=0
sum=`echo $n1 + $n2 | bc`
echo "sum of $n1 + $n2 = $sum"
8). Script to find the biggest no from the 3 numbers.
#!/bin/bash
#
# Script Name : biggestNumber.sh
# Description : Script to find the biggest no from the 3 numbers
#
if [ $# -ne 3 ]
then
        echo "Usage - $0 n1 n2 n3"
        echo "          where n1 ,n2 and n3 are number among which biggest no has to find"
        exit 1
fi
n1=$1
n2=$2
n3=$3

if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
        echo "$n1 is the biggest number"

elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
        echo "$n2 is the biggest number"

elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
        echo "$n3 is the biggest number"

elif [ $n1 -eq $n2 ] && [ $n1 -eq $n3 ] && [ $n2 -eq $n3 ]
then
        echo "all numbers are equal"
else
        echo "I couldn't figure out which number is gretest"
fi
9). Script to print 1 to 10 numbers using while loop.
#!/bin/bash
#
#Script Name : whileLoop.sh 
#Description : while loop to print 1 to 10 numbers.
#

i=1
while [ $i -le 10 ]
do
        echo "$i"
        i=`expr $i + 1`
done
10). Script to show a demo of for loop.
#!/bin/bash
#
#Script Name : forLoop.sh
#Description : Script showing demo of for loop
#

for val in a b c d e f g h i j
do
        echo $val
done

Tuesday, 8 March 2016

Using VLOOKUP() function in Excel

VLOOKUP() function is very useful when you want to update a column values by taking some reference from another sheet or same sheet column values.

Here i have two sample sheet first sheet is "Student" in which we have three columns (student name, Grade, Marks) and second sheet is "Grade" in which we have two columns (Grade, marks) as showing in following images.

Sample Sheet01 "Student" :




Sample Sheet02 "Grade" :




Now our objective is to update the column "Marks" of "Student" sheet by referring "Grade" column of second sheet "Grade" using VLOOKUP() function. Following are the steps to update column "Marks" using VLOOKUP() :


  • Select first cell (C2) of marks in  "Student" sheet and hit on formula tab and click on 'Lookup & Reference' and then hit on VLOOKUP as highlighted in below image.



  •  A lookup window will pop-up as showing below image. Select whole column "B" (Grade) to provide the value in "Lookup_value"  .





  • To give the value in "Table_Array" select second sheet and select both the column "Grade" and "Marks"  and give "Col_index_num" = 2 as 'Marks' is in the second column which need to be refer and for "Range_lookup" give as "False" as showing in below image.     
   

  •  Once you complete above steps hit on "OK" button and you can see a complete vlookup formula as =VLOOKUP(B:B,Grade!A:B,2,FALSE) . which will update the cell "C2" with respective marks. Drag and drop it till end to update all the values.




Nagios Plugin to check used space

Here is very simple Nagios plugin in shells script which can be be used to check the used disc space in server and accordingly can be get alert mail.

  • If used space less than 70%, then output status remains "OK"
  • if used space >=70% and < 90%, then output status will be "WARNING"
  • If used space >=90%, then output status will be "CRITICAL"
  • If none of the above, then output status will be "Unknown" 

Following is the script for the above requirements :
#!/bin/bash

# Script Name : usedSpace.sh
# Description : This script will check total used space in % of current disc .


used_space=`df -h / | grep -v Filesystem | awk '{print $5}' | sed 's/%//g'`

case "$used_space" in
        [1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-9])
                echo "OK - $used_space% of disk space used."
                exit 0
                ;;
        7[0-9]|8[0-9])
                echo "WARNING - $used_space% of disk space used."
                exit 1
                ;;
        9[0-9]|100)
                echo "CRITICAL - $used_space% of disk space used."
                exit 2
                ;;
        *)
                echo "UNKNOWN - $used_space% of disk space used."
                exit 3
                ;;
esac


You can change script as per your used space percentage and can set the email alerting if it is in "WARNING", "CRITICAL" or "UNKNOWN" state.


Related Posts Plugin for WordPress, Blogger...