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 created a two backup scripts - one for files (filebackup.sh) and another for database (dbbackup.sh). filebackup.sh calls dbbackup.sh and stores the output in a variable. The output returned by dbbackup.sh is the tar filename in which the script has stored all the backed up databases.

The script dbbackup.sh contains the following at the end:

echo $TARFILE > /dev/tty

The script filebackup.sh calls dbbackup.sh like this:

DBBACKUP="`/scripts/dbbackup.sh &>/dev/null`"

This dumps all extra output from the script to /dev/null and ensures only the filename is returned.

Everything works except when I call filebackup.sh, the filename is printed to screen. The variable $DBBACKUP remains empty. What am I doing wrong here?

share|improve this question

1 Answer 1

In short: just remove the >/dev/tty part from dbbackup.sh; use just:

echo $TARFILE

This will print $TARFILE to the standard output, i.e., to the screen if you're running dbbackup.sh in a terminal, or to the a file/pipe if redirection is in place (as happens with the shell backquote/$(...) expansion).

There is also another reason why the thing won't work, even if you correct the issue above: your dbbackup.sh script prints the tar file name to standard output, but in the line

DBBACKUP="`dbbackup.sh >&/dev/null`"

you are discarding any output coming from dbbackup.sh (by redirecting it to /dev/null). If you only wanted to discard errors, then use the 2>/dev/null redirection.

Longer story: the line

echo $TARFILE > /dev/tty

redirects the output of the echo command to /dev/tty (the terminal to which the current process is attached) instead of printing to the standard output stream shared by other commands in the dbbackup.sh script. Therefore, when you further redirect the output of dbbackup.sh in dbbackup.sh >&/dev/null, this second redirection does not affect the echo command.

You might want to read a bit more on shell redirection and command substitution.

share|improve this answer

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.