Assigning file attributes to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning file attributes to variables
# 1  
Old 08-18-2006
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
# 2  
Old 08-18-2006
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
# 3  
Old 08-18-2006
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 09:37 AM..
# 4  
Old 08-18-2006
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 10:37 AM.. Reason: Correct variable name and display
# 5  
Old 08-18-2006
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
# 6  
Old 08-18-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a file - and assigning responses to variables (or something)

So first: Sorry if the title is confusing... I have a script I'm writing with a file with several names in it (some other info - but it's not really pertinent...) - I want to be allow the user to delete certain records, but I ran into a problem I'm not sure how to go about fixing. If I were... (6 Replies)
Discussion started by: sabster
6 Replies

2. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

3. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

4. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

5. Shell Programming and Scripting

Getting value from one file and assigning it to variables of another file.

Hi, I need to read two values,startdate and enddate from a param file and then write this value to another param file(global) under a specific workflow name.Here in global param file there is may workflow names, hence we need to check for the right one and then add the data below it. Any... (5 Replies)
Discussion started by: angel12345
5 Replies

6. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

7. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

8. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

9. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

10. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies
Login or Register to Ask a Question