bash 多行 zenity 输入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14968855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
zenity input file with several lines
提问by user1983400
I have a problem with zenity I cannot work out. Could you guys help me?
我有一个无法解决的 zenity 问题。你们能帮我吗?
I have a 7 line long tmp3 file:
我有一个 7 行长的 tmp3 文件:
AAA
BBB
...
FFF
GGG
I want to send this file through zenity so that it displays a checklist with the possibilty to check every line I want with every combination I want.
我想通过 zenity 发送这个文件,以便它显示一个清单,可以用我想要的每个组合检查我想要的每一行。
I previously wrote:
我之前写过:
cat tmp3 | zenity --list \
--column='#' \
--text "Select playlist from the list below" \
--title "Please select one or more playlists" \
--multiple \
--width=300 \
--height=300 \
--checklist \
--column "Select" \
--separator="/ ")
All this does is create one single line in zenity with all 7 files of tmp3. Thats not what I want.
所有这些都是在 zenity 中创建一行 tmp3 的所有 7 个文件。那不是我想要的。
I currently wrote this:
我目前写了这个:
choice=$(zenity --list \
--column "Playlists" FALSE $(cat tmp3) \
--text "Select playlist from the list below" \
--title "Please select one or more playlists" \
--multiple \
--width=300 \
--height=300 \
--checklist \
--column "Select" \
--separator="/ ")
Here something really weird happens that I dont understand. 4 out of 7 fields are created in zenity: AAA CCC EEE and GGG. But not the other ones. When I set -x for debugging I can see all 7 lines being piped to zenity... What is happening?????
这里发生了一些我不明白的非常奇怪的事情。7 个字段中有 4 个是在 zenity 中创建的:AAA CCC EEE 和 GGG。但不是其他的。当我设置 -x 进行调试时,我可以看到所有 7 行都通过管道传输到 zenity ......发生了什么????
I tried another solution by listing the 7 subfolders in my current folder (which happen to have the exact same name as the lines in tmp3). The same thing happens!:
我通过列出当前文件夹中的 7 个子文件夹(恰好与 tmp3 中的行具有完全相同的名称)尝试了另一种解决方案。同样的事情发生了!:
I wrote this:
我是这样写的:
choice=$(zenity --list \
--column "Playlists" FALSE $(ls -d -1 */) \
--text "Select playlist from the list below" \
--title "Please select one or more playlists" \
--multiple \
--width=300 \
--height=300 \
--checklist \
--column "Select" \
--separator="/ ")
The second solution seems easier but my skills aren't very high. And I would like to understand the latter solution and why it does this.
第二种解决方案似乎更容易,但我的技能不是很高。我想了解后一种解决方案以及为什么这样做。
Thank you guys!
谢谢你们!
EDIT: I have found this and tried to make it work my way but no success so far... http://www.linuxquestions.org/questions/programming-9/reading-lines-to-an-array-and-generate-dynamic-zenity-list-881421/
编辑:我发现了这个并试图让它按照我的方式工作,但到目前为止没有成功...... http://www.linuxquestions.org/questions/programming-9/reading-lines-to-an-array-and-生成动态zenity-list-881421/
回答by Armali
The part FALSE $(cat tmp3)expands to
该部分FALSE $(cat tmp3)扩展为
FALSE AAA
BBB
CCC
DDD
EEE
FFF
GGG
What you need is
你需要的是
FALSE AAA
FALSE BBB
FALSE CCC
FALSE DDD
FALSE EEE
FALSE FFF
FALSE GGG
One way to achieve this is --column "Playlists" $(sed s/^/FALSE\ / tmp3) \
实现这一目标的一种方法是 --column "Playlists" $(sed s/^/FALSE\ / tmp3) \
回答by AvanOsch
I know I'm kinda late, but wanted just about the same thing, and figured it out in the end. My solution does a search (hiding errors), adds TRUE and a newline to each result (that was the key!), then sends the result to zenity:
我知道我有点晚了,但想要几乎相同的东西,并最终想通了。我的解决方案进行搜索(隐藏错误),为每个结果添加 TRUE 和换行符(这是关键!),然后将结果发送到 zenity:
CHECKED=`find /music/folder -name "*.mp3" -type f 2>/dev/null | \
awk '{print "TRUE\n"CHECKED=`cat tmp3 | awk '{print "TRUE\n"zenity \
--list \
--checklist \
--column "Buy" \
--column "Item" \
TRUE Apples \
TRUE Oranges \
FALSE Pears \
FALSE Toothpaste
}' | zenity --list --checklist \
--separator='/ ' --title="Select Results." \
--text="Finding all MP3 files..." --column="" --column="Select"`
}' | \
zenity --list --checklist --separator='\n' --title="Select Results." \
--text="Finding all MP3 files..." --column="" --column="Files"`
In your situation, I guess this should be:
在你的情况下,我想这应该是:
find . -name '*.h' |
zenity \
--list \
--title "Search Results" \
--text "Finding all header files.." \
--column "Files"
So it seems Zenity puts each newline in a column, and fills the list that way. This means you can manipulate the strings going into Zenity to add any number of colums.
因此,Zenity 似乎将每个换行符放在一列中,并以这种方式填充列表。这意味着您可以操纵进入 Zenity 的字符串以添加任意数量的列。
回答by Gilles Quenot
There's an interesting example in man zenity:
有一个有趣的例子man zenity:
cat tmp3 | zenity ... ...
You just need to turn on a neurone to adapt it a bit =)
你只需要打开一个神经元来适应它=)


EDIT:
编辑:
if you have an undefined length list, this example will be more interesting :
如果你有一个未定义的长度列表,这个例子会更有趣:
sed 's/^/.\n/' tmp3 | zenity ... ...
回答by Udi
In short and clear summary, you have two options:
简而言之,您有两个选择:
Option one:Input file, newline separate the columns
选项一:输入文件,换行分隔列
Instead of
代替
cat tmp3 | zenity ... ...
do:
做:
zenity ... ... `sed 's/^/. /' tmp3`
Option two:Inline command, the colunms are read as pairs from the command args
选项二:内联命令,从命令 args 中将列作为对读取
Instead of
代替
$ zenity --list --checklist --height 400 --text "Select playlist from the list below" --title "Please select one or more playlists" --column "Playlists" --column "Select" --separator="/ "
$(ls -d -1 */ | xargs -L1 echo FALSE)
do:
做:
##代码##
