Untitled Document
Functions In Shell
1. Program - Simple Function Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Invoke and Run a Function
                                                                                                                                       
        function hello ()   #-- Function Body                                                                                                      
        {
           echo " Hello World"                                                                                   
           echo " Hello user `who am i`"        
        }                                         
            hello # -- Function Call    
    
2. Program - To do Sum of two numbers Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To do Sum of two numbers
       
       function sum ()  # -- Function Body and Declaration                                                 
       {                                                  
         add=$(( $num1 + $num2 ))                                                   
       }                                                   
       echo "Enter Any 2 numbers"    
       read num1                                               
       read num2 
       
       sum $num1 $num2    # -- Calling function sum and pasing variables to function                                         

       echo "Sum of Two numbers $num1 + $num2 is : $add "
       
3. Program - To get the background jobs that are runing with given id Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       #############################################################
       # To get the background jobs that are runing with given id 
       #############################################################

       get_background_jobs()
     {
	   user_id=$1 
	   #$1 receives the 1st arguement passed to the function call. similarly $2 receives the 2nd arguement and so on.
	   echo "USER ID -$user_id"
	   echo "Jobs running with $yser_id are - "
	   ps -ef | grep "$user_id" 	
     }

        echo -n "Provide the user ID - "
        read id
        get_background_jobs $id
        # arguement passed to the function is $id

        OUTPUT:
        Provide the user ID - mangadaaku
        Jobs running with mangadaku are -
        (LIST OF JOBS)
       
4. Program - use a function repetedly Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       *** use a function repetedly

       ###################################################
       #  To show the stdout with with a better format
       ###################################################

        info()
      {
	    message=$1
	    echo "`date ` - [INFO] - $msg"
      }

        info "This is my first script."
        info "This script shows echo outputs in a timeline manner."
        info "This is done by repeatative use of function."

        OUTPUT:
        Sat May 21 12:46:56 EDT 2016 - INFO - This is my first script.
        Sat May 21 12:46:56 EDT 2016 - INFO - This script shows echo outputs in a timeline manner.
        Sat May 21 12:46:57 EDT 2016 - INFO - This is done by repeatative use of function.
       
5. Program - returning value from a function Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       search a pattern in a log file and return results accordingly.

       #1. echo a string


           search_pattern()
       {
	       log_file=$1
	       if [[ ! -e $log_file ]] # checks if the file exists or not
	       then
		           echo "FILE_MISSING"
	       else
		           if [[ `less $log_file | grep "DATA SAVED"` ]]
		           then
			       echo "PASS"
		           else
			       echo "FAIL"
		           fi
	       fi
       }

       log_dir="/var/tmp/work/test1.0"
       for log in $( ls ${log_dir}/*log )
       do
	        result=$(search_pattern $log)
	        ## recieves the return value from called function

	        if [[ $result == "PASS" ]]
	        then
		          echo "$log - PASSED"
	        elif [[ $result == "FAIL" ]]
	        then
		          echo "$log - FAILED"
	        elif [[ $result == "FILE_MISSING" ]]
	        then
		          echo "$log - File Missing."
	        else
		          echo "Error occured."
	        fi
      done


       
6. Program - returning value from a function ( Return an exit status) Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       #search a pattern in a log file and return results accordingly ( Return an exit status).

         search_pattern()
       {
	     log_file=$1
	     if [[ ! -e $log_file ]] # checks if the file exists or not
	     then
		      result="FILE_MISSING"
	     else
		      if [[ `less $log_file | grep "DATA SAVED"` ]]
		      then
			       result="PASS"
		      else
			       result="FAIL"
		      fi
	     fi
       }

        result="" ## declare a global variable, accessible inside function
        log_dir="/var/tmp/work/test1.0"
        for log in $( ls ${log_dir}/*log )
        do
	         result=$(search_pattern $log)
	         ## recieves the return value from called function

	         if [[ $result == "PASS" ]]
	         then
		          echo "$log - PASSED"
	         elif [[ $result == "FAIL" ]]
	         then
		          echo "$log - FAILED"
	         elif [[ $result == "FILE_MISSING" ]]
	         then
		          echo "$log - File Missing."
	         else
		          echo "Error occured."
	         fi
      done
      
Untitled Document