Unix basic shell script help needed please?

0

1

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

roacha

Posted 2011-08-04T00:46:39.563

Reputation:

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 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 – 2011-08-08T17:56:45.187

Answers

1

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

Michael Dillon

Posted 2011-08-04T00:46:39.563

Reputation: 899

1

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

Vlueboy

Posted 2011-08-04T00:46:39.563

Reputation: 673

eval is evil. Why don't you use functions? – Michał Šrajer – 2011-08-08T22:22:03.740

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 – 2011-08-08T22:46:44.287

ksh is sh compatible. syntax: funcname() { instrunction; } is portable. – Michał Šrajer – 2011-08-09T08:44:16.727

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 – 2011-08-09T21:03:20.920

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 – 2011-08-10T06:41:26.943