dspinellis / dgsh

Shell supporting pipelines to and from multiple processes
http://www.spinellis.gr/sw/dgsh/
Other
323 stars 22 forks source link

different example #110

Closed ghost closed 5 years ago

ghost commented 5 years ago

visiting this page:

https://spinellis.gr/sw/dgsh

i came across this example:

FREE=$(df -h . | awk '!/Use%/{print $4}')
ls -n |
tee |
{{
        # Reorder fields in DIR-like way
        awk '!/^total/ {print $6, $7, $8, $1, sprintf("%8d", $5), $9}'

        # Count number of files
        wc -l | tr -d \\n

        # Print label for number of files
        echo -n ' File(s) '

        # Tally number of bytes
        awk '{s += $5} END {printf("%d bytes\n", s)}'

        # Count number of directories
        grep -c '^d' | tr -d \\n

        # Print label for number of dirs and calculate free bytes
        echo " Dir(s) $FREE bytes free"
}} |
cat

this example is confusing, as you can rewrite it with a single POSIX AWK script:

#!/usr/bin/awk -f
BEGIN {
   # calculate free bytes
   while ("df -h" | getline) {
      if (index($0, "Use%") == 0) {
         free_byte = $4
      }
   }
   while ("ls -n" | getline) {
      if (index($0, "total") == 1) {
         continue
      }
      # Reorder fields in DIR-like way
      printf "%s %s %s %s %8d %s\n", $6, $7, $8, $1, $5, $9
      # Count number of files
      num_file += 1
      # Tally number of bytes
      num_byte += $5
      # Count number of directories
      if (index($0, "d") == 1) {
         num_dir += 1
      }
   }
   printf "bytes free: %s\n", free_byte
   printf "bytes: %s\n", num_byte
   printf "dirs: %s\n", num_dir
   printf "files: %s\n", num_file
}

surely an entirely new shell doesnt need to be written in this case?

dspinellis commented 5 years ago

This specific toy example is intended to give you a feeling of the language. Look at the other examples to find real utility in terms of expressiveness, conciseness, and performance.

ghost commented 5 years ago

@dspinellis that may be true, but its hard to take this project seriously without a real world example, or at least one that would be more difficult with current software.

dspinellis commented 5 years ago

@cup Am I missing something? There are 18 other examples on the web page.

ghost commented 5 years ago

@dspinellis this is a good example:

#!/usr/bin/env dgsh
tee |
{{
    printf 'File type:\t'
    file -

    printf 'Original size:\t'
    wc -c

    printf 'xz:\t\t'
    xz -c | wc -c

    printf 'bzip2:\t\t'
    bzip2 -c | wc -c

    printf 'gzip:\t\t'
    gzip -c | wc -c
}} |
cat

but thats not the first example - the first example is the one i put in the original post. as a new user it can be distracting if the first example someone encounters can be more easily solved with current tools - i would recommend removing the current first example and replacing with the one in this comment

dspinellis commented 5 years ago

Good point!