Friday, 2 September 2011

Invoke an awk command from a function within shell script

If we pass awk '{print $3}' to another function in shell script. $3 will be considered as third argument for that function.To make it as literal for shell and a field for awk , then we should write it as: awk '{print " ' $3' "}'.

Example :

    echo  "BEGIN Command : $@ "
    #Executing command and redirecting stderr to devnull
    output=$(eval $@)
    #Exit status of the command
    echo $output

    if [[ $stat -ne 0 ]] ; then
        #collecting all return codes of commands into a variable
        _Report "$@ not successful"
        _Report "END"
        _Report "$output"
        _Report "END"

#List of commands to be run for unix cpu usage problem
    _Command "ps -ef | grep \"<defunct>\" | grep -v grep | awk  '{print "'$3'"}'"

1 comment:

Tweets by @sriramperumalla