I have two BASH shells and would like to send data from one to the other. To achieve this, I have thought the following:
1) Shell #1 runs a script in order to create the file "output.out" with the data.
2) Meanwhile Shell #2 is waiting for "output.out" is created for starting with a second script later.
My question is: when "output.out" is created, how can I send an event to Shell #2 so it can start with the second script?
I tried this:
While Shell #1 is running, Shell #2 is executing this script:
while [ ! -e output.out ]; do sleep 0.1; done
# Second script starts here...
But since I'm doing a heavy simulation, I cannot use this solution! What I need is to send an event to Shell #2...
Put your logic into a function, which gets called by trap when you send a SIGUSR. In the main body, run an infinite loop that just idles along. From your first script, send the signal using kill as soon as output.out has been created.
You could create a named pipe and use that to communicate between the two. Create the file mkfifo pipe_name, with one shell, cat some_file > pipe_name. It will appear to hang, with second shell, cat pipe_name and the output will be displayed and the first shell will complete.
Now with the second shell, cat pipe_name, this will appear to hang. With the first shell, cat some_file > pipe_name. The output will be displayed in the second.
Thanks a lot peterro! Definitely this is the best and clear way to do it! But now I have another question... what to do if I don't want to display the file content on the shell?
Many thanks in advance!
----
PS: Thanks pludi for your reply!... sorry for the ignorance (as you can see, I'm a beginner in the unix universe)... you say: "run an infinite loop that just idles along"... if I do an infinite loop, it will force the processor, isn't it?
Thanks in advance!
---------- Post updated at 03:56 PM ---------- Previous update was at 03:52 PM ----------
Thanks pludi for your reply!... sorry for the ignorance (as you can see, I'm a beginner in the unix universe)... you say: "run an infinite loop that just idles along"... if I do an infinite loop, it will force the processor, isn't it?
Yup. It's just like reading/writing to any typical flat file. The named pipe just has some useful properties for purposes such as this. See Chapter 14 in Expert Shell Scripting (Apress)