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 :

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

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

#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