storing output of a mathematical expression

36 Views Asked by At

i am new to SunOS unix system. i want to store the uptime and convert it into minutes in a shell script. below is what i used inside script.

hrs=`uptime | awk '{print \$5}' | sed 's/[:,]/ /g' | awk '{print \$1}'`
mins=`uptime | awk '{print \$5}' | sed 's/[:,]/ /g' | awk '{print \$2}'`
uptimesecs=$(($mins*60)))

and error what i got in script.

enter image description here

can anyone help me with the syntax

1

There are 1 best solutions below

0
On

You are overquoting the $s in the awk scripts:

hrs=`uptime | awk '{print $5}' | sed 's/[:,]/ /g' | awk '{print $1}'`
mins=`uptime | awk '{print $5}' | sed 's/[:,]/ /g' | awk '{print $2}'`

You're also doing more work than is necessary. Get the uptime once:

uptime=$(uptime | awk '{print $5}')

Then split the value on a : using the shell itself.

IFS=: read hrs min <<< "$uptime"