鞭尾上的动态选项未打印

鞭尾上的动态选项未打印

我有一个 json 文件,它提供“whiptail”复选框或单选列表的选项。我抓住它们,进行一些编辑并想用它们显示为选项:

defaults.json文件:

{"displays":
  [
    {"id": "320x240", "default":"on", "description":"320x240 (native resolution of 3.2 TFT-Display)", "hdmi_group":"2", "hdmi_mode":"87","hdmi_cvt":"320 240 60 1 0 0 0"},
    {"id": "640x480", "default":"off", "description":"640x480", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "720x540", "default":"off", "description":"720x540", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "800x600", "default":"off", "description":"800x600", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "1024x768", "default":"off", "description":"1024x768", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "1280x720", "default":"off", "description":"1280x720 (16:9)", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "1600x900", "default":"off", "description":"1600x900 (16:9)", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"},
    {"id": "1920x1080", "default":"off", "description":"1920x1080 (16:9)", "hdmi_group":"2","hdmi_mode":"87", "hdmi_cvt":"3240 240 60 1 0 0 0"}
  ]
} 

剧本jq

displays=$(cat defaults.json | jq -r -j '.displays[] | "\(.id) \"\(.description)\" \(.default) "')

这给了我以下输出(当我将其直接粘贴到该[tag item status]位置时,该输出有效:

320x240 "320x240 (native resolution of 3.2 TFT-Display)" on 640x480 "640x480" off 720x540 "720x540" off 800x600 "800x600" off 1024x768 "1024x768" off 1280x720 "1280x720 (16:9)" off 1600x900 "1600x900 (16:9)" off 1920x1080 "1920x1080 (16:9)" off

这个效果很好:

whiptail --title "Display setup" --radiolist  "Choose your display" 20 78 8 320x240 "320x240 (native resolution of 3.2 TFT-Display)" on 640x480 "640x480" off 720x540 "720x540" off 800x600 "800x600" off 1024x768 "1024x768" off 1280x720 "1280x720 (16:9)" off 1600x900 "1600x900 (16:9)" off 1920x1080 "1920x1080 (16:9)" off 3>&1 1>&2 2>&3

当我尝试通过变量添加它们时$displayswhiptail只吐出“帮助”文件。

这不起作用

whiptail --title "Display setup" --radiolist  "Choose your display" 20 78 8 $displays 3>&1 1>&2 2>&3

我做错了什么,为什么这不起作用?

答案1

这是因为$displays变量将简单地在空格上分割(使用IFS变量的默认值),而不关心引号,这样的 (将作为两个参数"1920x1080 (16:9)"传递,并且,而不是作为包含 的单个参数。在这种情况下,我强烈建议使用来找出真正传递给命令的参数。whiptail"1920x1080(16:9)"1920x1080 (16:9)set -x

试试这个:

jq -r '.displays[]|.id,.description,.default|@sh' defaults.json |
   xargs whiptail --title "Display setup" --radiolist  "Choose your display" 20 78 8

whiptail似乎总是使用 stdout 作为终端的句柄,因此如果在命令替换中使用,则需要更复杂的东西:

res=$(jq -r '.displays[]|.id,.description,.default|@sh' defaults.json |
   xargs whiptail --output-fd 3 --title "Display setup" --radiolist  "Choose your display" 20 78 8 3>&1 >/dev/tty)
echo "$res"

相关内容