Yifan Zhang Yifan Zhang - 4 months ago 7
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 :

android1
android2
xx_android
...


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

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"
    fi
done
Comments