The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's manas6 UNIX for Dummies Questions & Answers 0 06-05-2008 03:44 AM
extracting fields prvnrk Shell Programming and Scripting 2 10-08-2007 12:39 AM
extracting a string start_shell Shell Programming and Scripting 2 09-30-2007 05:41 AM
Extracting a users environment variables Tornado Shell Programming and Scripting 2 12-04-2006 11:23 PM
extracting from tar.bz2 Raom UNIX for Advanced & Expert Users 1 03-07-2006 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 25
extracting return of ls -l into variables

If I do "ls -l filename" in a script, it should return something like this:

-rw-r--r-- 1 user group 5945 Feb 28 14:24 filename

How do I put each of the above strings into a different variable?

eg Permissions, username, groupname, date
Reply With Quote
Forum Sponsor
  #2  
Old 03-01-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
something like this,

Code:
ls -l filename | awk '{print $1, $3, $4, $6"-"$7"-"$8}' | read perm username grpname date
Reply With Quote
  #3  
Old 03-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 25
Excellent. Thanks a lot!
Reply With Quote
  #4  
Old 03-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 25
Okay, I've been playing around with the command you gave me, matrixmadhan, but I can't figure out how to print the fields $1, $2, $3 etc to different variables. They always either end up in the first variable, or all the variables remain blank. Any ideas?
Reply With Quote
  #5  
Old 03-01-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,614
That command will only work if your shell is ksh.
Reply With Quote
  #6  
Old 03-01-2006
Registered User
 

Join Date: Feb 2006
Posts: 25
Right. I'm in a bash shell. Any ideas about how I go about it in that?
Reply With Quote
  #7  
Old 03-01-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
You can set named variables like $perm $username etc by using something like this....
Code:
eval $(ls -l file1|awk '{printf "perm=%s username=%s", $1, $3}')
...but you can just use set to set $1 $2 etc, e.g...
Code:
set -- $(ls -l file1)
perm=$1
username=$3

Last edited by Ygor; 03-01-2006 at 10:29 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0