[Solved] Output in bash script not captured in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Output in bash script not captured in variable
# 1  
Old 02-22-2011
[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:

Code:
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:

1.
Code:
while [[  "$bjobs_out" != "No unfinished job found" ]]; do

-> result: as described above
2.
Code:
while [[  "$bjobs_out" != "No unfinished job found" ]]; do

-> result: as described above
3.
Code:
while "$bjobs_out" != "No unfinished job found"; do

-> result: "command not found"
4.
Code:
while $bjobs_out != "No unfinished job found"; do

-> result: "command not found"
5.
Code:
while [[  $bjobs_out != "No unfinished job found" ]]; do

-> result: as described above
6.
Code:
while (echo $(echo "${bjobs_out}")!="No unfinished job found")

-> result: as described above
7.
Code:
while (echo $(echo "${bjobs_out}")!="")

-> result: as described above
8.
Code:
while echo $(echo "${bjobs_out}")

-> result: as described above
9.
Code:
while echo($(echo "${bjobs_out}"))

-> result: as described above
10.
Code:
while ($(echo "${bjobs_out}"))

-> result: as described above
11.
Code:
while !($(echo "${bjobs_out}" | grep -q "No unfinished job found"))

-> result: as described above
# 2  
Old 02-22-2011
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.

Moderator's Comments:
Mod Comment Thread transferred


bakunin
# 3  
Old 02-22-2011
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!
# 4  
Old 02-22-2011
I suspect the program is outputting to stderr (or worse directly to /dev/tty).

Try this:
Code:
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:

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


Last edited by Chubler_XL; 02-22-2011 at 08:14 PM..
# 5  
Old 02-23-2011
Thank you very much!
I will try as soon as possible!
At the moment i avoided the problem in this way:

Code:
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!

Moderator's Comments:
Mod Comment edit by bakunin: Thank you for writing a follow-up with the solution. I edited the threads title to something a bit more meaningful.

Last edited by bakunin; 02-23-2011 at 08:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

3. Shell Programming and Scripting

cannot pass a echo output to a variable in bash

Hi, I have a problem with passing a echo output into a variable in bash file='1990.tar' NAME='echo $file | cut -d '.' -f1'; echo $NAME the result is echo $file | cut -d . -f1 however with this one,#!/bin/bash file='1990.tar' echo $file | cut -d '.' -f1 the result is what I... (2 Replies)
Discussion started by: 1988PF
2 Replies

4. Shell Programming and Scripting

[SOLVED] Capturing output in a korn variable

Hi, I'm new to korn and having trouble capturing the text output from one program in an array that I can then feed into another program. Direct approaches didn't work, so I've tried to break it down thus: The program lonlat2pixline gives the values I need in the second column, so I print that... (4 Replies)
Discussion started by: daurin
4 Replies

5. UNIX for Dummies Questions & Answers

Solved: how to save an output to a variable

i want to save the output of /scripts/whoowns domain.com to a username like $user = /scripts/whoowns domain.com but I'm not sure how to do that This is inside a bash script how can I get the output of /scripts/whoowns then save that to a variable? thanks! ---------- Post updated at... (0 Replies)
Discussion started by: vanessafan99
0 Replies

6. Shell Programming and Scripting

[Solved] Using SSH in bash script

Hello, I am writing a script that has to log in to X number of servers over a full C class range. I am only keyed on a certain number of these servers and this can change from time to time. The part of my script that deals with this is for i in $(cat $server_list); do ssh ... (0 Replies)
Discussion started by: colinireland
0 Replies

7. Shell Programming and Scripting

In bash script, how to assign output of a command to a variable while keeping tabs?

Hi, wondering if it's been asked before but didn't find matches from google. Basically I have this line: myvar=$(echo -e "a\tb") Now somehow the '\t' from the echo output gets replaced with white space and then stored in $myvar. It creates a problem for me later to use tab as delimiter to do... (2 Replies)
Discussion started by: birddie
2 Replies

8. Shell Programming and Scripting

[SOLVED] how to use my aliases in a bash script

Hi, I confess to be stuck ... as explained here Aliases - Bash Reference Manual I added shopt -s expand_aliasesto my script ... but it didn't work. Later I've read I had to add this at the begning of my my script : . ~/.bash_aliases(of course my aliases are in this file) but again, the... (0 Replies)
Discussion started by: xib.be
0 Replies

9. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

10. UNIX for Dummies Questions & Answers

Informix function return value needs to be captured in shell script

I need to run a 4ge from within my shell script and capture the string value that is being returned into a unix variable. I have tried the back ticks to no avail. I know it is returning because I did get the returned value to go to my screen at one point.. What I have is: sendlist =... (3 Replies)
Discussion started by: jeniferz
3 Replies
Login or Register to Ask a Question