I've just found this command:
upower -e
that displays a list of files that can be used with upower -i
to display plugged device status.
So my first try was using:
upower -e | xargs upower -i
but it doesn't work. So I've tried:
$ upower -e | xargs echo
/org/freedesktop/UPower/devices/line_power_AC /org/freedesktop/UPower/devices/battery_BAT0 /org/freedesktop/UPower/devices/line_power_ucsi_source_psy_USBC000o001 /org/freedesktop/UPower/devices/DisplayDevice
and it display all files in single line. So I've used:
$ upower -e | xargs -0 echo
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/line_power_ucsi_source_psy_USBC000o001
/org/freedesktop/UPower/devices/DisplayDevice
it works but displays one empty line, but this doesn't work:
$ upower -e | xargs -0 upower -i
failed to set path: Object path invalid: /org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/line_power_ucsi_source_psy_USBC000o001
/org/freedesktop/UPower/devices/DisplayDevice
Why upower -e | xargs upower -i
doesn't work? I'm using Bash on Fedora.
Is there something I'm missing here?
EDIT:
This seems to work:
upower -e | xargs -I {} upower -i "{}"
But I'm wondering: why a quote is needed if the filename doesn't have spaces?
Best Answer
upower -e
produces a newline-separated list of object paths.When you used
upower -e | xargs upower -i
, thexargs
command tokenized that on whitespace and passed all the paths to a single invocation ofupower -i
, which it was unable to handle.You then tried
upower -e | xargs echo
, and noted that the output consisted of a single line - that's down toecho
though, and does not tell you how the output ofupower -e
was delimited.Next you tried
upower -e | xargs -0 echo
, which (since the output ofupower -e
is not null delimited) passed a single multi-line argument toecho
, which happily printed it. Similarlyupower -e | xargs -0 upower -i
passed a single multi-line path toupower -i
, which unsurprisingly barfed.Finally you discovered
upower -e | xargs -I {} upower -i "{}"
. Since-I
implies-L
, this passed each line of theupower -e
output to a separate invocation ofupower -i
. You could have achieved the same withWhitespace within the paths isn't a factor here, but if it was, you should tell xargs not only to read a single line per invocation, but also to tokenize it on newlines only:
or equivalently
Quoting the replacement text
{}
probably isn't necessary either (regardless of whether the text itself contains whitespace) - see Quoting curly braces in the shell and the linked duplicate discussing the same issue in the context offind -exec