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!

grab permstring and file name from ls -la 3

Status
Not open for further replies.
Feb 12, 2002
80
NO
I need to pass a ls -al output and grab just the:
. permission string
. month
. day
. swing (time/year)
. filename

This seems to be failing:

$line is the entire line, e.g.
Code:
-rw-------   1 sam      samadm        11 Feb 17 15:19 file.txt


Code:
echo "$line" | IFS=" " read permstring junk junk junk junk \
				month day swing rawname



To be clear:
this snippet comes from a much larger script called HardFeed that I found here:

i have spent ages working out that this script is failing at this point for some reason.

Simple suggestions please!

Thanks
 
When you pipe to read for some reason it is run in a subshell under some operating systems, so the variable is not inherited by the current, parent shell.

Try dumping the line to a temporary file and then doing a permstring junk junk junk junk month day swing rawname < filename instead.

You don't need to set IFS since it will use spaces, tabs or newlines by default.

Annihilannic.
 
Thanks for your reply - but I can't get this to work

I have altered the script to read
Code:
\rm /tmp/HardFeed.line.tmp
echo "$line" > /tmp/HardFeed.line.tmp
permstring junk junk junk junk month day swing rawname < /tmp/HardFeed.line.tmp
[\code]

I also tried

[code]
\rm /tmp/HardFeed.line.tmp
echo "$line" > /tmp/HardFeed.line.tmp
awk '{print $1}' /tmp/HardFeed.line.tmp > permstring
[\code]

Any other ideas?
(can't get code to work on this post)
 
You're using the wrong type of slashes to terminate the code tags. :)

Where has your "read" command gone from the first script you attempted?

Annihilannic.
 
I guess that's why I'm littleIdiot ... (-:

I have got this to work in a rather unelegant way:

Code:
\rm /tmp/HardFeed.line.tmp
echo "$line" > /tmp/HardFeed.line.tmp
echo "Read line to tmp file"

permstring=`awk '{print $1}' /tmp/HardFeed.line.tmp`
     month=`awk '{print $6}' /tmp/HardFeed.line.tmp`
       day=`awk '{print $7}' /tmp/HardFeed.line.tmp`
     swing=`awk '{print $8}' /tmp/HardFeed.line.tmp`
   rawname=`awk '{print $9}' /tmp/HardFeed.line.tmp`

I'm happy with that - after spending a whole afternoon finding where the error was and fixing it.

Any better, more elgant solutions welcome!

thanks for your help.

lil'Id
 
And what about this ?
set -- $line
permstring=$1
month=$6
day=$7
swing=$8
rawname=$9

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
One possibility:

Code:
lsinfo=`ls -l filename`
set -- $lsinfo
permstring=$1
month=$6
day=$7
swing=$8
rawname=$9

Annihilannic.
 
Billiant - thank you.

I'll work on that on Monday.

but it's nearly 5pm here in the Netherlands - time I left work and had a beer.

Cheers for your time people - enjoy your weekends.
[afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top