LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2006, 06:38 PM   #1
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Rep: Reputation: 15
Best approach with this script


Hello,

I need to write a script to do the following:

a script that takes an argument of a file. The script should be able to determine what permissions the owner, group and everybody has of the file passed in. The output should look similar to this

..............................READ WRITE EXECUTE

OWNER MICHAEL KELLY YES YES NO
GROUP ......................YES NO NO
EVERYBODY................. NO NO NO

I don't want any code I simply want a nudge in the right direction or advice on a potenial way to be able to extract and display the above info. Is there a command in unix that can display info like this rather than displaying rwx------ when command and option "ls -i" are specified or should my script be able to produce the above info using variables from an output such as rwx------?

BTW the lines of ....'s are not to be part of the script they are simply to improve readablilty of my thread.

TIA

Mike

Last edited by mike9287; 06-05-2006 at 06:39 PM.
 
Old 06-05-2006, 08:00 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
I'd use "stat -c %a". Note octal 1st digit is setuid/setgid/sticky.
So either: a=$(stat -c %a filename); [ "${#a}" = "3" ] && a="0$a"
or: [ "${#a}" = "4" ] && a="${a:1:3}"
Maybe o=1; g=2; e=3; case "${a:$o:1}" in ...; etc
Of course you could favour human readable: a=$(stat -c %A filename)
and work with that: expr index rwx ${a:4:1} > /dev/null && echo YES || echo NO


I don't want any code
OK.
 
Old 06-05-2006, 08:02 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
I'd use
info=`ls -l $1`
to get all the info, then awk to get each whitespace separated field, then cut to separate out the permission chars.
man ls
man awk and http://www.oracle.com/technology/pub...laney_awk.html
man cut
 
Old 06-06-2006, 06:24 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
I'd use (..) ls
I don't want to turn this into a pissing contest, but talking efficiency you'd see using an array saves you two commands, using "stat -c "%a %G %U" filename" gives you exactly the information you want/need, and "stat" has more output choices compared to "ls" (which only numerical switch is for id's).
 
Old 06-06-2006, 01:24 PM   #5
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi, I tried using the "-c" option and I get an error telling me that options -l -f -v -t can only be used, I googled and found some info that lists -c as an option but my shell dosent seem to like it, do you think this maybe the shell or me inputting wrong?

Thanks for both replies.

Mike
 
Old 06-06-2006, 04:45 PM   #6
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by mike9287
Hi, I tried using the "-c" option and I get an error telling me that options -l -f -v -t can only be used, I googled and found some info that lists -c as an option but my shell dosent seem to like it, do you think this maybe the shell or me inputting wrong?

Thanks for both replies.

Mike

BTW by I mean when I say I can't use the -c option that when I try it simply on the command line like so

stat -c myfile

that I get the error message of invalid option, do I need to use -c under some other circumstances? I tried the man pages and google has nothing much on the stat command, my course dosent mention it once! in fact I think they must be expecting me to get the info using another method but checking the material they provide there is nothing that stands out as a way of producing human readble output like I listed in my initial post, I am confident that I can use AWK to get the layout and add text where needed but I guess I am struggling with extracting the initial info.

Any help or info on this -c option would be great.

TIA

Mike
 
Old 06-06-2006, 05:42 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
I tried the man pages and google has nothing much on the stat command
If you "man stat" somewhere it reads somewhat like "valid format sequences for files". Below that the format modifiers are listed. In my second post I offered an example which is usable if you remove the outer quotes and replace filename with an valid filename. If omens read getting into deeper scripting waters it might be beneficial to scour around for the ABS or "Advanced Bash Scripting Guide".
 
Old 06-06-2006, 06:13 PM   #8
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi, thanks again for your reply.

I checked man and there is no mention of -c or format, here is the complete man page I see:


STAT(1) STAT(1)

NAME
stat - display file or filesystem status

SYNOPSIS
stat [-l] [-f] [-v] [-t] file-name [file-name]...

DESCRIPTION
This command displays information about the specified file(s). You do not need any access rights to the file to get this information
but you need search rights to all directories named in the path leading to the file.

stat stats the file pointed to by file-name

stat -l is identical to stat, only that for links information about the files that are obtained by tracing the links is displayed.

stat -f does not stat the file itself but instead stats the filesystem where file-name is located.

stat -v prints version information.

stat -t prints the information in terse form, suitable for parsing by other programs

DISPLAY
stat and stat -l both display the following information:

Device number

Inode number

Access rights

Number of hard links

User ID (and name if available) of owner

Group ID (and name if available) of owner

Device type (if inode device)

Total size, in bytes

Number of blocks allocated

IO block size

Time of last access

Time of last modification

Time of last change

If -f is specified the following information is displayed:

Filesystem type

Block size of the filesystem

Total blocks in the filesystem

BUGS
None known so far.

AUTHORS
Written by Michael Meskes <meskes@debian.org>, -f added from statfs by Radovan Garabik <garabik@melkor.dnp.fmph.uniba.sk>

SEE ALSO
stat(2), statfs(2)

Linux 28 August 1997 STAT(1)

-----------------------------------------------------------------------------------------------------------

I then found this online which must refer to version you are currently using - http://linux.about.com/library/cmd/blcmdl1_stat.htm

I tried the the command you listed before I even posted back here, I tried without quotes to begin with and my shell won't entertain the -c option, is there another way to make it work? i can log into my Mandriva account and use it there but I was instructed to use the app called "putty" by the training company so there must be away of doing it with what I all ready have.

Regards

Mike

EDIT: I noticed it mentions stat(2), is this a variation of stat and how do I call it on the command line if thats the case?

Last edited by mike9287; 06-06-2006 at 06:15 PM.
 
Old 06-07-2006, 12:27 AM   #9
Sledge
Member
 
Registered: Nov 2003
Distribution: Fedora Core 6
Posts: 76

Rep: Reputation: 15
What shell are you using? In bash I see this as the beginning of man stat:

NAME
stat - display file or file system status

SYNOPSIS
stat [OPTION] FILE...

DESCRIPTION
Display file or file system status.

-f, --file-system
display file system status instead of file status

-c --format=FORMAT
use the specified FORMAT instead of the default

-L, --dereference
follow links

-t, --terse
print the information in terse form
 
Old 06-07-2006, 03:13 AM   #10
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hello, I am using a windows account and then a program called Putty which is a telnet & SSH client, the servers I am connecting to are Red Hat.

http://www.softpedia.com/progScreens...shot-1001.html

I am not sure whether its putty's shell I am using or Red Hat's but there is no -c option.

Does anyone know what it means by see Stat(2) in the Stat man page?

Mike
 
Old 06-07-2006, 05:30 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
It means that you need to take a look at the man page for "stat" in the 2nd man section.
Do this like so:
Code:
man 2 stat
Different man sections are for different purposes (ie C/C++ library functions, shell commands, general system config info, etc).
But don't worry, in your case, "man 1 stat" or "man stat" (which both should give the man page you've posted - see STAT(1) at the top) will do just fine. "man 2 stat" will give you the manual for C "stat" function.

Depending on your system settings, a different man section can be accessed by default, so "man stat" may actually resolve to different man pages on different machines.

Anyway,
Code:
info coreutils stat
should do the trick.

My old RH8 box doesn't seem to have the "stat" coreutil (man 1 stat doesn't exist, man stat yields the man page for C function "stat" and calling "stat" gives "command not found). My Fedora box however does have stat.
So, maybe it's depending on the RedHat version...
 
Old 06-07-2006, 06:10 AM   #12
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi temmeke,

Thanks for the info, I will check it out once I get home. I think if it dont work in putty I will try in Mandriva and tell the Academy i did my course work from there rather than this putty program. I know its probably possible to write a program which does what I need from a "rwx------" output but for me it would be a little complicated and impracticle if the "-c" option provides me with exactly what I need.

Thanks again.

Mike
 
Old 06-07-2006, 04:46 PM   #13
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi, I have sort of wrote some of the script but its real messy and will look even worse if I pursue thigns the way I am. Here is what have so far:

#i!/bin/sh
# script to determine permission that owner, group
# and everybody has of a file passed to it
FILE=myfile
ls -l $FILE |
awk '{print $1}' |
sed -e 's/-//1;s/e/YES /g;s/w/YES /g;s/-/NO /g;s/r/YES /g' > temp2


and then temp2 looks like this:

YES YES NO YES NO NO YES NO NO

and I need to display it as listed in my intial post, i am sure I can do this but it will be a crappy script and I will probably be going the longest way round ever. I can't use "stat -c" on my shell.

Can anyone get me started with another approach, I hate to sound like I am skyving becuase I am not, all my spare time goes into this at the moment.

Cheers

Mike

BTW, just incase you think it dosent look to bad and to carry on and format it with AWK using fields then let me know

Last edited by mike9287; 06-07-2006 at 04:53 PM.
 
Old 06-07-2006, 05:41 PM   #14
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi again,

I have it almost done, it dont look as bad as I though either.

My code is now:

#i!/bin/sh
# script to determine permission that owner, group
# and everybody has of a file passed to it
FILE=myfile
ls -l $FILE |
awk '{print $1}' |
sed -e 's/-//1;s/x/YES /g;s/w/YES /g;s/-/NO /g;s/r/YES /g' |
awk 'BEGIN {print " READ\t WRITE\t EXECUTE"} {print "OWNER " $1"\t", $2"\t", $3"\t"}
{print "GROUP USERS "$4"\t",$5"\t",$6"\t"} {print "EVERYBODY "$7"\t",$8"\t",$9"\t"}'

Which produces this:

........................READ.....WRITE...EXECUTE
OWNER...................YES......YES.....NO
GROUP...USERS...........YES......NO......NO
EVERYBODY...............YES......NO......NO

a couple of questions still, I need to insert users name who is executing the script, how would I go about this? can I use "whoami" as a variable? also, where it says USERS, I am not sure if thats ment to be as it looks a generic term or an actual real time print of the users in the group. anyone any ideas?

Last edited by mike9287; 06-07-2006 at 06:46 PM.
 
Old 06-08-2006, 02:54 AM   #15
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Code:
user=`whoami`;
echo "Current user is: $user";
should do it.
whoami prints the eUID (effective User ID). So, when you "su" to impersonate someone else, the whoami output changes too.

To get the name of the user who owns the file, is slightly different.
You can use your version of "stat" to get the UID and username (if available) (cfr your post of "man stat").
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell script - variable magic needed (no idea how to approach this) arrenlex Programming 8 03-02-2006 06:41 PM
permissions - is this the best approach? Chinook Linux - Security 3 01-19-2006 06:38 PM
Best upgrade approach oxman Fedora - Installation 2 12-24-2004 03:08 PM
backup approach ulaoulao Linux - Software 0 10-05-2004 08:03 AM
How do i approach those security advisories vavoem Linux - Security 1 09-11-2004 08:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration