?
Solved

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

Posted on 2013-01-30
10
Medium Priority
?
852 Views
Last Modified: 2013-01-30
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

0
Comment
Question by:labradorchik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Assisted Solution

by:AnthonyHamon
AnthonyHamon earned 600 total points
ID: 38836696
The currently logged on user is in the environment variable $LOGNAME
echo You are logged on as $LOGNAME

You may need to export it first:
export LOGNAME
0
 
LVL 20

Accepted Solution

by:
n2fc earned 800 total points
ID: 38836722
instead of "me007" use "$USER" or "$LOGNAME" environment variable to pick up logged on user id
0
 

Author Comment

by:labradorchik
ID: 38836729
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?
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 20

Expert Comment

by:n2fc
ID: 38836744
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

0
 
LVL 20

Expert Comment

by:n2fc
ID: 38836751
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"...
0
 

Author Comment

by:labradorchik
ID: 38836757
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

0
 
LVL 20

Expert Comment

by:n2fc
ID: 38836774
You forgot - before -user $LOGNAME...

Otherwise OK...
Try each way... $USER and $LOGNAME
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 600 total points
ID: 38836799
LOGNAME is in the environment anyway, so no need to export it.

Further, the shell will expand $LOGNAME before "find" will see it, another reason why it doesn't have to be exported.

Next, the statement, if used anyway, must read "export LOGNAME" without "$" in front.

Lastly, please note that $USER and $LOGNAME will not change to "newuser" after issuing "su newuser", but $(whoami)  and $LOGIN will.
After "su - newuser" all four variables will change to "newuser", but $(who am i | cut -f1 -d" ") will not.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38836928
change the username to:
`whoami`
0
 

Author Comment

by:labradorchik
ID: 38837443
Thank you everyone for your comments and suggestions!!
I just replaced user with $LOGNAME and it worked fine! :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question