1. 24

I love this idea - “What is in your bin directory?” was one of my first Lobsters posts. https://lobste.rs/s/f4konf/ask_lobsters_what_s_in_your_personal_bin_directory

  1.  

  2. 3

    sponge: soak up standard input and write to a file

    Ermmm, how it is different from >?

    1. 4

      Try running the example on the page using > instead of sponge.

      Edit: don’t do it on /etc/passwd.

      1. 1

        I honestly see no difference…

        1. 7

          The behavior you see depends on the shell. On bash 3.2 for an example:

          $ cat foo
          foo
          bar
          quux
          $ cat foo | tr o O >foo
          $ cat foo
          $
          
      2. 3

        One thing that is different is that it’s explicitly not a shell construct, independent of bash, etc. Which means you can then do command | sed | sudo sponge /etc/file. Trying to redirect the output using > would fail with a permission denied (unless your user could actually write to /etc, like root).

        1. 2

          You can hijack tee for this purpose (pipeline | sudo tee /…/file > /dev/null), but buffering stdin before writing is extremely useful. Is sponge smart enough to buffer to another file and mv it into place at the end, I wonder?

          1. 2

            Yes. See the sponge manpage.

      3. 3

        I find myself often needing a realpath (1) in a shell script when all I get from the standard is a realpath (3), so I carry this around with me.

        1. 2

          are there are good,quick,small scripts to create summary statistics and histograms for numbers? numutils is mentioned in the description of moreutils, but it doesnt appear to provide those.

          atchange is a handy script to trigger a command whenever a file gets updated. I use it to automate the edit-save-run loop when writing code. It’s not technically in my /bin directory, it’s in .bash_aliases as a shell script, which I wrote after reading about the original perl script described here

          # atchange $1 $2
          # runs command $2 whenever file $1 is modified
          function atchange(){
            if [ "$#" -lt 2 ]
            then
              echo "Insufficient arguments. Requires at least 2"
              echo "Usage: atchange [filename] [command(s)]"
            else
              FILE="$1"
              shift
              COMMAND="$@"
              oldmodtime=""
              while [ -f "$FILE" ] || ! echo "File $FILE not found" 
              do 
                #inotifywait -qq -e modify "$FILE"; # disabled to remove dependency. use polling instead
                newmodtime=`stat -c %y "$FILE"`
                if [ -z "$oldmodtime" ]; then
                  oldmodtime="$newmodtime"
                fi
                if [ "$newmodtime" != "$oldmodtime" ]; then
                  echo "*** atchange: $FILE change detected ($newmodtime) ***"
                  sleep 1 # may not be necessary
                  $COMMAND
                  oldmodtime="$newmodtime"
                fi
                sleep 2 # polling interval
              done
            fi
          }
          
          1. 2

            Perhaps spark.