Operating System - HP-UX
1830898 Members
3250 Online
110017 Solutions
New Discussion

Re: ksh script to set home directory

 
SOLVED
Go to solution
Nigel McGuinness_1
Occasional Advisor

ksh script to set home directory

I have a script called cdh that works fine on Solaris but not on HP. The script cdh contains:
==============================================
WHOAMI=`who am i|cut -d" " -f1`
echo $WHOAMI
cd ~$WHOAMI
pwd

output from Solaris:
========================
c950849
/home/oraadm/c950849

output on HP
===============================
c950849
cdh[3]: ~c950849: not found
/ora/admin

Could someone please advise how to achieve the Sun result on HP. Thankyou.
8 REPLIES 8
harry d brown jr
Honored Contributor

Re: ksh script to set home directory


What shell you using for login?

What is the login directory?

live free or die
harry
Live Free or Die
Nigel McGuinness_1
Occasional Advisor

Re: ksh script to set home directory

Harry,
What shell you using for login?
/usr/bin/ksh
What is the login directory?
/ora/admin

More info:

I first login as c950849 to directory
/home/oraadm/c950849
I then su to oracle to directory
/ora/admin
I then run cdh and get
c950849
cdh[3]: ~c950849: not found
/ora/admin

Thanks Nigel.
Deepak Extross
Honored Contributor

Re: ksh script to set home directory

Hi,
Does "grep c950849 /etc/passwd" give you any output?
Nigel McGuinness_1
Occasional Advisor

Re: ksh script to set home directory

Deepak,

Please see output below:

>grep c950489 /etc/passwd
c950849:xVgnQ9xydh5/.:19622:200:Nigel McGuiness,,,:/home/c950849:/usr/bin/ksh

Please note that this works:

>cd ~c950849

Just that when I use the variable it doesn't.
eg.

>WHOAMI=`who am i|cut -d" " -f1`
>cd ~$WHOAMI
ksh: ~c950849: not found
Ollie R
Respected Contributor
Solution

Re: ksh script to set home directory

Hi Nigel,

Tilde substitution doesn't work with parameter substitution - the "~$" is taken as 2 seperate words and so that is why the output you are getting is "~c950849".

The way to get the substitution in is to use "eval" as follows:

WHOAMI=`who am i|cut -d" " -f1`
echo $WHOAMI
eval cd ~$WHOAMI
pwd

One more point - the "who am i" will return the original real user login so if you've logged in as "root" and done "su - c960849" you will still get the properties of "root".

If this isn't what you want then I would substitute in "whoami" which returns the current user login. But then again, if this is the case you can simply get to the home directory by using "cd" by itself!

I don't know exactly what you're trying to achieve but in any case I hope this has been of some help,

Ollie.
To err is human but to not award points is unforgivable
Gregory Fruth
Esteemed Contributor

Re: ksh script to set home directory

If you're trying to find out what your own home
directory is, just use the $HOME env variable.

If you're trying to get someone else's home
directory based on their user id, try something
like:

grep "^$username:" /etc/passwd | cut -d: -f6

or, if you're on NIS:

ypcat passwd | grep "^$username:" | cut -d: -f6
Tom Danzig
Honored Contributor

Re: ksh script to set home directory

Try replacing

cd ~$WHOAMI

with

cd ~/$WHOAMI
Nigel McGuinness_1
Occasional Advisor

Re: ksh script to set home directory

Ollie,
Perfect. Thanks.

Gregory,
Works well too. Ollie's just a bit more elegant!

Tom,
WHOAMI=`who am i|cut -d" " -f1`
echo $WHOAMI
cd ~/$WHOAMI
pwd

gives me
c950849
cdh[3]: /ora/admin/c950849: not found
/ora/admin

This is because the home directory for c950849
is actually /home/c950849 whereas this is trying to take me to oracle's home directory and subdirectory c950849.