What I need to remember when scripting

Shell/Scripting

GNU Parallel

  seq 200 | parallel --bar ./t.sh {} '>' ajm-{}.txt

Used with some test script like this to find corner cases:

  $ cat t.sh
  init=$PWD
  cd $(mktemp -d)
  echo $1
  ./exe $1 &> stdout
  diff stdout $init/correct && echo pass

This way I can run the executable N times and then recursively grep the output directory to find any cases that passed/failed.

Bash REGEX

Use $BASH_REMATCH or ${BASH_REMATCH[@]} with the regex match operator in a test expression:

$ [[ " -c -fPIC /path/to/file.F90 " =~ [^\ ]+/file.F90 ]] && echo ${BASH_REMATCH[@]}
/path/to/file.F90

more bash regex stuff

CLI

exe=$0
usage() {
    cat <<EOD
EOD
    exit 1
}

port=6666
cmd=""
host=""
while [[ $# -ne 0 ]]; do
  case $1 in
    -p) port=$2; shift;;
    -h) host=$2; shift;;
    --) shift;cmd="$*";shift $#;;
    *) usage;;
  esac
  shift
done

Strings

global sub

  $ var="some text to work with, more text"
  $ echo ${var//text/content}
  some content to work with, more content

previous command with some replacement

  $ echo one two three one
  one two three one
  $ !!:gs/one/five
  $ echo five two three five
  five two three five

local sub

  $ echo ${var/text/content}
  some content to work with, more text

Deletes LONGEST match, be careful of ./file.txt as the ./ component will get the whole thing removed.

  $ file=log.txt
  $ echo ${file%%.*}
  log

  $ file=./log.txt
  $ echo ${file%.*}
  ./log

string casing

  x="HELLO"
  echo $x  # HELLO

tolower

  y=${x,,}
  echo $y  # hello

toupper

  z=${y^^}
  echo $z  # HELLO

Linux documentation on string manipulation

Perl

One-Liners

(Other perl one-liners)[https://learnbyexample.github.io/learn_perl_oneliners/one-liner-introduction.html]

print line number and line of matching regex

perl -ne 'printf "%8d %s", $., $_ if /pattern/'

in-place replacement of input

perl -pi.bk -E 's/, !dbg !\d+//' good.llvm
      |      |                 | input file
      |      | regex/code/replacement
      | backup file extension to use

Getopt


Misc

Print the command to be run, run the arguments in a shell, and print all the lines of stderr/stdout:

sub sh {
    my $cmd="@_";
    chomp($cmd);
    say $cmd;
    open my $fh, "$cmd|";
    print while (<$fh>);
    close $fh;
    say "ec=$?";
}