有人可以告诉我如何计算所选项目的行号。我想使用特定的行号来引用一个子例程,该子例程将处理不同文件的同一行。
#! /bin/bash
item=$(zenity --list "Apples" "Peaches" "Pumpkin" "Pie" \
--column="Select your choice" --text="Text above column(s)" --title="My menu")
linenumber=x # Formula to calculate the line number of the selected item here
echo "You selected: $item which is in line number: $linenumber"
所需的输出是:
You selected Peaches which is in line number: 2
更新:
这是读取的项目的示例。我在上面的脚本中使用了水果来说明一个示例行。这是特定项目的示例。如您所见,某些实际文本已重复,但在另一行上。当用户选择一个项目时,我希望Zenity可以显示单击哪行。每次运行时,都会有不同的项目列表。
cairo-dock
Desktop
XdndCollectionWindowImp
unity-launcher
unity-panel
unity-panel
unity-dash
Hud
Your turn - Play esskwa003 in HneO9CtF • lichess.org - Google Chrome
ljames@ubunzeus
ljames@ubuntuserver
ljames@hera5
site
site
ljames@ubunzeus
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
ljames@ubunzeus
eclipse desktop launcher categories - Google Search - Google Chrome
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
eclipse
MightyText - Google Chrome
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
ljames@ubunzeus
Inbox - L. D. James - Mozilla Thunderbird
ljames@hera5
ljames@hera5
ljames@ubunzeus
ljames@hera5
How to get the line number of a Zenity selected Item - Unix & Linux Stack Exchange - Google Chrome
workspace - MyPyDev - ShellTools/SEWork/SEWork/hkrecord.sh - Eclipse - /home/users/l/j/ljames/workspace
email - Mozilla Thunderbird
command line - Is it possible to control the recording if Audacity is running in the background? - Ask Ubuntu - Google Chrome
Bookmark Manager - Google Chrome
Formatting Sandbox - Meta Stack Exchange - Google Chrome
Apollo III Support - Backing up the Office Computer - Mozilla Thunderbird
这是我用于调用上述数据的确切块:
#!/bin/bash
INPUT=$HOME/infile.txt
# IFS=$'\n'
item=$(while read l
do
echo "$l"
done <$INPUT|zenity --list --text "sample text " --column "Choose")
echo "You selected: [$item] which is in line number: [$linenumber"]
这对我使用yad和zenity起作用,并且列ID在GUI中不可见:
zenity --list 1 "Apples" 2 "Peaches" 3 "Pumpkin" 4 "Pie" --column="id" \
--column="Select your choice" --hide-column=1 --print-column=1
现在,要在输入为文件时实现相同的效果,可以使用awk
eg对文件进行预处理,
awk '{print NR};1' infile
然后将结果传递给zenity
。
由于,根据文档:
Zenity将所选行的文本第一列中的条目返回到标准输出。
您$item
将只存储行号。(这是第一列中的条目),而不是行内容。
要获取行内容,您必须再次处理文件,然后根据行号提取该行。所以
linenumber=$(awk '{print NR};1' infile | zenity --list --column="No" \
--column="Select your choice" --text="Text above column(s)" \
--title="My menu" --hide-column=1)
然后
linecontent=$(sed ${linenumber}'!d;q' infile)
因此,现在您已将所选行的编号及其内容linenumber
分别保存到和中linecontent
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句