0

I'm really pulling my hair out on this one. I just started with some bash scripting and I'm trying to create a simple script that uses some dialogs to give me a fast way to do a svn checkout. It's working fine untill i want to serve a radiolist with multiple options which are fetched when you enter your project for a checkout.

Here's the last part of the code where it fails:

  if [ $MATCHFOUND = true ]; then
    echo 'Exact match found do checkout'
  else
    if [ ${#repoar[@]} == 0 ]; then
      whiptail --title "Error" \
      --msgbox "No matches found for [$PROJECT]" 8 78
      ./dialog.sh
    else
      for ((i=0; i<${#repoar[@]}; i++))
      do
        RADIOLIST=$RADIOLIST" \"$i\" \"${repoar[$i]}\" \"OFF\""
      done

      let ARLENGTH=${#repoar[@]}
      whiptail --radiolist "Projects found" 20 78 $ARLENGTH "$RADIOLIST"
    fi  
  fi

Example for $RADIOLIST

 "0" "test" "OFF" "1" " test 5" "OFF" "2" " test2" "OFF" "3" " test3" "OFF" "4" " test4" "OFF"

The Radiolist arguments do no seem to get escaped like they should when i add the variable as parameter arguments. When i copy past the example using the whiptail command it works fine.

Michael
  • 115
  • 1
  • 9

2 Answers2

2

Quotes get interpreted before variables are substituted, so embedding quotes in variable values doesn't do anything useful. If I understand what you're trying to do properly, the easiest way to do it is to use an array for RADIOLIST:

RADIOLIST=()
for ((i=0; i<${#repoar[@]}; i++))
do
    RADIOLIST+=("$i" "${repoar[$i]}" "OFF")
done

let ARLENGTH=${#repoar[@]}
whiptail --radiolist "Projects found" 20 78 $ARLENGTH "${RADIOLIST[@]}"
Gordon Davisson
  • 11,216
  • 4
  • 28
  • 33
0

Looks like you need to change

RADIOLIST=$RADIOLIST" \"$i\" \"${repoar[$i]}\" \"OFF\""

To

RADIOLIST=$RADIOLIST" \\\"$i\\\" \\\"${repoar[$i]}\\\" \\\"OFF\\\""

This will insert a \ before each " in the generated output, resulting in the desired escaping in the call to whiptail a few lines later.

Mike Insch
  • 1,254
  • 8
  • 10