i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons).
So far i managed to do the following, which is working, but am still considering it as "dirty".
Your TIME_INTERVAL variable is not expanded by the shell, so your results may not be the desired ones.
And, I'm not sure I understand why you overwrite the output file again and again in the loop, so only the last one will persist and be available / workable upon.
Instead of running 27 external commands for every single interface, you might want to consider doing it ALL in one awk script like
FNM=${0##*/}
FNM=${FNM%.sh}
$SNMP -v2c -c $COMMUNITY $HOST ifDescr |
awk -vX=2 -vDT="$DATE" -vTI="$TIME_INTERVAL" -vOF="$OUTPUTDIR/$FNM" -vHF="$HISTDIR/${DATESTAMP}.$FNM" '
FNR == NR {INT[$4]++
next
}
FNR == 1 {FC++
}
$X in INT {
sub (/incoming=/, ""; $4)
sub (/outgoing=/, ""; $5)
if (($4 < 0) || ($5 < 0)) {print "Negative numbers not allowed...exiting"
exit 1
}
inOct [$X,FC] = $4
outOct[$X,FC] = $5
sub (/speed=/, ""; $3)
if (!speed[$X]) speed[$X] = $3
sub (/state=/, ""; $1)
sub /\([0-9]\)/, "", $1)
if (!state[$X]) state[$X] = $1
}
END {for (i in INT) {inUsage = (inOct [i,2] - inOct [i,1]) / TI * 8
outUsage = (outOct[i,2] - outOct[i,1]) / TI * 8
TMP = sprintf ("%s|%s|%.f|%s|%.0f|%.3f|%.0f|%.3f\n", DT, i, speed, state, inUsage, inUsage/speed*100, outUsage, outUsage/speed*100)
print TMP > OF
print TMP >> HF
close (OF)
close (HF)
}
}
' - $HOMEDIR/cron/tmp/${FNM}1 $HOMEDIR/cron/tmp/${FNM}2
Define X to be the column the interface name is in (here assumed to be 2). Unfortunately, this is just a vague proposal as there are no data nor data structures known to allow for a decent testing.
@Rudi C. I need to try to that to get the script much "cleaner" as am still not yet happy with the code.
In the meantime i have also tried with the arbitrary echos and all worked fine (both Corona's and MadeInGermany's)
Thanks all for the contribution guys.