Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I am trying to create a script to be used on RHEL server. If I replace the $t with a number, the script works. If I try add a ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    2

    Help with script with variable in if statement

    Hello,

    I am trying to create a script to be used on RHEL server. If I replace the $t with a number, the script works. If I try add a variable or the $1, the script doesn’t work (returns all processes even if less than the $t value). I thought that it may be treating it as a literal, but I wasn’t sure of the conversion.

    t=$1
    echo $t
    lcnt=`ps -eo pid,ppid,rss,vsize,pcpu,pmem,cmd -ww --sort=pid | awk '{if ($6 > $t) print $1 " "$5 " "$6 " " $7}' | wc -l`
    #echo $lcnt
    if test $lcnt -eq 0 ; then echo "0 0.0 0.0 null" ; else ps -eo pid,ppid,rss,vsize,pcpu,pmem,cmd -ww --sort=pid | awk '{if ($6 > $t) print $1 " "$5 " "$6 " " $7}' ; fi

    Thanks in advance for any assistance.
    Brian

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    try exposing the "$t" in your awk script--

    awk '{if ($6 > '$t')...

    do it both places so the shell can make appropriate substitution
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    2
    Thank you. Using the '$t' is working and returning the proper output.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...