I want to use whiptail to generate a checkbox list based on the output from another program. Only the first word on each line of the output from the first program is necessary, so I extract it using awk.

To learn about how bash handles arrays, I have been using this link by tldp. I got how to check the size of arrays from here.

This doesn't run as expected, but if I copy the echo output, and type "whiptail" in front of it, it does work.

How do I fix this code so that it works?

example program.sh:

#! /bin/bash
find ./  -printf "%f\n"

example mainscript.sh:

#! /bin/bash
MYARRAY=($(./program.sh -l | awk '{print $1;}'))
MYPARAMS=" --checklist \"\" 15 40 5"
i=0
while [[ $i -lt ${#MYARRAY[@]} ]]
do
        MYPARAMS+=" \"${MYARRAY[$i]}\" \" \" off"
        i=$[$i+1]
done
whiptail $MYPARAMS
echo $MYPARAMS
exit
share|improve this question
    
There should be a space after awk, though I guess it's only a typo here and not present in the script if it produces the output you expect it to. – outlyer Nov 28 '14 at 18:54

Not a very desirable solution, but you can use eval:

eval whiptail $MYPARAMS

I suspect the problem is the quoting. You can however construct MYPARAMS as an array, and it will work as expected:

#! /bin/bash
MYARRAY=($(./program.sh -l | awk '{print $1;}'))
MYPARAMS=( --checklist "" 15 40 5 )
i=0
while [[ $i -lt ${#MYARRAY[@]} ]]
do
        MYPARAMS+=( "${MYARRAY[$i]}" " " off )
        i=$[$i+1]
done
whiptail "${MYPARAMS[@]}"
echo "${MYPARAMS[@]}"
exit
share|improve this answer
    
Is an array better form than a string? – USERID_UNK Nov 28 '14 at 20:37
1  
@USERID_UNK they aren't better, but they allow avoiding the need to escape quotes within the string – outlyer Nov 28 '14 at 20:57

Why did you make MYPARAMS a string and not an array? Since MYPARAMS is a string like --checklist "" 15 40 5 "foo1" " " off, the shell command line whiptail $MYPARAMS results in whiptail being called with the arguments --checklist, "" (a string consisting of two double quotes), 15, 40, 5, "foo1", ", ", and off. When you leave a variable expansion unquoted, its value is split into fields at each whitespace sequence, and the resulting fields are treated as wildcard patterns which are expanded if they match any file. Quotes belong to shell syntax, they are only expanded by the shell parser, not as part of variable expansion.

You can invoke the shell parser with the eval builtin, but it's difficult to get the quoting right. It would be especially difficult if the items might contain characters which don't stand for themselves in shell syntax.

An array is the right way to store a list of strings. Assuming that you've successfully set MYARRAY to the list of tags you want to use, you can loop over that array to build the parameter array.

MYPARAMS=()
for t in "${MYARRAY[@]}"; do
  MYPARAMS+=("$t" " " "off")
done
whiptail "${MYPARAMS[@]}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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