Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls command selection 1

Status
Not open for further replies.

imFrank

Technical User
Mar 30, 2005
28
US
I'm using ls & awk to generate a (small) numbered list of files ending in .art. Rather than having the user enter the long filename, I want to pick a number and pass the respective filename on to the next command. Here's what I have so far:

ls -lh *.art | awk '{ print NR, ":\t", $5, "\t",$6, $7 $8, $9 }'

The output looks like:

1 : 1.4G Mar 3008:46 2005_035155903000000c004.art
2 : 402M Mar 3008:44 2005_036234503000000c004.art
3 : 88M Mar 3008:44 2005_037133003000000x004.art

Enter .art file number (1, 2, 3...):

I'm using: "set INPUT = $<" to capture the users choice (1, 2, or 3...) but how can I use awk to convert the users choice to the respective filename to pass to the next command?

Where the next command would be something like:

read_art_file $INPUT

Thanks,
Frank
 
The question is more suited to the UNIX Scripting Forum forum822.

You don't day which shell you are using. This is bash, but should work for ksh too...
Code:
#!/usr/bin/bash

oldIFS=$IFS
IFS='
'
PS3='Enter .art file number: '
select i in $(ls -lh *.art|awk '{print $5,$6,$7,$8,$9}')
do
  if [[ $i ]]
  then
    echo Number selected: $REPLY
    echo File selected: $(echo $i|awk '{print $5}')
    break
  fi
done
IFS=$oldIFS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top