1

I wrote this bash:

#!/bin/bash

    eval "(a command) &"
    pid1=$!

    eval "(while kill -0 $pid1; do .... ; done) &"  #It creates file.txt after few seconds
    pid2=$!

    if [ -s /tmp/file.txt ]; then
             for line in $(cat /tmp/file.txt)
                 do
                  sth
                 done

With logic of bash scripting: Is the values of pid1 and pid2 correct?

Is this script running correctly?

MLSC
  • 972
  • 7
  • 21
  • 28

1 Answers1

1

I do not think you need the `eval "(...) &" syntax. Simply do this instead:

cmd1 &
pid1=$!

cmd2 &
pid2=$!

But otherwise your approach looks OK to me.

Example

$ more ex.bash 
#!/bin/bash

sleep 10 &
pid1=$!

sleep 10 &
pid2=$!

echo "ID1: $pid1 --- ID2: $pid2"

Now when I run it:

$ ./ex.bash 
ID1: 27866 --- ID2: 27867

We can confirm this is correct:

$ pgrep -l sleep
27866 sleep
27867 sleep
slm
  • 363,520
  • 117
  • 767
  • 871