Computing.Net > Forums > Solaris > get the home directory after su

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

get the home directory after su

Reply to Message Icon

Name: lei_michelle
Date: September 13, 2008 at 06:16:18 Pacific
OS: Solaris
CPU/Ram: N/A
Product: Sun
Comment:

Hi All,

After using the "su test1" (but not "su - test1") command to switch to user test1, is there a way to get the home directory of test1?

I understand if "su - test1" is used then $HOME will be changed to the home directory of test1.

Thanks in advance!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 13, 2008 at 23:51:59 Pacific
Reply:

You can't get the home directory without doing some shell scripting with the unix id command. The later versions of id command supports the -un option which provides the effective user id. However, my version of the id command doesn't support -un.

Executing the id command with no options displays this as an output:

1003(nails) gid=1000(people)

The following script parses the effective user id and then searchs the first field of the password file, /etc/passwd, and when a match is found, returns the sixth field:


#!/bin/ksh

# retrieve the user id from id command
uid=$(id|cut -d \( -f 2|cut -d \) -f 1)

while IFS=":" read puid f2 f3 f4 f5 homedir f7
do
if [[ "$uid" = "$puid" ]]
then # display the home directory
echo "$homedir"
break
fi
done < /etc/passwd



0

Response Number 2
Name: jefro
Date: September 16, 2008 at 15:57:37 Pacific
Reply:

To get to home (home is a relative directory by the way) you use cd.

"By default, su does not change the current directory. It sets the environment variables `HOME' and `SHELL' from the password entry for USER, and if USER is not the super-user, sets `USER' and `LOGNAME' to USER. "


Could be way wrong. See man page for cd on your shell.

See also pwd.

"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10


0

Response Number 3
Name: nails
Date: September 16, 2008 at 18:16:45 Pacific
Reply:

jefro:

Sorry I must disagree wtih you. At least in Solaris su <user id> doesn't change the user environment - including environmental variables. To change the environment, you must execute su - <user id>

According to the su MAN page:

If the first argument to su is a dash (-), the environment will be changed to what would be expected if the user actually logged in as the specified user.


0

Response Number 4
Name: jefro
Date: September 17, 2008 at 18:56:25 Pacific
Reply:

OK,

"The following statements are true if the login shell is /usr/bin/sh or an empty string (which defaults to /usr/bin/sh) in the specific user's password file entry. If the first argument to su is a dash (-), the environment will be changed to what would be expected if the user actually logged in as the specified user. Otherwise, the environment is passed along, with the exception of $PATH, which is controlled by PATH and SUPATH in /etc/default/su."

"Best Practices", Event viewer, host file, perfmon, antivirus, anti-spyware, Live CD's, backups, are in my top 10


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: get the home directory after su

Home directories www.computing.net/answers/solaris/home-directories/1938.html

cant hear online songs www.computing.net/answers/solaris/cant-hear-online-songs/4109.html

No directory! whith su command www.computing.net/answers/solaris/no-directory-whith-su-command/3121.html