Bash 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
Bash 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
Perl 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
CLI 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
CLI 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.
CLI 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
CLI 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
CLI Use exec to redirect all BASH script output to a file, syslog, or other command One of the neat tricks of exec is that you can use it to redirect all output from a shell. If you take advantage of this inside of a script you can easily send all your output to a file, or even syslog. #!/bin/bash # redirect STDOUT to a file
Perl 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
Bash 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
Perl Return Multiple Values from a Hash in any Order using Slices Usually when you access a hash you get a single scalar value back, but it's also possible to get an array of values back - a slice of the hash. To do this all you have to do is pass in an array of keys, rather then a single key,
Perl Return a Random Hash inside a Hash of Hashes or a Random Hash Key The following will return a random key from the top level hash (in this case foo or bar) which is then used to grab a value from the second level hash: my %hash = ( foo => { x => "0", y => "1" }, bar => { x => "