Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

expanded "ls"

by blahblahblah (Priest)
on Aug 10, 2005 at 03:14 UTC ( #482505=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info /msg me
Description: wrapper for "ls" that supplies default args and paged output

I got tired of typing "ls -halt ... | less -E". Now I just run this script, which I've named "sl".

Usage:

sl sl -R sl -R *.pl *.txt
#!/usr/local/bin/perl

# Quote each argument.
# When you run "sl *a*", the shell interprets the "*a*" and passes a l
+ist of files
# in @ARGV.  I noticed that things broke on a file named "#blah#" (lik
+e emacs creates),
# and quoting each argument fixed that.
map {$_ = "\"$_\""} @ARGV;

my $hasLess = 1 unless ($^O eq 'aix');  # change as needed
# (-E Causes less to automatically exit  the  first  time  it reaches 
+end-of-file.)
my $pager = $hasLess ? 'less -E' : 'more';

# put the command together
$command = "ls -halt @ARGV | $pager";

# debugging
# print "$command\n";

# You probably don't need this anymore but I think it's
# harmless.  (see $| in "perldoc -f exec".)
$| = 1;

# run the command and exit
exec $command;
Replies are listed 'Best First'.
Re: expanded "ls"
by chanio (Priest) on Aug 10, 2005 at 03:37 UTC
Re: expanded "ls"
by nothingmuch (Priest) on Aug 11, 2005 at 10:27 UTC
    exec can be given arrays, so you don't need to quote arguments.

    to get the pipe open behavior you can pipeopen ls, and then exec the pager:

    open STDIN, "-|", ...; exec @pager;
    FYI, that is how the shells do it: they create the pipeline descriptors, fork off children for the processes, dup the filedescriptors to stdin/stdout as appropriate, and then exec the subproccesses.

    If you insist on quoting and concatenating there are cpan modules to do this more safely.

    map { } in void context is silly:

    $_ = qq{"$_"} for @ARGV;
    To check if you have less, don't make assumptions based on the platforms. Instead:
    use File::Which; my $pager = which("less") || which("more") || die "no pager";

    Lastly, all of this could be completely superceded with a shell alias =)

    -nuffin
    zz zZ Z Z #!perl
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2020-05-31 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?