Linux

為什麼不出現此錯誤:systemctl show ‘random text here’.device

  • September 10, 2018

樣本:

[ctor@dom0 ~]$ cat nonsense 
#!/bin/bash

set -vex
systemctl show "sys-devices-virtual-block-dm\x2d0.device"
echo 
systemctl show sys-devices-virtual-block-dm\x2d0.device
echo 
systemctl show 'random text here'.device
echo 

\x2d當shell 不解釋 時,以下輸出很好(因為它被引用了):

[ctor@dom0 ~]$ ./nonsense 
systemctl show "sys-devices-virtual-block-dm\x2d0.device"
+ systemctl show 'sys-devices-virtual-block-dm\x2d0.device'
SysFSPath=/sys/devices/virtual/block/dm-0
Id=sys-devices-virtual-block-dm\x5cx2d0.device
Names=sys-devices-virtual-block-dm\x5cx2d0.device
Wants=lvm2-pvscan@253:0.service
Description=/sys/devices/virtual/block/dm-0
LoadState=loaded
ActiveState=active
SubState=plugged
StateChangeTimestamp=Sun 2018-09-09 15:49:14 CEST
StateChangeTimestampMonotonic=10072227
InactiveExitTimestamp=Sun 2018-09-09 15:49:14 CEST
InactiveExitTimestampMonotonic=10072227
ActiveEnterTimestamp=Sun 2018-09-09 15:49:14 CEST
ActiveEnterTimestampMonotonic=10072227
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=no
CanStop=no
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=yes
NeedDaemonReload=no
JobTimeoutUSec=30s
JobTimeoutAction=none
ConditionResult=no
AssertResult=no
ConditionTimestampMonotonic=0
AssertTimestampMonotonic=0
Transient=no
StartLimitIntervalSec=10000000
StartLimitBurst=5
StartLimitAction=none
echo 
+ echo

以下輸出不好,因為sys-devices-virtual-block-dmx2d0.device不存在(注意外殼如何刪除\):

systemctl show sys-devices-virtual-block-dm\x2d0.device
+ systemctl show sys-devices-virtual-block-dmx2d0.device
Id=sys-devices-virtual-block-dmx2d0.device
Names=sys-devices-virtual-block-dmx2d0.device
Description=sys-devices-virtual-block-dmx2d0.device
LoadState=loaded
ActiveState=inactive
SubState=dead
StateChangeTimestampMonotonic=0
InactiveExitTimestampMonotonic=0
ActiveEnterTimestampMonotonic=0
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=no
CanStop=no
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=yes
NeedDaemonReload=no
JobTimeoutUSec=30s
JobTimeoutAction=none
ConditionResult=no
AssertResult=no
ConditionTimestampMonotonic=0
AssertTimestampMonotonic=0
Transient=no
StartLimitIntervalSec=10000000
StartLimitBurst=5
StartLimitAction=none
echo 
+ echo

以下輸出不好,因為random text here.device不存在:

systemctl show 'random text here'.device
+ systemctl show 'random text here.device'
Id=random\x5cx20text\x5cx20here.device
Names=random\x5cx20text\x5cx20here.device
Description=random\x5cx20text\x5cx20here.device
LoadState=loaded
ActiveState=inactive
SubState=dead
StateChangeTimestampMonotonic=0
InactiveExitTimestampMonotonic=0
ActiveEnterTimestampMonotonic=0
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=no
CanStop=no
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=yes
NeedDaemonReload=no
JobTimeoutUSec=30s
JobTimeoutAction=none
ConditionResult=no
AssertResult=no
ConditionTimestampMonotonic=0
AssertTimestampMonotonic=0
Transient=no
StartLimitIntervalSec=10000000
StartLimitBurst=5
StartLimitAction=none
echo 
+ echo

因此,上述兩個不存在.device顯示了類似的通用輸出,並沒有真正表明它們不存在(Substate=dead?)。很容易忘記引用設備名稱並且您不會收到任何錯誤,因此認為這.device是您想要查看的特定資訊的真實資訊。

在 Qubes OS 4.0 的 dom0 中使用來自 Fedora 25 的 Systemd 版本 231:

[ctor@dom0 ~]$ systemctl --version
systemd 231
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN

為什麼 systemctl 不給出錯誤(和退出程式碼!= 0)呢?

show命令旨在由電腦(不是使用者)解析,使用等效(但為人類定義)命令status

$ systemctl status 'random-text-here'.device; echo "Exit Status :$?"
● random-text-here.device
  Loaded: loaded
  Active: inactive (dead)
Exit Status :3

引用自:https://unix.stackexchange.com/questions/467866