8

I'm writing a bash script that constantly read a folder in a loop to send data to a server at a defined time interval. I'm using netcat as the tool to connect to the server and send the data. My pseudo code would look like:

while true
do
    read_folder()
    process_data() > result.txt
    cat result.txt > netcat ip port
    wait 10 sec
done

My only problem is that in the scenario the client connects and disconnects the TCP/IP connection to the server every time. I would prefer to establish the connection at the start of the script and close it at the end.

Is there any way this could be done with command line tools in a bash script?

2
  • Actually, I prefer your current setup. In case of long connection, there is bigger chance of connection loss. This way, in case of connection loss, there is automatic retry after 10 seconds
    – bbaja42
    Jun 30, 2011 at 15:02
  • Take a look at socat, it's a netcat replacement that has a built-in functionality to respawn pipes after they've finished.
    – Marcin
    Jul 8, 2011 at 13:42

2 Answers 2

5

Just put the redirection outside the loop.

while true; do
    read_folder
    process_data
    sleep 10
done | netcat $ip $port

If you need to output to the loop's standard output from inside the loop, divert it through another file descriptor.

{
  while true; do
      read_folder
      echo tick >&3
      process_data
      sleep 10
  done | netcat $ip $port
} 3>&1
4

Two separate processes: One that copies result.txt to netcat. Result.txt is fed via another process.

echo -n >result.txt
tail -f result.txt | nc ip port &
while true
do
    read_folder()
    process_data() > result.txt
    wait 10 sec
done
5
  • I thought about something like that but the first process will return when it reaches the end of the file, it will not wait for the other process to write to it.
    – jmbouffard
    Jul 7, 2011 at 11:11
  • @jmbouffard: fixed. tail -f will continue to read the file even after it reaches EOF.
    – Kevin M
    Jul 7, 2011 at 19:28
  • Thanks it works amazingly well with tail -f. I'm even able to use a fifo pipe instead of a "growing" result.txt file for the transfer. Answer accepted.
    – jmbouffard
    Jul 8, 2011 at 15:34
  • @Kevim: The edit @Gilles made using : >result.txt was fine, it does not leave a newline in the file. Test it yourself.
    – Caleb
    Jul 8, 2011 at 16:10
  • echo -n is fine too, as long as your shell supports it (some shells print -n) — so : is preferable (it works everywhere). And you do need tail -c +1; without it, if tail is slow to start, it will omit all but 10 lines of what's already in the file. Jul 8, 2011 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .