[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is perl still the No.1 language for sysadmin?



On Mon, Apr 03, 2023 at 07:58:03PM +0200, Emanuel Berg wrote:
> David Christensen wrote:
> 
> > # echo $PATH | tr ':' '\n' | perl -MFile::Slurp -ne
> >    'chomp;@e=read_dir($_,prefix=>1); print map "$_\n",@e'|xargs
> >    file|perl -pe 's/\S+\s+//'|grep -v 'symbolic link'|perl -pe
> >    's/, dynamically linked.+//'|sort|uniq -c|sort -rn
> 
> I'm still so impressed by this, I tried to run this but it
> seems I lack the Slurp module?
> 
> Also, if it isn't too much to ask, can you put it in the form
> of a script or shell function the way you would? It doesn't
> feel right for me to "indent your code" if you follow ...

Might be cleaner just to rewrite it from scratch.  Especially since
it mixes multiple invocations of perl together with (unsafe!) xargs and
other shell commands....

Here's a bash version.  It's not fast, but at least it doesn't invoke
perl repeatedly.  (If you're going to invoke perl *at all* you should
simply rewrite the whole thing in perl, IMHO, or at worst have a short
sh script that pipes file's output to one perl invocation.)


#!/bin/bash
shopt -s extglob
IFS=: read -ra paths <<< "$PATH:"
for d in "${paths[@]}"; do
    printf '%s\0' "$d"/*
done |
    xargs -0 file |
    while read -r line; do
        [[ $line = *symbolic\ link* ]] && continue
        line=${line#*:}
        line=${line##+( )}
        line=${line%%, dynamically linked*}
        printf '%s\n' "$line"
    done |
    sort | uniq -c | sort -rn


And its output on my system:

   1485 ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)
    358 POSIX shell script, ASCII text executable
    341 Perl script text executable
     63 Bourne-Again shell script, ASCII text executable
     34 Python script, ASCII text executable
     30 ELF 64-bit LSB executable, x86-64, version 1 (SYSV)
     23 setgid ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)
     15 ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux)
     12 setuid ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)
     11 POSIX shell script, UTF-8 Unicode text executable
      8 Tcl script, ASCII text executable
      7 Python script, UTF-8 Unicode text executable
      6 ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)
      5 ASCII text
      3 Python script, ISO-8859 text executable
      3 POSIX shell script, ASCII text executable, with very long lines
      2 setuid, setgid ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)
      2 POSIX shell script, UTF-8 Unicode text executable, with very long lines
      2 ELF 32-bit LSB pie executable, Intel 80386, version 1 (SYSV)
      2 Bourne-Again shell script, UTF-8 Unicode text executable
      2 Bourne-Again shell script, ASCII text executable, with very long lines
      1 Tcl/Tk script, ASCII text executable
      1 POSIX shell script, ASCII text executable, with very long lines, with escape sequences
      1 Paul Falstad's zsh script, ASCII text executable
      1 Java source, UTF-8 Unicode text
      1 ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux)
      1 cannot open `/usr/local/games/*' (No such file or directory)
      1 a /usr/bin/env python script executable (binary data)


Reply to: