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.
That is:
- Execute
./read_pipe.sh
- It waits until a command is echoed to
pipe
and reads it. - It executes the command once. <- What doesn't work. It keeps executing it forever.
- Repeat from step 2.
My read_pipe.sh
#!/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
If I 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?
pipe
, execute it. – Pedro Adame Vergara Jun 06 '17 at 08:05rm -f pipe && mkfifo pipe
. Now it works correctly. For some reason it was a simple text file. -.-' – Pedro Adame Vergara Jun 06 '17 at 08:58sh
line? – rottweiler Feb 27 '20 at 08:14