Search code examples
bashawkzenity

How do I mark only one selection as TRUE in a dynamic zenity radiolist?


This is the code I have at the moment - it finds all files that have the file extension .sv and lists them in a zenity radiolist, however, none of the items are selected. Of course, if I change awk '{print "FALSE\n"$0}' to awk '{print "TRUE\n"$0}' they'll all be selected, which I don't want.

test_name=$(
    find ../tests/ -name '*.sv' | 
    awk '{print "FALSE\n"$0}' |
    cut -d "/" -f3 | 
    cut -d "." -f1 |
    zenity  --list \
            --radiolist \
            --title "Select test" \
            --text "Tests..." \
            --column "Box" \
            --column "Files"
) #select test via zenity

How do I make just the first item be selected, and the rest not? I know this can be done by using arrays, as suggested by this question/answer, however it would require significantly more code. Is there a more elegant way of achieving this?


Solution

  • You can amend your awk command to print "TRUE" if it is the first line to be processed. You can also remove your cut commands by using basename:

    test_name=$(
        find ../tests/ -name '*.sv' -exec basename {} .sv \; | 
        awk 'NR==1{print "TRUE\n"$0} NR>1{print "FALSE\n"$0}' |
        zenity  --list \
                --radiolist \
                --title "Select test" \
                --text "Tests..." \
                --column "Box" \
                --column "Files"
    ) #select test via zenity
    

    You could also use sed in place of awk:

    sed -e '1iTRUE' -e '1!iFALSE'