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 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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Thank you very much, I used eval because my commands are long and quite complicated... –  MortezaLSC May 25 at 13:01
    
So my while loop with the condition kill -0 $pid1 is correct, isn't it? –  MortezaLSC May 25 at 13:02
1  
@MortezaLSC - seems reasonable to me. –  slm May 25 at 13:24

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.