I tested this under ksh, which I believe is the shell used in AIX. Unlike my comment to the question, it seems "until" works OK. This script assumes bc is working; I'm not sure if the $( )
syntax works in your likely old shell, but it's a gamble and without specific / shell version information, it will have to do.
Usage: thisscript.sh *minutes*
Calls the script with a number of minutes. It will run the commands every second until it has counted down from the calculated loop_total to zero.
There's no visible output other than a couple "start / end" and duration messages. Everything is piped to the LOG_FILE. If you want output to the screen, you'll likely have to use variables to store the data and THEN output that to the LOG_FILE. Cheers.
#!/bin/sh
LOG_FILE="connections.txt"
PORT="16752"
COMMAND="netstat -an | grep ${PORT} | grep ESTABLISHED | wc -l"
TIMESTAMPER="date +'%D %H:%M:%S'" # Format like "mm/dd/yy hh:mm:ss"
if test -z "$1" ; then
echo "ERROR: No duration provided"
exit 1
else
INPUT="$1" # Store the number of minutes wanted
LOOP_TOTAL=$(( ${INPUT} * 60 ))
echo "Started on $( eval ${TIMESTAMPER} )"
echo "Executing for: ${LOOP_TOTAL} seconds (${INPUT} minutes)"
COUNTDOWN=${LOOP_TOTAL} # Initialize counter
until test ${COUNTDOWN} -eq 0 ; do
COUNTDOWN=$(echo "${COUNTDOWN} - 1" | bc)
OUTPUT=$( eval ${COMMAND} )
if test -z "${OUTPUT}"; then
OUTPUT="0" # greps are blank when no connection, so fill out as 0
fi
TIMESTAMP=$( eval ${TIMESTAMPER} )
echo "${TIMESTAMP} #${OUTPUT}"
sleep 1;
done >> ${LOG_FILE}
echo "Finished on $( eval ${TIMESTAMPER} ) "
fi
1
There's little chance we can provide further help seeing how migrated questions rarely get the user to cross-register. The problem here is that AIX doesn't like bash (http://blog.evermeet.cx/archives/63-seq-on-AIX.html), and "basic Unix shell script" is near-guaranteed to mean we can't use
– Vlueboy – 2011-08-08T17:56:45.187seq
to loop easily, nor$(( ))
and friends. Matter of fact, I'm not even sure theuntil
command works. I'll try to work on something and post, but the asker would have to test it and do the work of porting it to AIX after all.