pipe stdout to zenity notification
Solution 1
Proper approach would be to do the following:
$ pkexec shutdown -P +60 2>&1 | xargs -L1 -I % zenity --width=250 --height=250 --info --text=%
With the following result:
There are several important things going on:
since you're going to be using GUI popup anyway, use
pkexec
to have GUI popup for password instead ofsudo
.shutdown
command outputs to stderr stream ( file descriptor #2). But pipes only take stdout stream. Thus we need to redirect the contents of stderr via pipe as well. That's what2>&1
does. ( Side note: those who intend to use this withbash
shell only can use|&
instead, however2>&1
works with majority of Bourn-like shells)xargs
lets us take command-line arguments from stdin stream and run a command (in this casezenity --info
) with those arguments.-L1
lets us take single line as argument. Thus the output ofshutdown
will be stored into%
variable and substituted intozenity --width=250 --height=250 --info --text=%
The reason why I'm not using zenity --notification
is also because it has two buttons - cancel and OK, yet for shutdown
command you specifically need to do shutdown -c
to cancel it, thus making the cancel button in the notification dialog absolutely useless.
Solution 2
You can not pipe the text you want to display into zenity --notification
this way.
From man zenity
:
Notification options
--text=STRING
Set the notification text
--listen
Listen for commands on stdin. Commands include 'message',
'tooltip', 'icon', and 'visible' separated by a colon. For exam‐
ple, 'message: Hello world', 'visible: false', or 'icon:
/path/to/icon'. The icon command also accepts the four stock
icon: 'error', 'info', 'question', and 'warning'
So you could either somehow convert the data you pipe into the format message: COMMAND-OUTPUT
and use the --listen
option, or simpler, directly pass the message in the command as argument option behind --text=
:
zenity --notification --text="$(shutdown -P +60 2>&1)"
The command of which you want to capture the output is enclosed in $( )
, which is called Bash "command substitution". It runs the inner command and behaves as if that one's output (standard output stream only) would have been entered in place of it.
Also note the 2>&1
which redirects the standard error stream of the inner command to the standard output stream. This is needed because shutdown
prints its info message to the standard error stream which is not captured by the Bash command substitution.
Related videos on Youtube
Sergiy Kolodyazhnyy
Updated on September 18, 2022Comments
-
Sergiy Kolodyazhnyy over 1 year
I would like to pipe the stdout of
shutdown -P +60
tozenity --notification
. But this does not work:sudo shutdown -P +60 | zenity --notification
-
WinEunuuchs2Unix over 7 yearsWhy not just do zenity "shutting down in 60 seconds" and do the shutdown on the next line? or concatenate with && on a single terminal line?
-