简体   繁体   English

Bash 脚本 - Select 命令和剪切结果

[英]Bash script - Select command and cut results

I'm working on a file that contains info such as: service.txt我正在处理一个包含以下信息的文件:service.txt

service1 - info1
service2 - info2
service3 - info3
...

I added each line of the file to an array.我将文件的每一行添加到一个数组中。 Subsequently with the select command I want to query the elements of the array displaying only the "serviceN" and display the "info" once the element has been selected.随后使用 select 命令,我想查询仅显示“serviceN”的数组元素,并在选择元素后显示“info”。 At the moment I can't cut the line to display only the "service" `目前我不能切断线只显示“服务”`

#File example
#service.txt
#service1 - info1
#service2 - info2
#...
#serviceN - infoN

#!/bin/bash
file='service.txt'
line=()
while read -r line; do
        line+=($line)
done < $file

echo "Select the service..."
select line in ${line[@]}; do      # how can i use "cut" here?
        echo $line
        break
done

exit 0

You don't need the cut for this particular problem.对于这个特定问题,您不需要cut In pure bash:在纯 bash 中:

#!/bin/bash

readarray -t lines < service.txt
echo "Select the service..." >&2
select service in "${lines[@]%%[[:blank:]]*}"; do
    echo "$service"
    break
done

For the "${lines[@]%%[[:blank:]]*}" , see Shell Parameter Expansion , paragraph starting with ${parameter%word} .对于"${lines[@]%%[[:blank:]]*}" ,请参阅Shell Parameter Expansion ,以${parameter%word}开头的段落。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM