1

Following my previous question, I have write the following MWE to create a simple file selector by date

filesArr=( $(ls -1rt ??????????_?) )
nfiles=${#filesArr[@]}
iLastFile=$[nfiles-1]
choiceArr=( )
for i in `seq 0 $iLastFile`; do
    filesSN[i]=${filesArr[i]:11:1}
    year=${filesArr[i]:0:2}
    month=${filesArr[i]:2:2}
    day=${filesArr[i]:4:2}
    hour=${filesArr[i]:6:2}
    min=${filesArr[i]:8:2}
    wday=`date -d $month/$day/$year '+%a'`
    filesDate[i]="'$wday $day/$month/$year $hour:$min'"
    choiceArr=( ${choiceArr[@]} "'${filesSN[i]}'" "${filesDate[i]}" )
done

#choiceArr=( '4' 'Fri 23/01/15 10:09' '1' 'Mon 02/02/15 09:15' '3' 'Wed 25/03/15 11:38' '2' 'Sat 18/04/15 23:45' )
echo ${choiceArr[@]}

dialog --title "Backup target file"  \
--radiolist "Select the file to replace" 15 60 4 \
"${choiceArr[0]}" "${choiceArr[1]}" ON \
"${choiceArr[2]}" "${choiceArr[3]}" OFF \
"${choiceArr[4]}" "${choiceArr[5]}" OFF \
"${choiceArr[6]}" "${choiceArr[7]}" OFF 2>/tmp/menu.sh.$$

The problem is that this mixes the choices in the radio list

(*) '4'       'Fri
( ) 23/01/15  10:09'
( ) '1'       'Mon
( ) 02/02/15  09:15'

However, if I create the array using the same for loop, print it on screen and then assign it explicitely to the same variable (i.e. uncommenting the commented line), it works fine.

Does anyone has any suggestions?

4
  • choiceArr=( ${choiceArr[@]} ... you haven't quoted ${choiceArr[@]}. Or use the simpler choiceArr+=( "'${filesSN[i]}'" "${filesDate[i]}" ).
    – muru
    Apr 28, 2015 at 11:16
  • Overall this seems like a fragile way to do this. Checkout unix.stackexchange.com/q/198037/70524.
    – muru
    Apr 28, 2015 at 11:21
  • I can't believe it was that simple. (Should I copy your comment as an accepted answer, or it is better if you do it?)
    – Theo
    Apr 28, 2015 at 11:21
  • If I write an answer, I won't be able to resist saying a few more things about it. Might be better if you posted it as the answer.
    – muru
    Apr 28, 2015 at 11:23

1 Answer 1

1

Following @muru 's comment, the problem was solved by replacing the last line inside the loop by choiceArr+=( "'${filesSN[i]}'" "${filesDate[i]}" ).

1
  • Why you put it as 2 separate elements into array when use it both altogether?
    – Costas
    Apr 28, 2015 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .