[Solved] Output in bash script not captured in variable

I'm tring to write down a simple script that would execute a command and wait until it returns a specific result.
This is what i did:

bjobs_out=`bjobs`
while [[  "$bjobs_out" != "No unfinished job found" ]]; do
bjobs_out=`bjobs`

sleep 6
done

It seems to work until the command 'jobs' return the list of jobs in execution, but when there aren't jobs it prints on the screen "No unfinished job found" infinitely!

Is there someone that can help me?

These are the tests i have already did:

while [[  "$bjobs_out" != "No unfinished job found" ]]; do

-> result: as described above
2.

while [[  "$bjobs_out" != "No unfinished job found" ]]; do

-> result: as described above
3.

while "$bjobs_out" != "No unfinished job found"; do

-> result: "command not found"
4.

while $bjobs_out != "No unfinished job found"; do

-> result: "command not found"
5.

while [[  $bjobs_out != "No unfinished job found" ]]; do

-> result: as described above
6.

while (echo $(echo "${bjobs_out}")!="No unfinished job found")

-> result: as described above
7.

while (echo $(echo "${bjobs_out}")!="")

-> result: as described above
8.

while echo $(echo "${bjobs_out}") 

-> result: as described above
9.

while echo($(echo "${bjobs_out}"))

-> result: as described above
10.

while ($(echo "${bjobs_out}")) 

-> result: as described above
11.

while !($(echo "${bjobs_out}" | grep -q "No unfinished job found"))

-> result: as described above

Even if this problem occurs on an AIX system it is a shell scripting problem. I'm going to transfer this thread to the appropriate subforum therefore.

bakunin

I inserted it under AIX session because I'm quite sure that the written code is correct and I hope someone could help me to understand why it doesn't work under AIX!

However:

thank you very much!

Please help me!

I suspect the program is outputting to stderr (or worse directly to /dev/tty).

Try this:

bjobs_out=`bjobs 2>&1`
while [[  "$bjobs_out" != "No unfinished job found" ]]; do
bjobs_out=`bjobs 2>&1`
 
sleep 6
done

If you still have no luck, perhaps the return status of the bjobs command will help try checking the value of $? after it's run:

while bjobs > /dev/null 2>&1 ; do sleep 6; done

Thank you very much!
I will try as soon as possible!
At the moment i avoided the problem in this way:

bjobs_out=`bjobs`
if [[ $bjobs_out != "No unfinished job found" ]]; then
        var=0
else
        var=1
fi
while [ $var -eq 0 ]; do
        sleep 6
        bjobs_out=`bjobs`
        if echo $bjobs_out ; then
                var=0
        else
                var=1

        fi
done

---------- Post updated at 12:43 PM ---------- Previous update was at 12:43 PM ----------

The first way indicated by Chubler_XL is right! It works!
Thank you very much!