Bash use of zenity with console redirection

288 Views Asked by At

In efforts to create more manageable scripts that write their own output to only one location themselves (via 'exec > file'), is there a better solution than below for combining stdout redirection + zenity (which in this use relies on piped stdout)?

parent.sh:

#!/bin/bash
    
exec >> /var/log/parent.out
    
( true; sh child.sh ) | zenity --progress --pulsate --auto-close --text='Executing child.sh')
[[ "$?" != "0" ]] && exit 1

...

child.sh:

#!/bin/bash

exec >> /var/log/child.out

echo 'Now doing child.sh things..'
...

When doing something like-

sh child.sh | zenity --progress --pulsate --auto-close --text='Executing child.sh'

zenity never receives stdout from child.sh since it is being redirected from within child.sh. Even though it seems to be a bit of a hack, is using a subshell containing a 'true' + execution of child.sh acceptable? Or is there a better way to manage stdout?

I get that 'tee' is acceptable to use in this scenario, though I would rather not have to write out child.sh's logfile location each time I want to execute child.sh.

1

There are 1 best solutions below

0
Logan Lee On

Your redirection exec > stdout.txt will lead to error.

$ exec > stdout.txt
$ echo hello
$ cat stdout.txt
cat: stdout.txt: input file is output file

You need an intermediary file descriptor.

$ exec 3> stdout.txt
$ echo hello >&3
$ cat stdout.txt
hello