Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Command line question

by monk2b (Pilgrim)
on Dec 03, 2000 at 10:06 UTC ( [id://44631]=perlquestion: print w/replies, xml ) Need Help??

monk2b has asked for the wisdom of the Perl Monks concerning the following question:

I have a file that contains the name of everyone that has an account on our server.I wanted to look for a particular file so I ran a quick script from the command line.
perl -pe '`ls /home/$_/bin`' file | grep filename
I am not sure why a space is placedbetween the variable and the /bin. I would appreciate any help monk2b learning perl

Replies are listed 'Best First'.
Re: Command line question
by chromatic (Archbishop) on Dec 03, 2000 at 10:30 UTC
    You're probably picking up a newline character. Use chomp to get rid of it:

    perl -pe 'chomp; print `ls /home/$_/bin`;' file | grep filename

    Any suggestions for a pure Perl one-liner, anyone? Maybe something like:

    perl -pe 'print if glob <"/home/$_/bin/filename">;' file

      I'd be more inclined to use -l in a command line so that chomps are done automatically when used with -n or -p, but a pure perl one liner could be ...

      perl -nle 'print if -e "/home/$_/bin/filename"' file

      ... if you needed to use a seperate file, but on UNIXish systems (possibly others?) you can use getpwent() to get usernames and home directories (esp. since they may not always be under /home/!) of active accounts instead of using a seperate file.

      perl -le 'while (($u,$d)=(getpwent)[0,7]) { print $u if -e "$d/bin/$ARGV[0]" }' filename

          --k.

(tye)Re: Command line question
by tye (Sage) on Dec 03, 2000 at 11:09 UTC

    Updated a bit from my original submission. (:

    perl -lne 'print for grep /filename/, </home/$_/bin/*>`

    without the "-l", you need to chomp or $_ will have a trailing newline, which would cause you problems. Thanks to kanji for mentioning that -l auto-chomps, which I hadn't been aware of.

            - tye (but my friends call me "Tye")
Re: Command line question
by AgentM (Curate) on Dec 03, 2000 at 10:35 UTC
    Running Perl from the shell will become quite complicated for more than simple scripts since the shell implements it's own filtering and scanning for things it might be interested in. You would need to toy alot with backslahes (escaping) In this case, the shell might be interested in the $ character, since it can mean alot of things in various shells. Since I don't know which shell you're using, I can't tell you. Solutions:
    • Write the non-Perl variation: find /home/*|grep filename
    • throw your script into a .pl file and run it (obvious answer)
    • throw around some backslashes (maybe even for the backticks) until it works :o) I already see one potential problem- you are not properly encapsulating your mini-script with the appropriate shell metachars.

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found