pipe stdout to zenity notification

7,406

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:

enter image description here

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 of sudo.

  • 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 what 2>&1 does. ( Side note: those who intend to use this with bash shell only can use |& instead, however 2>&1 works with majority of Bourn-like shells)

  • xargs lets us take command-line arguments from stdin stream and run a command (in this case zenity --info) with those arguments. -L1 lets us take single line as argument. Thus the output of shutdown will be stored into % variable and substituted into zenity --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.

Share:
7,406

Related videos on Youtube

Sergiy Kolodyazhnyy
Author by

Sergiy Kolodyazhnyy

Updated on September 18, 2022

Comments

  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 1 year

    I would like to pipe the stdout of shutdown -P +60 to zenity --notification. But this does not work:

    sudo shutdown -P +60 | zenity --notification
    
    • WinEunuuchs2Unix
      WinEunuuchs2Unix over 7 years
      Why 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?