home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!xoanon.dev.cdx.mot.com!mcook
- From: mcook@xoanon.dev.cdx.mot.com (Michael Cook)
- Subject: Re: Filename expansion ("~user")
- Message-ID: <mcook.714424590@xoanon.dev.cdx.mot.com>
- Keywords: ~user filename expansion
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: xoanon.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1992Aug20.153727.167@cbnews.cb.att.com> <1992Aug21.183318.11200@merlin.dev.cdx.mot.com>
- Distribution: usa
- Date: Fri, 21 Aug 1992 19:16:30 GMT
- Lines: 53
-
- lezz@merlin.dev.cdx.mot.com (Lezz Giles) writes:
-
- >In article <1992Aug20.153727.167@cbnews.cb.att.com>, daleske@cbnews.cb.att.com (John D. Daleske) writes:
- >|>I'm relatively new to Perl and love it. I've read the recent notes in this
- >|>notes group and have not found how do one capability that I am trying to
- >|>use in a Perl script which is the ~user filename expansion. Following is a test
- >|>script:
- >|>
- >|>$tst1="~daleske";
- >|>die "${tst1} not a directory" if !(-d ${tst1});
- >|>opendir(DH, ${tst1});
- >|>@allfiles = readdir(DH);
- >|>closedir(DH);
- >|>print "@allfiles\n";
- >|>
- >|>If I set $tst1 to the full pathname it works fine. I'd like to use the common
- >|>facility of ~user to avoid needing to know the full path name. (User home
- >|>directories do get moved around.)
-
- >You'll certainly get the standard response about "~ is only a shell construct
- >and so you'll need to use a shell to expand it", which is valid enough and
- >leads to various solutions. However, some time ago I was working in an
- >environment where we wanted to write /bin/sh scripts which could access
- >people's home directories - so we had a semi-standard tool called 'logdir'
- >which takes a username and returns the home directory, e.g.
-
- >$ logdir lezz
- >/usr/u/a1/lezz
-
- >Once you've written this tool, you can add lines like these to your perl prog:
-
- >($homedir = `logdir lezz`) || die "Can't find home dir for lezz\n";
-
- >$homebin = `logdir lezz` . "/bin";
-
- >The source for logdir is:
-
- [C source code deleted.]
-
- How about this:
-
- #! /bin/ksh -p
- for i ;do eval echo \~$i ;done
-
- Or, the Perl code (er, this is comp.lang.perl, isn't it :-) would be:
-
- sub logdir
- {
- getpwnam($_[0])[7];
- }
- ($homedir = &logdir(lezz)) || die "Can't find home dir for lezz\n";
-
- Michael.
-