Sunday, October 13, 2013

Automating a process in Unix


Here we are going to talk on Bash programming whch is quite powerful when It comes to automating a   process in unix environment. Here we go.
 
1. Declaring Variable
         variable_x= test_val
         variable_y=$variable_x

2. Declaring command output in a variable
         command=  some unix command here

         variable_x=`unix_command`

3. Conditional Statement
         if

             then
                 // do something 
         fi

        #If Else If syntax goes here
         if
           then
               // do something
           else
            // do something else
         fi

 4. Replacing some content or value of a variable  into a file

         $variable_x > some_file.txt

5. Storing the content of a file in a variable
        variable_form_content=$(<disk_file.txt)

 6.  Conditional check  for 0
        //check if variable_x is zero
       if [ -z "$variable_x is" ]
             then
                //do something
       fi

7. Conditional check for no command output/null
        variable_command_output=`command`
         if [ -n "$variable_command_output" ]
            then
                 //do  something  
          fi

8. Check for equality
         if [ $variable_x=="test_value" ]
 

9. Creating file at run time
         new_file= hello_world_$(date +"%m_%d_%Y_%s").txt


10. Displaying output on console while piping to some other file  
         command | tee $piped_out_put_file  

11. Assigning scanned file output to a variable
          output_variable=`cat some_file.txt |grep "scan_some_text"`

12. Running command in a new process
          ( command   &)

13. Do while loop
         while (( ++some_variable_count <= limit )) ;do
          // do something
        done


14. Wait for some time

         sleep  // in seconds
 
15. Passing argument to a funtction.
    function function_name {
      command...
  } 
 function_name "$arg1" "$arg2"