Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Get home dir from shell

by vit (Pilgrim)
on Apr 03, 2013 at 12:55 UTC ( #1026850=perlquestion: print w/ replies, xml ) Need Help??
vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I need to get a home dir via shell inside Perl/CGI. In this case %ENV will not give it since it shows CGI parameters.
I also cannot do
`echo $HOME`
since it considers $HOME as a variable.
Any advice is appreciated.

Comment on Get home dir from shell
Download Code
Re: Get home dir from shell
by Corion (Pope) on Apr 03, 2013 at 12:57 UTC

    If $ENV{HOME} is not set, it won't be set in the shell either.

    Most likely, your login environment is not set up because your script is running as the CGI user, or if it is running as your user, the login / .profile scripts have not been run.

    See Get default login environment and the replies for how to set up the environment from a script.

Re: Get home dir from shell
by CountOrlok (Friar) on Apr 03, 2013 at 13:02 UTC
    If your CGI script is being run from a web server like apache, you likely won't see a home directory. The script would be running as user apache, which does not have a home directory. Whose home directory are you expecting?
Re: Get home dir from shell
by kennethk (Monsignor) on Apr 03, 2013 at 14:29 UTC
    since it considers $HOME as a variable
    Which is why you escape it: perl -e 'print `echo \$HOME`'

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      It will print $HOME
        moo@cow>perl -e 'print `echo \$HOME`' /home/moo
        ~$ perl -e 'print `echo $HOME`' ~$ perl -e 'print `echo \$HOME`' /home/kennethk ~$ perl -e 'print `echo \\$HOME`' \ ~$ perl -e 'print `echo \\\$HOME`' $HOME ~$ perl -e '$q = chr 39; print `echo $q\$HOME$q`' $HOME ~$ perl -e '$loc = q{\$HOME}; print `echo $loc`' $HOME
        Try outputting your command to make sure it's what you think it is.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Get home dir from shell
by aitap (Deacon) on Apr 03, 2013 at 17:29 UTC
    Don't rely on shell, use system functions instead: my $homedir = (getpwuid($<))[7];. $< is the real UID of the user who's running the script, and 7'th field of getpwuid($uid) is the home directory path. See also: getpwent.
    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026850]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-25 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls