This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# From Bash Hackers - http://wiki.bash-hackers.org/howto/collapsing_functions | |
# The first time you run chatter(), the function redefines itself based on the value of verbose. Thereafter, chatter doesn't check $verbose, it simply is. | |
# Further calls to the function reflect its collapsed nature. | |
# If verbose is unset, chatter will echo nothing, with no extra effort from the developer. | |
[[ $1 = -v || $1 = --verbose ]] && verbose=1 | |
chatter() { | |
if [[ $verbose ]]; then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for loop on directories in PATH | |
for foo in ${PATH//:/ }; do | |
done | |
# use mapfile to load binaries | |
# just loads up element 0 | |
mapfile _binary <${PATH//:/\/\* } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Complex Shell Script Outline | |
---------------------------- | |
header | |
begin script | |
preamble: | |
- sanity checks | |
- create shell environ | |
- log & debug | |
error handling | |
check dependencies |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/ | |
Simple | |
######################################### | |
#!/bin/bash | |
# exit if flock fails. | |
set -e | |
# Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# closure - defined by Peter Landin in 1964 | |
# closure - a function, plus a pointer to that functions scope | |
# In programming languages, closures (also lexical closures or function closures) are a technique for | |
# implementing lexically scoped name binding in languages with first-class functions. Duh. | |
# Operationally, a closure is a record storing a function[a] together with an environment:[1] | |
# A mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) | |
# with the value or storage location to which the name was bound when the closure was created. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fast & efficient triple check: | |
# existence, directory, writable || or error and exit | |
[[ -e $DIR && -d $DIR && -w $DIR ]] || { echo >&2 "Unable to access $DIR - check directory path / permissions?" ; exit 1 ; } | |
# exists , file, readable | |
[[ -s $FILE && -d $FILE && -r $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; } | |
# exists , file, writable | |
[[ -s $FILE && -d $FILE && -w $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
needs file descriptor and something like this | |
exec 10>$temp | |
<$temp> | |
#!/bin/bash - | |
# track path & filename for tempfile | |
temp="./example.tmp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# one line command below | |
ssh -X -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no \ | |
-i /Users/veryv/.ssh/google_compute_engine -A -p 22 [email protected] -- | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# display a random number based on the number of lines in a file | |
echo $(($RANDOM%`grep -c '$' ~/.bash_history`)) | |
# replace COMMAND to find all the libboost libraries it is using | |
lsof | grep 'COMMAND.*mem.*libboost' | |
# display contents of file without comments or empty lines, also removes "in-line" comments | |
# this is an example of a reason to NOT USE IN LINE COMMENTS | |
egrep -v '#|^$' /etc/mysql/my.cnf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# diplay alphabetically sorted list of processes with open files | |
# 'file' means: regular file, directory, block special, character special, executing text reference, library, stream or network file (Internet socket, NFS file or UNIX domain socket) | |
lsof | awk '{print $1}' | sort -u | |
# display all files that apache2 has open | |
# | |
lsof -c apache2 | less |