PDA

View Full Version : Bash, zenity clears the variable



5ive
July 3rd, 2011, 12:06 PM
I want to have the result in variable.

apt=`sudo apt-get update`
echo "$apt"
Echo something always returns.

apt=`sudo apt-get update | zenity --progress --auto-close`
echo "$apt"
Echo empty always returns. Why doesn't anything display? How to do well?

mikejonesey
July 3rd, 2011, 01:28 PM
man tee

tee stdout to zenity and cat the stdoutput from file on finish :)

eg


sudo apt-get update | tee mylog.log | zenity --progress --auto-close
cat mylog.log:)

ziekfiguur
July 3rd, 2011, 03:19 PM
apt=`sudo apt-get update | zenity --progress --auto-close`
echo "$apt"
Echo empty always returns. Why doesn't anything display? How to do well?

The reason is that `command` takes the output of command, if you use a pipe to redirect the output from apt-get to zenity and zenity does not output anything there will be no output.

5ive
July 7th, 2011, 11:07 AM
Thank you.

dwhitney67
July 7th, 2011, 11:08 AM
Thank you.

Mark this (your) thread as solved.

5ive
July 20th, 2011, 06:49 PM
var="$(sleep 5 && echo "Ubuntu 11.04 is...")" &
sleep 5
echo $var
Echo empty always returns. Is there a solution? Is there a solution? I want to necessarily result in a variable.

dethorpe
July 21st, 2011, 01:29 PM
(should probably have started new thread for this as its a new question )

However, the reason is the 1st line puts the command into the background in a subshell, so $var is then set in the sub-shell not the parent shell and remains undefiend in the parent.

If you want to capture the output of a background command investigate using the bash coprocess (a few google searchs should bring up all the info you need), that allows you to connect to and capture the background processes stdout.

mikejonesey
July 21st, 2011, 08:21 PM
strange way of doing things... but...

var="$(sleep 1; (echo "Ubuntu 11.04 is..."))"; sleep 2; echo $var