I need some help writing a basic Unix shell script that will run on AIX:

Loop that will run a netstat command (netstat -an| grep 16752| grep ESTABLISHED| wc -l) that counts sessions every second and also gets the timestamp and redirects that data to a log for review. I would like to be able to input in minutes how long it should run for if possible. I would like the output to be pretty basic and look like similar to this:

timestamp  #count
timestamp  #count

migrated from stackoverflow.com Aug 5 '11 at 15:54

This question came from our site for professional and enthusiast programmers.

  • 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 (blog.evermeet.cx/archives/63-seq-on-AIX.html), and "basic Unix shell script" is near-guaranteed to mean we can't use seq to loop easily, nor $(( )) and friends. Matter of fact, I'm not even sure the until 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. – Vlueboy Aug 8 '11 at 17:56

Here is a hint.

date +%s

returns the number of seconds since the epoch. Since there are 60 seconds in a minute so you can easily do the math. Both ksh and bash allow you to do arithmetic with double parentheses around an expression like this

echo $(( 324567 / 60 ))

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.

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
  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

    TIMESTAMP=$( eval ${TIMESTAMPER} )  
    echo "${TIMESTAMP} #${OUTPUT}"
    sleep 1;
  done >> ${LOG_FILE}

  echo "Finished on $( eval ${TIMESTAMPER} ) "
  • eval is evil. Why don't you use functions? – Michał Šrajer Aug 8 '11 at 22:22
  • basic shell scripts don't need to be pretty; More importantly, function syntax didn't work in ksh and I don't even want to know how such a short script, could be made MORE portable by adding more features. We don't even know whether his shell is really ksh. – Vlueboy Aug 8 '11 at 22:46
  • ksh is sh compatible. syntax: funcname() { instrunction; } is portable. – Michał Šrajer Aug 9 '11 at 8:44
  • Tried it before posting my first answer, but there is a syntax error that I couldn't pin down; Ubuntu's ksh kept disliking the parenthesis and/or the need for that semicolon after "instruction". The bash keyword "function" is also not compatible with kshell, and if adding it / removing it will cause problems that my intentional inlining avoided. Assuming the shebang line is for bash solves problems, but the asker is the final source of his (anonymous) environment, not the (helpful) commenters. Seeing that nobody else is offered answers, my code offer isn't changing unless the asker follows up. – Vlueboy Aug 9 '11 at 21:03
  • I'm not criticizing. Just sharing thoughts. WRT function definition. The portable way is: "fnname() { instrunction; }". ksh and bash also accept "function funcname { echo foo; }". Bash also accept "function funcname() { echo foo; }" which ksh does not. – Michał Šrajer Aug 10 '11 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy