Computing.Net > Forums > Linux > How to read info from a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to read info from a file

Reply to Message Icon

Name: ciroredz
Date: June 25, 2007 at 07:32:45 Pacific
OS: windows
CPU/Ram: 1024
Product: dell
Comment:

i have to write a script that should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this.
READ WRITE EXECUTE
OWNER LEE.BALLANCORE YES YES NO
GROUP USERS YES NO NO
EVERYBODY NO NO NO


how do i read the peermission information from a file and write it like the example above?

Please guys i need your help



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: June 25, 2007 at 09:03:41 Pacific
Reply:

You are in a Linux forum, but your OS reads windows? Are you asking for bash shell scripting help?


0

Response Number 2
Name: ciroredz
Date: June 25, 2007 at 09:15:41 Pacific
Reply:

yes i know that was a mistake!!!!
yes im asking for bash shell script


0

Response Number 3
Name: nails
Date: June 25, 2007 at 09:52:14 Pacific
Reply:

I won't do the whole script for you, but I'll get you started. The first column of the a long listing contains all the permissions for owner, group and everybody else.

After finding the permissions, parse them to get each individual permission. This shows the owner:

#!/bin/bash

fperm=$(ls -l myfile|awk ' { print $1 } ')
echo $fperm
or=$(echo "$fperm"|cut -c2)
echo $or # owner read
ow=$(echo "$fperm"|cut -c3)
echo $ow # owner write
oe=$(echo "$fperm"|cut -c4)
echo $oe # owner execute


0

Response Number 4
Name: ciroredz
Date: June 25, 2007 at 10:01:48 Pacific
Reply:

Thnx a lot mate i really appreciate your interest, but
i ve been told to use less then 15 lines of code and 10 are only for the owner inthe one u gave me...is there another way of doing it?


0

Response Number 5
Name: anonimus
Date: June 25, 2007 at 10:20:28 Pacific
Reply:

Well, make it in one line (long one though):

find ./ -mindepth 1 -ls | awk '\
BEGIN{print "Read", "Write", "eXecute"} \
{\
if (substr($3,1,1)=="d") printf "\n\n folder %s", $11; else printf "\n\n file %s",$11; \
printf "\nOwner %s", $5 ;\
if (substr($3,2,1)=="r") printf " YES"; else printf " NO "; \
if (substr($3,3,1)=="w") printf " YES"; else printf " NO ";\
if (substr($3,4,1)=="x") printf " YES"; else printf " NO "; \
printf "\nGroup %s", $6; \
if (substr($3,5,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,6,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,7,1)=="x") printf " YES"; else printf " NO ";\
printf "\nRestOfWorld "; \
if (substr($3,8,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,9,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,10,1)=="x") printf " YES"; else printf " NO ";\
}'



0

Related Posts

See More



Response Number 6
Name: ciroredz
Date: June 25, 2007 at 10:53:58 Pacific
Reply:

to be honest im a bit confused!!!
i tried to run it but there are lots of mistake that im not able to correct


0

Response Number 7
Name: anonimus
Date: June 25, 2007 at 19:48:40 Pacific
Reply:

>i tried to run it but there are lots of mistake that im not able to correct

did you run that form shell? don't.
1. put this 'line' as is into some file, save it and run as
[prompt]>sh yourfile
it should work
2. add as a first line #!/bin/sh, then "chmod u+x yuorfile"; script is ready. Done. Tell back what you see.

P.S. shell treats 'new line' simbols as one of possible delimiter of instructions. Unless you put '\' on the end! than, even if it looks like bunch of lines, shell treats it as one huge long command line.



0

Response Number 8
Name: ciroredz
Date: June 26, 2007 at 07:46:02 Pacific
Reply:

look at this...would you be able to work out how to use the if statements to dispalay the words yes or no?

[clive@vle ~]$ ls -la .bashrc
-rw-r--r-- 1 clive clive 155 Jul 17 2005 .bashrc
[clive@vle ~]$ ls -la .bashrc | cut -c1
-
[clive@vle ~]$ ls -la .bashrc | cut -c2
r
[clive@vle ~]$ ls -la .bashrc | cut -c3
w
[clive@vle ~]$ ls -la .bashrc | cut -f3 -d" "
1
[clive@vle ~]$ ls -la .bashrc | cut -f4 -d" "
clive


0

Response Number 9
Name: anonimus
Date: June 26, 2007 at 11:27:24 Pacific
Reply:

Yes, please:

master@console:~$ ls -la .bashrc | cut -c1 | awk '{if ($1 != "-") print "Yes"; else print "No";}'
No
master@console:~$ ls -la .bashrc | cut -c1 | awk '{if ($1 == "-") print "Yes"; else print "No";}'
Yes

Also:
master@console:~$ cat yourfile
find ./ -mindepth 1 -ls | awk '\
BEGIN{print "Read", "Write", "eXecute"} \
{\
if (substr($3,1,1)=="d") printf "\n\n folder %s", $11; else printf "\n\n file %s",$11; \
printf "\nOwner %s", $5 ;\
if (substr($3,2,1)=="r") printf " YES"; else printf " NO "; \
if (substr($3,3,1)=="w") printf " YES"; else printf " NO ";\
if (substr($3,4,1)=="x") printf " YES"; else printf " NO "; \
printf "\nGroup %s", $6; \
if (substr($3,5,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,6,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,7,1)=="x") printf " YES"; else printf " NO ";\
printf "\nRestOfWorld "; \
if (substr($3,8,1)=="r") printf " YES"; else printf " NO ";\
if (substr($3,9,1)=="w") printf " YES"; else printf " NO "; \
if (substr($3,10,1)=="x") printf " YES"; else printf " NO ";\
}'

master@console:~$ sh yourfile | head -10
Read Write eXecute


folder ./.gimp-2.2
Owner master YES YES YES
Group master YES NO YES
RestOfWorld YES NO YES

folder ./.gimp-2.2/brushes
Owner master YES YES YES



0

Response Number 10
Name: shivu
Date: August 29, 2007 at 01:49:34 Pacific
Reply:

#!/bin/sh
for i in `ls`
do
OWNER=`ls -l $i | awk '{print $3}'`
GROUP=`ls -l $i | awk '{print $4}'`
OTHERS="Restofworld";
TEMP=`ls -l $i | cut -c 2-10`
j=1
while [ $j -le 9 ]
do
if [ `echo $TEMP | cut -c $j` != "-" ];then
WORD="YES"
else
WORD="NO"
fi
if [ $j -le 3 ];then
OWNER="$OWNER $WORD"
elif [ $j -gt 3 -a $j -le 6 ];then
GROUP="$GROUP $WORD"
else
OTHERS="$OTHERS $WORD"
fi
let j=$j+1
done
ls -l $i
echo "$OWNER"
echo "$GROUP"
echo "$OTHERS"
done


shivakumara K Madegowda


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: How to read info from a file

how to get linux on a..... www.computing.net/answers/linux/how-to-get-linux-on-a/30401.html

How to read 1 line from text file www.computing.net/answers/linux/how-to-read-1-line-from-text-file/15725.html

i need to read a file and redirect www.computing.net/answers/linux/i-need-to-read-a-file-and-redirect-/29705.html