LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Best approach with this script (https://www.linuxquestions.org/questions/linux-newbie-8/best-approach-with-this-script-451913/)

mike9287 06-05-2006 05:38 PM

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

unSpawn 06-05-2006 07:00 PM

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.

chrism01 06-05-2006 07:02 PM

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

unSpawn 06-06-2006 05:24 AM

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).

mike9287 06-06-2006 12:24 PM

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

mike9287 06-06-2006 03:45 PM

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

unSpawn 06-06-2006 04:42 PM

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".

mike9287 06-06-2006 05:13 PM

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?

Sledge 06-06-2006 11:27 PM

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

mike9287 06-07-2006 02:13 AM

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

timmeke 06-07-2006 04:30 AM

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...

mike9287 06-07-2006 05:10 AM

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

mike9287 06-07-2006 03:46 PM

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

mike9287 06-07-2006 04:41 PM

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?

timmeke 06-08-2006 01:54 AM

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").


All times are GMT -5. The time now is 06:36 AM.