Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 '11 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 '11 at 13:42
add comment

2 Answers

up vote 2 down vote accepted

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
share|improve this answer
    
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 '11 at 11:11
    
@jmbouffard: fixed. tail -f will continue to read the file even after it reaches EOF. –  Kevin M Jul 7 '11 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 '11 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 '11 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. –  Gilles Jul 8 '11 at 17:22
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.