> See 'return codes' from Commands Inside a Chain of Pipes (pipeline)

It can be hard to tell if everything went okay inside a chain of pipes since $? will only report the return code from the final command in the chain. Using a different variable ($PIPESTATUS) the return codes of all the commands chained together can be found. #!/bin/bash # as long…

> Track BASH script command failures or abort after N failures

Using the trap built-in signals received by a scripts (e.g. SIGINT). Using this functionality a script can also take actions based on errors. This can allow some more nuanced handling as opposed to set -e. #!/bin/bash ErrorThreshold=5 Cumulative_RC=0 ErrorCount=0 TrackErrorTrap() { ((Cumulative_RC += $?)) ((ErrorCount++)) if…

> Loop through Multiple Arrays in Perl at the Same Time

Say you have more than one array that you want to loop through at the same time, pulling out the same row index from each per cycle through the loop. One way would need to loop over each index number: for my $I (0 .. $#arrayA) { my $firstVal = $arrayA[$I]; my…

> A Couple Ways to Return a Random Number on the Command Line

Lets explore a couple ways to get random numbers on the command line. Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin. John von Neumann $RANDOM Bash Internal $RANDOM is an internal Bash function (not a constant) that returns a…

> Simple Way to Get your External IP on the Command Line

This is an easy way to see what IP a website is seeing you come from: curl httpbin.org/ip { "origin": "52.11.93.181" } curl icanhazip.com 52.11.93.181 Lots of other options, but these are some of the shortest I've seen.…

> How a BASH Script Can Find Its Own Location

Most foolproof method I'm aware of is the following: SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd && echo x)" SCRIPTDIR="${SCRIPTDIR%x}" or if you're not worried about newlines at the end of your directory names: SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" How does…

> Exploring the BASH_SOURCE Array

$BASH_SOURCE is the call stack of scripts with the top of the stack being the currently executing sourced script, sourcing another script from the current one will push it on top of the stack. To get an idea of how this works you can set up a directory tree…

> What does "use strict;" in Perl do?

What does use strict; in Perl really get you? By default three things, which can be specified separately or turned off separately: # All on by default when using use strict; use strict "vars"; use strict "subs"; use strict "refs"; # One or more can be turned off if need be no…

> Print out Line Numbers in a BASH Script

The environment variable $LINENO contains the current line number of the running BASH shell. On the command line the value will be the number of command line prompts you've used in that shell, but in a script it's the currently executing line -- and it really is line number, if…