Yifan Zhang Yifan Zhang - 1 year ago 64
Bash Question

how to add line number for a output

I wrote a shell script like this:

#! /bin/sh
ls | grep "android"

and the output is :


I want to add a number in each file, like this:

1 android1
2 android2
3 XX_android
please choose your dir number:

and then wait for the user input line number x, the script reads the line number back then process the corresponding dir. How can we do this in shell ? Thanks !

kev kev
Answer Source

Instead of implementing the interaction, you can use built-in command select.

select d in $(find . -type d -name '*android*'); do
    if [ -n "$d" ]; then
        # put your command here
        echo "$d selected"