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 have a shell script, where i start a new .sh script. Now I want to save the return value of the sub script in a variable.

I found this script:

#!/bin/bash
r=1
while [[ r -ne 0 ]]
do
r=`vx $* < \`tty\` > \`tty\``
done
exit 0

It calls the Script vx with a param and saves the return value in the variable r. What does the tty ... mean? Is that the return value? Can't I use $? for return value?

Thanks!

Edit:

So would be the following wrong?

#!/bin/bash

returnvalue=1

while  [ $returnvalue -ne 0 ]
do
    bash ./vx $1
    returnvalue=$?
done
share|improve this question
    
have you tried ? this r=$(vx $* < `tty` > `tty`; echo $?) –  Rahul Patil Mar 2 '13 at 20:45
    
I don't get it. What is the < tty > tty;? Is r= $? wrong? –  John Smithv1 Mar 3 '13 at 7:26
add comment

3 Answers

I beleive you actually want:

#! /bin/sh -
until rv "$@"; do
  continue
done

You generally don't need to get the actual value of the exit status ($?) nor to know whether or not 0 means true or false. The until, while, if... construct take care of that. The continue above is only for legibility, you can put any command in there, include the no-op one: :.

Never use $* or $@ unquoted, that doesn't make sense.

share|improve this answer
add comment

You will save the output from the script in "r", the exit code would be in $?.

So just add a line before "done"

result=$?
share|improve this answer
add comment

In the expression

r=`vx $* < \`tty\` > \`tty\``

Bash interprets vx as an executable filename to which you pass all ($*) of the arguments passed to the current script after shell expansion. For example, if the arguments to the script were * `ls /dev` then the argument list passed to vx will be the names of all of the files in the current directory plus the names of all of the files in /dev. The number of arguments will be the same as the number for file names.

vx is assumed to accept input on its standard input.

tty is an executable that returns a string that Bash interprets as a filename.

Note that the first invocation of tty could return a different filename than the second invocation' for example, if tty produces the filename based on some external condition such as the content of the file with the name produced by the previous invocation, or based on a timestamp or other external condition that could change.

The expression r=`...` assigns the standard output of the backticked command to r. The return value of the backticked expression is in $?.

Since the standard output of vx is redirected to the filename produced by the second invocation of tty, the value of r will always be empty (r=) which is the same as r=0 in the expression [[ r -ne 0 ]]. The result is that there is no situation where the loop can run more than once.

share|improve this answer
1  
$* is not the list of arguments, it's the list of files matching the patterns resulting from the further splitting of the arguments on the $IFS characters. You need "$@" for the list of arguments. –  Stephane Chazelas Mar 2 '13 at 19:49
    
How could the two tty invocation return a different result? Their stdin is the same. –  Stephane Chazelas Mar 2 '13 at 19:57
    
Very interesting analysis, but does that help OP with the problem? –  vonbrand Mar 2 '13 at 20:02
    
@vonbrand The OP asked three questions. I believe I sepcifically replied to all of them. –  Jonathan Ben-Avraham Mar 2 '13 at 20:06
1  
Nope "$*" would be a single string argument. Try calling the script with those three arguments: the-script 'a b' '*' and see how many are passed to rv, and now compare with "$@". strace -fe execve can help. –  Stephane Chazelas Mar 2 '13 at 20:12
show 1 more comment

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.