0

I have a json file which provides options for a 'whiptail' checkbox or radiolist. I grab them, make some redactions and wanna use them to show as options:

defaults.json file:

{"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"}
  ]
} 

The jq script

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

which gives me the following output (which works when I paste it directly into the [tag item status] position:

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

This one works perfectly:

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

BUT when I try to add them through the variable $displays, whiptail only spits out the "help" file.

This is not working

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

What I am doing wrong, why is this not working?

0

That's because the $displays variable will simply be split on whitespace (with the default value of the IFS variable), without caring about the quotes and such ("1920x1080 (16:9)" will be passed to whiptail as two arguments, "1920x1080 and (16:9)", not as a single argument containing 1920x1080 (16:9). In cases like this, I warmly recommend using set -x to figure out what arguments are really passed to commands.

Try this instead:

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

whiptail seems to always use stdout as the handle to the terminal, so if used in a command substitution, something a bit more complex is needed:

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"
  • spits out: xargs: illegal option -- d – Jan Nov 3 '18 at 12:37
  • ok, I've modified it. The -d option to xargs is GNU-only. – mosvy Nov 3 '18 at 12:50
  • Cool, it seems to be working: Can you disclose me why capturing the selected result choosenvalue=$(THE_CODE_FROM_ABOVE) in a variable keeps my shell in a stucking status? Full example: choosenvalue$(jq -r '.displays[]|.id,.description,.default|@sh' defaults.json | xargs whiptail --title "Display setup" --radiolist "Choose your display" 20 78 8) – Jan Nov 3 '18 at 13:00
  • When I do the exact same example with dialog instead of whiptail everything works fine: selected_display=$(echo $displays|xargs dialog --separate-output --checklist "Select Tags To Change" 0 0 0 3>&1 1>&2 2>&3) – Jan Nov 3 '18 at 13:21
  • dialog is probably smarter and is directly opening /dev/tty or is using stderr as a handle as the controlling terminal; anyways, I've updated the answer with a a=$(... whiptail) example. – mosvy Nov 3 '18 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.