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:

while [[ r -ne 0 ]]
r=`vx $* < \`tty\` > \`tty\``
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?



So would be the following wrong?



while  [ $returnvalue -ne 0 ]
    bash ./vx $1
  • 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

I beleive you actually want:

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

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.

| improve this answer | |

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

So just add a line before "done"

| improve this answer | |

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.

| 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. – Stéphane Chazelas Mar 2 '13 at 19:49
  • How could the two tty invocation return a different result? Their stdin is the same. – Stéphane 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. – Stéphane Chazelas Mar 2 '13 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.