Reset $1 variable

Hi all,

I'm using a scipt with one input and one output.
I'm referring to the input by $1
after executing a command line, I'm getting the output via $1.
Normally, the $1 shouldn't get the same values between the first call and the second.
Is there a solution to force my second call for $1 to get the value of $PS -efo pcpu,vsz,arg

Here is my script:

SWAPINFO="/usr/sbin/swapinfo"
PS="/usr/bin/ps"
AWK="/usr/bin/awk"
GREP="/usr/bin/grep"
SORT="/usr/bin/sort"
TAIL="/usr/bin/tail"


MYPAT="$1"
Availability=0
export UNIX95=1
VAR=$($SWAPINFO -ta | $GREP memory | $AWK ' { print $2 } ')
MYCPU=` $PS -efo pcpu,vsz,args | $GREP -v COMMAND | $GREP -v awk | $GREP "$MYPAT" | $SORT -k 1 | $TAIL -1 | $AWK -v var=$VAR -v pattern="$MYPAT" ' { print $1 } '`
echo $MYCPU

Sorry, I don't get your request.

Why not? What should modify it?

In your script, there's no second call for $1.

Please rephrase your request.

supose my script is called myscript.sh
in the UNIX terminal I will type
$ myscript.sh ProcessName
where ProcessName is the name of the process I want to monitor.

MYPAT=$1 will affect ProcessName to MYPAT.
In the end of the line

MYCPU=` $PS -efo pcpu,vsz,args | $GREP -v COMMAND | $GREP -v awk | $GREP "$MYPAT" | $SORT -k 1 | $TAIL -1 | $AWK -v var=$VAR -v pattern="$MYPAT" ' { print $1 } '`

Print $1 should give the value of "pcpu" in relation to

MYCPU=` $PS -efo pcpu,vsz,args

That print $1 is a field within awk and has nothing to do with the script's $1 (shell positional parameter).

1 Like

In awk, $ means column. $1 means "print column 1".