0

Getting the error above when running my script. Trying to check if the value of output is not = to "average:". If not, then print to list file, if it is then start from beginning of loop.

#!/bin/bash
Oad=$(date +%Y_%m_%d.%H.%M.%S)
for i in {1..120}
do
OUTP= uptime | awk '{print 10ドル}' | cut -f1 -d, 
echo $OUTP
if $OUTP != average:
 then $OUTP >> $Oad.lst
else continue;
fi
sleep 60
done
cat $Oad.lst | awk '{if(min==""){min=max=1ドル}; if(1ドル>max) {max=1ドル}; if(1ドル<min) {min=1ドル}; total+=1ドル; count+=1} END {print total/count, max, min}' > $Oad.out
William Pursell
214k48 gold badges277 silver badges315 bronze badges
asked Sep 9, 2013 at 14:12

2 Answers 2

4

That's not the way to compare it.

if $OUTP != average:

Should be

if [[ $OUTP != average: ]]

Some line seemed odd as well

OUTP= uptime | awk '{print 10ドル}' | cut -f1 -d,

I think should be

OUTP=$(uptime | awk '{print 10ドル}' | cut -f1 -d,)

And

then $OUTP >> $Oad.lst

if you want to send the value of the variable to the file, it should be

then echo "$OUTP" >> "$Oad.lst"

Overall I would suggest a format like this:

#!/bin/bash
Oad=$(date +%Y_%m_%d.%H.%M.%S)
for i in {1..120}; do
 OUTP=$(uptime | awk '{print 10ドル}' | cut -f1 -d,)
 echo "$OUTP"
 if [[ $OUTP != average: ]]; then
 echo "$OUTP" >> "$Oad.lst"
 sleep 60
 fi
done
awk '{if(min==""){min=max=1ドル}; if(1ドル>max) {max=1ドル}; if(1ドル<min) {min=1ドル}; total+=1ドル; count+=1} END {print total/count, max, min}' "$Oad.lst" > "$Oad.out"

One more note. For:

 if [[ $OUTP != average: ]]; then

You probably mean it as:

 if [[ $OUTP != *average:* ]]; then

To match any line not containing average: instead of any line that's not exactly like it.

answered Sep 9, 2013 at 14:13
1
  • Thanks. Works just like I wanted it to. Commented Sep 9, 2013 at 15:33
2

This is just a side comment to augment @konsolebox' answer, but it might be better still to do all of the logic in a single Awk script.

#!/bin/bash
Oad=$(date +%Y_%m_%d.%H.%M.%S)
for i in {1..120}
do
 uptime
 sleep 60
done |
awk '10ドル != "average" { t=10ドル; sub(/,.*/,"",t);
 if(min=="") min=max=t;
 if(t>max) max=t;
 if(t<min) min=t;
 total += t; count++ }
 END {print total/count, max, min}' > $Oad.out

This requires some refactoring if you also want the intermediate results in $Oad.lst, but it's not an intrusive change. (Just print in the main loop, and open a second file handle in the END loop.)

answered Sep 9, 2013 at 15:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.