Operating System - HP-UX
1821638 Members
3098 Online
109633 Solutions
New Discussion юеВ

How to find a user ID from a shell script

 
SOLVED
Go to solution
Nalin Uduwawala_1
Frequent Advisor

How to find a user ID from a shell script

Could someone please tell me what shell script command can be used within a shell script to find out who is presntly invoking the script ?

and store that in a variable ?

we are using HP-ux 10.20

Thanks in advance.
15 REPLIES 15
James A. Donovan
Honored Contributor
Solution

Re: How to find a user ID from a shell script

The "id" command.

You can do something akin to:

#!/bin/sh
X=`id -nu`
echo $X

X is set to the name of the invoking user.
Remember, wherever you go, there you are...
James A. Donovan
Honored Contributor

Re: How to find a user ID from a shell script

hmmm, it's early and I haven't had my coffee yet...

echo $LOGNAME

will also give you what you want.
Remember, wherever you go, there you are...
federico_3
Honored Contributor

Re: How to find a user ID from a shell script

You could use
#user_id=`id -u ` ( variable that stores the user id -> 0 for root )
# user_account=`id -nu` ( variable that stores the account )


federico
Nalin Uduwawala_1
Frequent Advisor

Re: How to find a user ID from a shell script

Jim,

which is better to be used with elm to send a file (a report that the user invoked or a message that something happened) ?

are both solutions equally valid ?

Sorry if I woke you up !!!! But you have to get up sometime !!!!!!!!!!.

Thanks again. Have a good day.
James A. Donovan
Honored Contributor

Re: How to find a user ID from a shell script

You're confusing my caffeine-starved brain here...are you asking whether it's better to send a report as an attachment or to send a notification that a report has been generated? Is the person you're sending the e-mail to, the same person you needed to get the username for?

Please clarify what it is you need to do...
Remember, wherever you go, there you are...
Nalin Uduwawala_1
Frequent Advisor

Re: How to find a user ID from a shell script

Yeah You seem to had a pretty busy night and apologies for making things worse.

a user logs in to hpux based application.

user runs a command from the application (which for eg: generates a report). This user should receive the report in his e-mail. From the application a shell script will be invoked to run the report or to generate the message.

The userid within the application and UNIX are identical. Infact the application picks it up from UNIX.

so what I would like to know is who ran the script so that within the script I can say
"elm $logname" ?

Have I confused you further ? I hope not.

I am not certain what caffeine does to a confused mind. But some Jack Daniels may help !!!!

have a good day
Andreas Voss
Honored Contributor

Re: How to find a user ID from a shell script

Hi,

just another solution:

elm `whoami` ...

Regards
James A. Donovan
Honored Contributor

Re: How to find a user ID from a shell script

OK, I think I follow...

1. The user logs in to an application.
2. The application generates a report
3. The application calls a script to e-mail the report to the user

Then yes, invoking "elm $LOGNAME" should send e-mail to your user.
Remember, wherever you go, there you are...
Nalin Uduwawala_1
Frequent Advisor

Re: How to find a user ID from a shell script

Hi,

Thanks for all the good suggestions.

JIm I will go with your suggestion. However with a small change.

elm $logname does not work for some reason
It should be elm $$logname.

This is not the full solution as the elm editor pops up even with the above command.

I have to find a way to stop it.

The mail is then routed through smtp to NOtes!! where the user while eating his breakfast or sipping his cup of tea gets to read the report while we toil in the engine room !!!!.

Thanks again guys.

Bill Hassell
Honored Contributor

Re: How to find a user ID from a shell script

elm is really designed for interactive use although with the right options, it can be used as a batch mailer.

Here are much easiest methos in a script:

cat report_name | mailx -s"Some subject" $LOGNAME

cat report_name | mailx -s"Some subject" $(/usr/bin/id -nu)

I think of mailx as the script mailer...only 3 things are required: -s"a useful subject", the email address and stdin with the text needing to be mailed.


Bill Hassell, sysadmin
Nalin Uduwawala_1
Frequent Advisor

Re: How to find a user ID from a shell script

I have used logname as opposed to LOGNAME .

My apologies for saying $LOGNAME doesnot work.

However when I use mailx it gives a strange message to say "The flags you gave are used only when sending mail.""


/usr/bin/id not found !!!!.

Is there a central place within ux 10.2 where I can define the smtp mail addresses of the users that can be accessed by mailx or elm as aliasses ?

Else I will have to type in the lot in the script with a if/case structure.

Thanks again for all the support.

I am trying to save some trees !!!!!!!




Joseph C. Denman
Honored Contributor

Re: How to find a user ID from a shell script

try this

user=`whoami`
report=/dir/dir/dir/reportname
mailx -s "subject" ${user} < ${report}
If I had only read the instructions first??
Brian Markus
Valued Contributor

Re: How to find a user ID from a shell script

If your having problems using "mailx" you could use "mail"


echo "User is $LOGNAME" | mail root &

However mailx with the following syntax should work.

echo "User is $LOGNAME" | mailx -s 'Someone has used your script' user@mail.domain.com
When a sys-admin say's maybe, they don't mean 'yes'!
Bill Hassell
Honored Contributor

Re: How to find a user ID from a shell script

Just a note: /usr/bin/id not found?

That is the correct pathname for the id command on HP-UX 10.xx. What happens when you type id? It it reports your id information, type the command:

whence -v id

/usr/bin/id must exist (along with hundreds of other commands)


Bill Hassell, sysadmin
Arthur Pols
Advisor

Re: How to find a user ID from a shell script

Dear Nalin,

I've found out that some users (on HP-UX 10.20) are clever enough to run certain scripts as if the are another user. When using options above you can find out the user they have become.
If you use

" who -m "

you will find out the first login id.

If you have prevented you system against login as root user and anyone needs to login by there own name first, you will be able to track down the write user. To mail the outcome, just use on of the options above.

Regards.
The purpose of education is to replace an empty mind with an open one.