I need to write a bash program that runs commands echoed to a named pipe it reads, but I cannot get it work only when a command is sent. It keeps repeating the last command until a new one is written.
- It waits until a command is echoed to
pipeand reads it.
- It executes the command once. <- What doesn't work. It keeps executing it forever.
- Repeat from step 2.
#!/bin/bash pipe="mypipe" if [ ! -p $pipe ]; then echo 'Creating pipe' mkfifo $pipe fi while true do if read line <$pipe; then COMMAND=$(cat $pipe) echo "Running $COMMAND ..." # sh -c $COMMAND fi done
cat "echo 'Hello World'" > mypipe the output is this forever:
Running "echo 'Hello World'" ... Running "echo 'Hello World'" ... Running "echo 'Hello World'" ... Running "echo 'Hello World'" ... ...
How can I run the command once and wait for another echoed command?