3

I have a system that handles incoming emails to send them to a blackbox application at my job. The high level script is maintained by inittab to always run and runs a child script to do the actual work with this command:

$SCRIPT | nc -l -p $PORT

The script itself reads from a named pipe, does a bit of parsing and processing of the data before calling echo to shuffle the data back through netcat to the process connected on $PORT.

What I need is some method to handle incoming data from the far end of my pipe. When I make a request within the application to close the connection it sends back a string (I can define it to whatever I want) and waits for my script to close the pipe. I currently am struggling to understand how I can add in the functionality to read incoming data from the other end; verify it is the command to close the pipe, and then exit the script.

My script (in a nutshell) looks like this:

while true ; do
  email_input="`cat "$pipe"`"
  if [[ $email_input =~ .*escape_queue.* ]] ; then
    break;
  fi
  echo "`parse`"
done

I'm open to the possibility of having to alter the program flow, I just can't wrap my head around how I would be able to read the data incoming asynchronously since the script blocks on cat $pipe until a new email is received to process.

If its not clear, I'm at a novice level with bash scripting and am always open to suggestions for improvement.

UPDATE I've changed my script call to

$SCRIPT | nc -l -p $PORT > $nc_data

and within the script itself

netcat_response="`cat "$nc_data"`";
if [[ "$netcat_response" =~ "exit" ]] ; then
  cat /dev/null > $nc_data
  break;
fi

At this point the script terminates once a new message is piped into the fifo. This means that I will always lose 1 message as it gets read by the script and then the script terminates. The script still blocks on the cat until something is read. Worst case scenario this will have to do.

1
  • 1
    It's not necessary to use the outer quotes in your assignment to email_input and $() is recommended instead of backticks. Bash can do cat's job: email_input=$(<"$pipe"). Also, to truncate a file: >"$nc_data" Commented May 19, 2012 at 11:01

1 Answer 1

3

You can have nc quit after a certain time from the EOF of stdin.

$SCRIPT | nc -l -q 5 -p $PORT > $nc_data

-q being the option to quit after a certain amount of seconds.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply, that seems it would resolve my issues but unfortunately this is the result: nc: illegal option -- q
If it is FreeBSD, try the -w option
HP-UX. Corporate server so I have very little control over software packages.
@TheCapn I guess you are going to have to manually script for the EOF of stdin, this thread may help you further - link
I managed to get it to work with my addition to $nc_data. It has one standing issue that I am able to work around as its a fault with the blackbox application I'm working with. I appreciate the help!
|

Your Answer

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.