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-08-2006 02:31 AM

Quote:

Originally Posted by timmeke
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").


Hi m8,

Thanks for the Info, I need to insert the name on the same line as OWNER so what I plan to do is create a variable based on UID and then in my first SED substitution instead of getting rid of the first "-" regular file symbol of the output from ls -l completely I will replace I it with the variable, that way its on the right line to start with and my second AWK print can be{print "OWNER " $1, $2, $3, $4 }and should look like this.

OWNER $OWNER_VAR YES YES NO

Thanks for the info.

Mike

mike9287 06-08-2006 03:47 PM

Hi temmeke, took your advice and came up with this script:

Code:

#!/bin/sh
# script to determine permission that owner, group
# and everybody has of a file passed to it
USER=`whoami`
echo "Hello $USER, which file would you like to see permissions on?"
read FILE
stat $FILE |
awk '/Uid:/ {print $2, $5}' |
sed -e 's/[0-9]//g;s/)//g;s/\///g;s/(//g; #Extract Uid and permissions
s/-//1;                                  #then convert permissions
1,9s/x/YES /g;                            #into readable form
1,9s/w/YES /g;                            #and pipe to AWK
1,9s/-/NO /g;                            #to format fields
1,9s/r/YES /g' |                          #and display relevant output
awk 'BEGIN {print "                            READ\t WRITE\t EXECUTE" "\n"}
{print "OWNER  "$10,      "            "$2"\t",$3"\t",$4"\t"}
{print "GROUP  USERS                  "$4"\t",$5"\t",$6"\t"}
{print "EVERYBODY                      "$7"\t",$8"\t",$9"\n"}'

and now I get this

Code:


Hello michael.kelly, which file would you like to see permissions on?
file
                                READ    WRITE  EXECUTE

OWNER  michael.kelly          YES      YES    YES
GROUP  USERS                  YES      NO      YES
EVERYBODY                      YES      NO      YES

[michael.kelly@unix michael.kelly]$

:cool:
Not bad for a n00b who couldnt even append a line of text to a file a week ago eh?

Works fine, thanks for you help!

Mike


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