Question

I have a problem with zenity list. The problem is that if I choose e.g. opt2 return me opt1 and opt2 as result. When I run command from terminal, the result is correct. Here is my script

#!/bin/bash

opt1=$(cat ~/test.txt | head -n 1 | tail -n 1)
opt2=$(cat ~/test.txt | head -n 2 | tail -n 1)
opt3=$(cat ~/test.txt | head -n 3 | tail -n 1)

response=$(zenity  --list  --width="350"  --height="350" --text "Text" --checklist  --column "Text" --column "Text" FALSE "$opt1" FALSE "$opt2" FALSE "$opt3"   --separator=":")

(
[[ $response = *$opt1* ]] && echo "$opt1" >> file.txt
[[ $response = *$opt2* ]] && echo "$opt2" >> file.txt
[[ $response = *$opt3* ]] && echo "$opt3" >> file.txt
[[ $response = *$opt4* ]] && echo "$opt4" >> file.txt
[[ $response = *$opt5* ]] && echo "$opt5" >> file.txt
)

Also I tried with [ $response = "$opt2" ] but without success and with [ $response = $opt2 ] works only if I choose one option from list but if I choose multiple choices the result is blank. The file test.txt is something like that

name-1
name-1-new
name-1-0-new
name-2
name-2-new
name-2-0-new
name-3
name-3-new
name-3-0-new

Has anyone any idea how I fix that?

Was it helpful?

Solution

Your test whether a value is in the result is to blame because in case you search for name-1 in a result containing name-1-new you will find it, so your program will act as if the $opt1 is in the result.

If you can, choose names for those opts which cannot be mistaken for one another. That would be the easy "poor-man's" approach but it might suite you.

A better way would be to search for the delimiters along with the values:

[[ :$response: = *:$opt1:* ]] && echo "$opt1" >> file.txt

etc. In any case make sure that delimiter cannot be part of the values of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top