4

could someone explain why the code below doesn't work? I'm going crazy trying to find out.

#!/bin/bash

TEST="M1 \"1-wire Interface\" ON"
echo $TEST
RESULT=$(dialog --title "Config Modules State" --checklist "Choose modules to activate" 20 50 1 $TEST)

It prints this at the output, as expected:

M1 "1-wire Interface" ON

'dialog' gives an error saying:

Error: Expected 3 arguments, found only 1.

'whiptail' doesn't give any error but just exits listing out its options.

If I were to take the string that it printed, copy and paste it like this on the command line, it works:

dialog --title "Config Modules State" --checklist "Choose modules to activate" 20 50 1 M1 "1-wire Interface" ON

with both 'dialog' and 'whiptail'. What is going on?

System:

  • Linux raspberrypi 3.18.11+ #781 PREEMPT
  • whiptail (newt): 0.52.14
  • dialog Version: 1.1-20120215
Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41
  • in the line containing the assignment to `RESULT`, the `TEST` variable is subject to word splitting producing four arguments to be passed to dialog: (1) `M1` (2) `"1-wire` (3) `Interface"` (4) `ON`. that is most likely not what you want – Diego May 09 '15 at 23:56
  • why would it split? Shouldn't the double quotes keep it as one argument, just like what I type directly at the prompt? I don't get it. The title too has spaces, enclosed in quotes. Why isn't that split then? and more importantly, how do i solve the problem? – Sujay Phadke May 09 '15 at 23:59
  • http://superuser.com/questions/360966/how-do-i-use-a-bash-variable-string-containing-quotes-in-a-command – Diego May 10 '15 at 00:15
  • ok, referring to the link you have, I converted it into an array. However, it still doesn't work if I add elements to the array (which I need to since the data is dynamic). example: `#!/bin/bash; TEST=(M1 "1-wire Interface" ON); TEST=( ${TEST[@]} M1 "1-wire Interface" ON ); echo ${TEST[@]}; RESULT=$(dialog --title "Config Modules State" --checklist "Choose modules to activate" 20 50 2 "${TEST[@]}" ) ` This only works if the second array appending line is commented out. How do I pass an array of dynamic data to dialog/whiptail in my case? (sorry I can't seem to write a block of code here) – Sujay Phadke May 10 '15 at 00:33
  • 1
    `${TEST[@]}` must also be quoted when extending the array. Run [this](http://pastebin.com/SNE8rDTQ) script. Single quotes delimit what is considered one parameter. See the difference? – Diego May 10 '15 at 00:56
  • ah thanks! I just quoted the array name while extending and also changed double quotes to single quotes while adding string arguments in the array. I wish the bash parser was better written. To waste so much time on single vs double quotes is pointeless. – Sujay Phadke May 10 '15 at 01:08
  • I reffered to the single quotes in the output of the script I linked. – Diego May 10 '15 at 01:16
  • Somebody should answer this so it can be accepted, I guess this won't be the last time somebody comes across this issue. – ShellFish May 10 '15 at 02:30

1 Answers1

0

The answer from the comments if somebody comes across this.

TEST=(M1 '1-wire Interface' ON)
TEST=( "${TEST[@]}" M2 'Other Interface' OFF )
echo ${TEST[@]}
dialog --title "Config Modules State" --checklist "Choose modules to activate" 20 50 2 "${TEST[@]}"
Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74
  • The moderators are rejecting a correction to the above answer, which is not 100% accurate. The edit is as follows: If variables are passed as multiple arguments to a command, as if the arguments contain spaces, the entire argument must be enclosed in single quotes, and not double quotes (as pointed out by @Diego above. Hence, change it like this: `TEST=(M1 '1-wire Interface' ON)` and `TEST=( "${TEST[@]}" M2 'Other Interface' OFF )` – Sujay Phadke May 10 '15 at 22:36
  • What is the difference in this case? The important part, as @Diegio mentioned, is that `${TEST[@]}` is quoted. – Reto Aebersold May 10 '15 at 23:17