Home > OS >  Bash script - Select command and cut results
Bash script - Select command and cut results

Time:12-11

I'm working on a file that contains info such as: 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. 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

CodePudding user response:

You don't need the cut for this particular problem. In pure 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}.

  • Related