Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

In the Unix bash shell script, how to capture who the user running the script is?

Hi, in the Unix bash shell script I am trying to capture who the user running the script is.

Question:
What would be a Unix bash shell script/commands so that "me007" (Unix user name) can be replaced with a name of the user running this script?

By the way, this script is called from the .bash_profile

Below is a working Unix script which is intended to delete those unneeded SAS* directories without asking a user too many questions but all files in those directories will be deleted automatically as well. I would like any user to be able to run this script from their .bash_profile file but as you see right now it is not possible to do that because there is a specific user name (me007) hardcoded in the script.

Note:
me007 - a user name on Unix server.
/directory1/tmp - a directory where all these processes will occur.
 
for dir in $(find /directory1/tmp -type d -a -user me007 -a  -name "SAS*")
do
   echo "Remove $dir (y/n)?  "; read YN
   [[ $YN = Y || $YN = y ]] && rm -r $dir 
done

Open in new window

SOLUTION
Avatar of AnthonyHamon
AnthonyHamon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of labradorchik

ASKER

Sorry, but I am not sure if I understand you correctly. How can $LOGNAME be implemented within this script?
 
Should I just place export $LOGNAME before this script and that is all?
In that case, what should I exchange "me007" with?
Try this script to see how the variables react in YOUR system!

echo " LOGNAME: "
echo $LOGNAME
echo "*********"
echo " USER: "
echo $USER
echo "*********"

Open in new window

The export may or may not be necessary...
If needed, it will be the first line in the script...

Try the script sample above... Your system will react properly either with $LOGNAME or $USER instead of "me007"...
Oh, just like this?

export $LOGNAME

for dir in $(find /directory1/tmp -type d -a user $LOGNAME -a  -name "SAS*")
do
   echo "Remove $dir (y/n)?  "; read YN
   [[ $YN = Y || $YN = y ]] && rm -r $dir 
done

Open in new window

You forgot - before -user $LOGNAME...

Otherwise OK...
Try each way... $USER and $LOGNAME
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
change the username to:
`whoami`
Thank you everyone for your comments and suggestions!!
I just replaced user with $LOGNAME and it worked fine! :)