0

I am learning bash and am trying to make an application for a shutdown timer using Zenity. As a feedback I want to display the output of the standard shutdown command.

In console, the command:

shutdown -h 30

gives me the output:

Shutdown scheduled for Sun 2022-09-18 13:58:36 CEST, use 'shutdown -c' to cancel.

Now I want to store the output into a variable. I tried:

FEEDBACK=$(shutdown -h +30) 

but then

echo $FEEDBACK

is empty.

Felix
  • 39
  • 6
  • 4
    You can't put a space after the `=`. For the expansion,`$FEEDBACK` or `${FEEDBACK}`, not `$(FEEDBACK)`. – chepner Sep 18 '22 at 11:59
  • Are you sure that message is written to stdout? Log messages or things intended for human rather than programatic consumptions go to stderr instead, or occasionally direct to the TTY when there's a reason the developer wants to make that content hard to capture (f/e, password prompts and input). This is an operating-system-specific detail, and you aren't telling us your OS, so we can't check that operating system's `shutdown` command to see where it writes. – Charles Duffy Sep 18 '22 at 16:28
  • BTW, not your problem here, but `echo $variable` is itself buggy; _always_ `echo "$variable"`, with the quotes. – Charles Duffy Sep 18 '22 at 16:30
  • Anyhow -- "how do I include stderr when capturing a program's output into a shell variable?" is a question that's been asked and answered here many times before. – Charles Duffy Sep 18 '22 at 16:31
  • (the title does specify "Linux", but that's not enough to identify a specific version of the `shutdown` program: busybox `shutdown` is different from systemd `shutdown` is different from pre-systemd util-linux `shutdown`, and all of those can run on the Linux kernel). – Charles Duffy Sep 18 '22 at 16:34
  • Great that you're providing the OS now, but have you checked whether the approach I suggested -- of capturing stderr as well as stdout -- works? See f/e [output not captured in bash variable](https://stackoverflow.com/questions/37115949/output-not-captured-in-bash-variable), which is itself a duplicate of [how to store stderr in a variable](https://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable). – Charles Duffy Sep 18 '22 at 17:21
  • ...that said, Ubuntu 22.04 means it's probably systemd's shutdown command. If you read f/e https://github.com/systemd/systemd/blob/14c811ff4a3025a3ba8b969f7228c05d31eb3796/src/systemctl/systemctl-logind.c#L377-L379, you'll see it's using `log_info` to present the message. I haven't run down the source for that function yet, but at this point I'd put money on it going to stderr. – Charles Duffy Sep 18 '22 at 17:24
  • I edited commands (comment by chepner) and specified the OS (comment by Charles Duffy). The other comment by Charles Duffy pointed me into the right direction. That message I was trying to capture went to stderr. – Felix Sep 18 '22 at 17:25

0 Answers0