The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
assigning variables from standard output whamchaxed UNIX for Dummies Questions & Answers 2 12-04-2007 03:31 PM
assigning variables in sed command wxornot Shell Programming and Scripting 3 02-04-2006 10:56 AM
Problem in assigning values to variables mohanprabu Shell Programming and Scripting 3 08-25-2005 01:44 AM
Assigning nawk output to variables steveje0711 Shell Programming and Scripting 6 08-19-2005 02:03 PM
assigning variables k@ssidy UNIX for Dummies Questions & Answers 9 06-16-2005 07:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
Assigning file attributes to variables

Hi,

I'm trying to assign the permissions, owner and group of a file to seperate variables, but using

ls -l filename | awk '{print $1 "\t" $3 "\t" $4}'

gives the owner as tom.ja instead of tom.james
Is there any way to expand it so i get the full name, or is there an easier way to get them into variables?

Also, i'm processing the permissions into a table ie Owner has READ and EXECUTE permissions, Group has... etc. I was thinking to use a shift to deal with each permission, is there any way to now seperate each permission? ie.
-rwxr-x-r-- into
- r w x r - x - r - -
it doesn't have to be whitespace, but just so i can process each one.


Thanks

Oliver
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Dear Oliver,

I am not sure about your first problem as it's working fine on my machine as far as tour second problem is concern, you can try this code.

ls -ltr filename | awk '{
for (i=1;i<10;i++)
print(substr($1,i,1));
}'



regards
Apoorva kumar
Reply With Quote
  #3 (permalink)  
Old 08-18-2006
Dhruva's Avatar
Registered User
 

Join Date: Mar 2006
Location: India
Posts: 255
regarding your question about permission,user and group..you can use it with cut.


Code:
line=`ls -l in2.txt`
perm=`echo $line| cut -d" " -f1`
owner=`echo $line| cut -d" " -f3`
grup=`echo $line| cut -d" " -f4`
echo $perm
echo $owner
echo $grup

Last edited by Dhruva; 08-18-2006 at 05:37 AM.
Reply With Quote
  #4 (permalink)  
Old 08-18-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Another way to parse file attributes :
Code:
ls -l x.sh | read perms links owner group size mtime1 mtime2 mtime3
 file
echo "$perms | $links | $owner | $group | $size | $mtime1 $mtime2 $mtime3 | $file"
The output is :
Quote:
-rw-rw-r-- | 1 | gssjgu | gsb4001 | 53 | Aug 18 14:30 | x.sh
Jean-Pierre.

Last edited by aigles; 08-18-2006 at 06:37 AM. Reason: Correct variable name and display
Reply With Quote
  #5 (permalink)  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
Thanks for the advice

Thanks Apoorva, this works great! As for the first question, if the owner name isn't cut short with you, maybe the columns are too small on mine, is there any way to widen them so i get the full username?

Dhruva, could you explain what f1, f3 and f4 do? As when i tried your code the only output is from $perm which is
-rwxr-xr-x

And i'm afraid John-Pierre that yours only prints out the | !!

Thanks again for your help,

Oliver
Reply With Quote
  #6 (permalink)  
Old 08-18-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Dear Oliver,

I have not faced any colmn size problem so far. Still if you think so, you can also try 'printf' function. As far as f1,f2...are concern they just indecate the respective fields.

regards
Apoorva Kumar
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:52 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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0