File/directory information......


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File/directory information......
# 22  
Old 01-08-2002
Yes,

The problem was that using "tr" you need to specify the path of the command in order to execute. This is got to do with your PATH settings. I hope that helps a bit.
# 23  
Old 01-08-2002
Think I just got some where jimbo??

This is what i just did: on the promte line just typed:

echo "rwx" | /usr/bin/tr rwx 421

and it printed this:

421


How can I use this in my script to enable me to print the permissions of a file/directory numericaly?
# 24  
Old 01-08-2002
OK, admittedly this is very yucky, and you should never never do this, but in a pinch:
Code:
#!/bin/sh
if [  -z "$1" ]; then exit 1; fi
p_a=`ls -ld $1 | cut -c1-10`
u_p=`echo $p_a | cut -c2-4`
g_p=`echo $p_a | cut -c5-7`
o_p=`echo $p_a | cut -c8-10`

# Owner
case `echo $u_p` in
rwx) u_n=7 ;;
rw-) u_n=6 ;;
r-x) u_n=5 ;;
r--) u_n=4 ;;
-wx) u_n=3 ;;
-w-) u_n=2 ;;
--x) u_n=1 ;;
---) u_n=0 ;;
*)   u_n=" ? " ;;
esac

# Group
case `echo $g_p` in
rwx) g_n=7 ;;
rw-) g_n=6 ;;
r-x) g_n=5 ;;
r--) g_n=4 ;;
-wx) g_n=3 ;;
-w-) g_n=2 ;;
--x) g_n=1 ;;
---) g_n=0 ;;
*)   g_n=" ? " ;;
esac

# Other
case `echo $o_p` in
rwx) o_n=7 ;;rw-) o_n=6 ;;
r-x) o_n=5 ;;
r--) o_n=4 ;;
-wx) o_n=3 ;;
-w-) o_n=2 ;;
--x) o_n=1 ;;
---) o_n=0 ;;
*)   o_n=" ? " ;;
esac
if [ -d "$1" ]; then
        F_type=" regular directory"
elif [ -f "$1" ]; then
        F_type=" regular file"
else
        F_type="n unknown file type"
fi

echo "$1 is a${F_type}, and has ${u_n}${g_n}${o_n} permissions."

Ick. It won't work for non-files or non-directories (for example, run it and give /dev/null as the file...), nor will it handle any"special" bits set on a file (sticky, setuid, etc...) - this is crude but simple.
# 25  
Old 01-08-2002
Going back to my original code, we just need to change tr to /usr/bin/tr. We will make it a script with filename as a parameter:

#!/bin/sh
ls -ld ${1:?Need one parameter: a filename} |
cut -c2-10 |
/usr/bin/tr [-rwx] [0421] |
sed "s/./& /g" |
awk '{print $1+$2+$3 $4+$5+$6 $7+$8+$9}'

By default, you must have been using another version of tr. Do "which tr" to see. The /usr/bin/tr is working. The code:

1) does ls -ld somefilename
2) cuts the permissions out such as rwxrw-rw-
3) translates those into digits such as 421420420
4) separates with spaces such as 4 2 1 4 2 0 4 2 0
5) awk adds and prints them such as 766

Perseverance! Thanks for hanging in.
Jimbo
# 26  
Old 01-08-2002
I am runnig Solaris 5.6 and using the command line. The tr command is in /usr/bin.
# echo "-777" | tr '\-rwx' 0421
0777


I also got this to work taking dashes into consideration. The permissions on this README file are 644:

# ls -l README | cut -c2-10 | tr '\-rwx' 0421
420400400
# 27  
Old 01-08-2002
I made this simple script to test wat u said jimbo:

print -n "Enter file name: "
read name
ls -ld $name |
cut -c2-10 |
/usr/bin/tr [-rwx] [0421] |
sed "s/./&/g" |
awk '{print $1+$2+$3+$4+$5+$6+$7+$8+$9}'


Comes up with a error message:
/usr/bin/tr: Bad string.

Doesn't seem iv soloved the problem, but these worked:
echo "-777" | tr '\-rwx' 0421

echo "rwx" | /usr/bin/tr rwx 421




Just tried this:
ls -l menu | cut -c2-10 | tr '\-rwx' 0421
which produced this outcome:
421000000 (which is correct)

but, when i tried this:
ls -l menu | cut -c2-10 | tr '\-rwx' 0421 | sed "s/./& /g" | awk '{print $1+$2+$3 $4+$5+$6 $7+$8+$9}'
produces:
421000000 which is the same as the above, seems that its not converting.

So, the problem is between:
sed "s/./& /g" |
awk '{print $1+$2+$3 $4+$5+$6 $7+$8+$9}'
those lines of code

Can ne1 help??
Smilie Smilie

Last edited by Makaveli.2003; 01-08-2002 at 04:38 PM..
# 28  
Old 01-08-2002
The problem now is with sed. As coded, for me that sed command will convert 421000000 to 4 2 1 0 0 0 0 0 0. If I come up with an alternate way to insert spaces between each character, I will let you know. In the meantime, since your PATH was getting to a problematic tr command, maybe you should try changing sed to /bin/sed (or whatever).
Jimbo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

2. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

3. UNIX for Dummies Questions & Answers

How can I use my code to gather information from a file in a completely different directory?

I need my code to compare two different files that are in two completely different directories, How can I do this? So for example, my code will look at file1 which is in my home directory, and compare the files with those from file2 that is in /abc/adf/adr/afc/adf/file2... does that make sense? (1 Reply)
Discussion started by: castrojc
1 Replies

4. Shell Programming and Scripting

reading a file extracting information writing to a file

Hi I am trying to extract information out of a file but keep getting grep cant open errors the code is below: #bash #extract orders with blank address details # # obtain the current date # set today to the current date ccyymmdd format today=`date +%c%m%d | cut -c24-31` echo... (8 Replies)
Discussion started by: Bruble
8 Replies

5. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

6. Shell Programming and Scripting

Only the required tag information the XML file file

Hi i have a single line xml file having many account no tag, from which i need only the account no from the tag. any one can help on this. below is the xml file: ... (2 Replies)
Discussion started by: Saravanapk
2 Replies

7. Shell Programming and Scripting

Generating an xml having information related to files in the directory

Hi all, Have to generate an xml having information related to files in the directory Suppose i have file file1.xml (datafile) file2.xml (datafile) file3.xml (metafile) Now i need to generate an xml in the format >> <?xml version="1.0" encoding="UTF-8"?> <AuditFile Version="2.0">... (8 Replies)
Discussion started by: abhinav192
8 Replies

8. UNIX for Dummies Questions & Answers

Read directory and fill a file with this information

Hello All, Got a question to make a script what reads a directory and put the file names from that directory in a file with some extra text. ls /tempdir output is: file1.gfh file2.txt file4.zzz these file names (always with an extention) must be placed in a line and written to... (2 Replies)
Discussion started by: ToXiQ
2 Replies

9. UNIX for Dummies Questions & Answers

why does tar sometimes include directory information?

hi, say I have dirA/file1 dirB/file2 and I tar them up, and then do zcat Tar.tar | tar tvf - Sometimes I will see: dirA/ dirA/file1 dirB/ dirB/file2 yet other times I will see (4 Replies)
Discussion started by: JamesByars
4 Replies

10. UNIX for Dummies Questions & Answers

date information of a directory

when i list the content of a directory date informations are also listed (ls -ltr) for included files and directories. for example: i see the date Jun 29 14:43 for one of the included directory, but when i look into the files contained in this directory i cannot see any file with this... (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies
Login or Register to Ask a Question