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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a select loop with input from the 'file' command 2

Status
Not open for further replies.

GirlInTrouble

Technical User
May 27, 2004
12
GB
Hi everyone
My head is getting to know the brick wall rather well by now.
My problem is all do to do with passing a list of files using the file command into a select loop. Now I can make the file command give me what I think works because I hard coded it into 2nd script, it doesn’t look pretty because it (input) is split over a number of lines.

It may be something to do with the way commands are parsed (which I don’t know ANYTHING about) or my not accounting for how tabs are pasted


I've stripped most of the junk out and this is just part of a script that I'm trying to make work.
Anyhow I’ve tried using xargs and eval anyhow this is the closest I’ve got to what I want


#VER=$(file * |sed 's/^/"/'|sed 's/ //g'|sed 's/$/" /' |sed 's/\n//')

#echo $VER
PS3="Select your file: "
select x in $(file * |sed 's/^/"/'|sed 's/ //g'|sed 's/$/" /'|sed 's/\n//') Exit
#`echo $VER` Exit
do
case "$x" in
*) return;;
Exit) return;;
esac
done


This doesn’t give me what I want, what it does is that it works for anything that doesn’t have spaces In it and it actually codes the “ into the option text! I know I went overboard with the SED and it HAS been worse, I managed to get it to write the “\” in as shown below. Anyhow its just getting stupid – I just can’t think of anything else. Any ideas???????

I mean I know select works with whatever you supply with “abc: def“

This is the hard coded version with a subset of the data pasted in (is that the problem?):

PS3="Please select your file: "

select x in "try: commands text" "try10: commands text" "try11: commands text" "try11b: commands text" "try12: commands text" "try2: commands text" "try4: assembler program text" "try5: commands text" "try6: commands text" "try7: assembler program text" "try8: assembler program text" "try9: commands text" Exit
do
case "$x" in
*) return;;
Exit) return;;
esac
done
 
Something like this ?
[tt]PS3=$(echo "E) Exit\nSelect your file: ")
select x in $(file * | sed 's![ ][ ]*!_!g')
--v-- --v--
Tab Tab[/tt]

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm not sure how best to do this using ksh or sh only, but you can always rely on perl?
Code:
#!/usr/bin/perl -w
@files = grep { -f } glob "*";
for ($i = 0; $i <= $#files; $i++) {
    printf "%2d) %s", $i, qx(file $files[$i]);
}
print "choose one: \n";
Cheers, Neil
 
You could reset the internal field separator IFS, eg....
Code:
#!/usr/bin/ksh

IFS="
"
select x in $(file *)
do
  break
done
unset IFS
 
Very nice, Ygor.
A slight variation to avoid some side effect:
oldIFS="$IFS"
IFS="
"
select x in $(file * | pr -e -t)
do
break
done
IFS="$oldIFS"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Cheers toolkit & PHV! High praise indeed!

PS. I guess that using "unset IFS" (to reset to default) only works for POSIX-compatable shells?
 

Hi there and thanks to everyone who posted :)

Ok I tried all the ideas except the perl one (only bcos I haven't got to perl yet!)

Looks like you guys have cracked it, I was obviously making it TOO complicated. I havn't used the pr command much so will be checking out the man page in a sec to remind myself.
The solution which you all kicked in a piece has resulted in a beautifully formatted result without compromises (temp file) WOW.

I'm new to this forum but I like the atmosphere, so you may hear from from me again.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top