Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add line number for output, prompt for line, then act based on input?

Tags:

linux

shell

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 !

like image 286
Yifan Zhang Avatar asked Feb 25 '12 11:02

Yifan Zhang


People also ask

What is the command to display line number only for the lines which contains data?

The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.

Which command is used to read a line of text from the standard input device and it assigns the read value on the specified variable?

1. Using the read built-in command. One line is read from the standard input, or from the file descriptor supplied as an argument to the -u option.


1 Answers

nl prints line numbers:

ls | grep android | nl 
like image 120
Traz Avatar answered Sep 18 '22 10:09

Traz