until loop fails.

i have a script like below, i am trying to print "...." until a background process that was executed before is running,, the below does work and teh loop keeps on printing "..", can someone help where i am wrong.

/tmp/test/abc.sh &
until [ -z $! ]
do
echo "..\c"
done

abc.sh is the scipt that i am running in bg
$! capturing the process id of abc.sh[runnig in bg]
trying to print ".." until the process exist..

Please help.

$! captures the PID of the last process run in the background, that's correct. But that won't reset the value. Consider this: If you'd happen to start more than one background process, which one should reset $! ? What if you'd want to wait for the other process?

It's better to check the process list for the PID of the background job, and act based on that:

abc.sh &
PID=$!
while ps $PID > /dev/null
do
    echo '..\c'
    sleep 1    # So we don't hog the CPU
done

Thanks pluid for ur response,,

the one you ried doesn't work for me,, the "ps" itself stucks here. when i add it to my script that throws error taht the usage of ps is wrong..

and moreover,, i am simple trying to print ".." until the bg process runs and then continue with the rest part of the script, simple like a status display on the screen,,

running abc.sh script in the bg and capturing the $! is all for the same,,

My reply was based on what you've told us. Since you didn't tell us you OS, I can only guess, and used GNU ps.

So you want something like "Launching abc.sh..."? That's not what your original code would have done (by intention, had it even compiled).

Putting a process into the background using '&' is a very very quick operation, and the PID will exist as soon as the shell finished processing the line. Meaning, no matter how big or long running abc.sh might be, the line 'abc.sh &' will return as soon as fork/exec is done (a tiny fraction of a second), and by that point there's a PID assigned to $!

Ambiguous.
You wrote:

I assume you mean:

Or do you actually mean:

Contrary to previous posts, try "jobs" not "ps" to check whether your background job is running.
Assuming you want to wait for the background process to complete.

/tmp/test/abc.sh &
while true
do
       if [ -z "`jobs`" ]
       then
              # Background job has finished
              break
       else
              # Background job is still running
              echo "..\c"
              sleep 5
       fi
done
/tmp/test/abc.sh &
while ps $! > /dev/null
do
  printf "."
  sleep 1 ## or .5; adjust to taste
done

I took a different approach. I made two scripts. One to do the work. The other to make dots. I figure the worker script may want to do more than one thing while the dots are moving. And I minimize the load on the system...

Dots.sh script:

while [ 1 ]
do
 echo -n  "."
 sleep 1
done

The worker script:

# start the dots going
./poker.sh &
DOTS=$!

# do the work work behind the dots
/tmp/test.sh &
wait $!

# kill off the dots
kill  $DOTS
echo "done"

I had a similar idea as jp2542a, but instead using a single script and also without the sleep granularity...

/tmp/test/abc.sh &
procpid=$!

# Start wait dots in the background
(while : ; do printf "."; sleep 1 ; done)&
printpid=$!

# wait for the program to finish
wait $procpid

# kill the wait dots
kill $printpid
echo

Thanka all,, that really helped me to learn lot, i have acheived the output using "jobs" and that is what i was looking for,,