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
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
# 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
#!/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
# # Includes perfect date perfected. The uses go beyond backups, beyond space and time ... ok not time. | |
# # Creates a directory structure of ./YYYY/mm/dd/HH/MM/SS | |
mkdir -p "$(date '+%Y/%m/%d/%H/%M/%S')" | |
# enDIRing elegant epoch | |
# # -p is optional - unless co-processing! | |
# # the " and ' are also optional, but "'safer'" | |
# # for scripting, do set the epoch in a variable, reference that variable - write once read many and yer done! | |
mkdir -p "$(date '+%s')" |
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
# captures packets on bond0 interface containing "REGISTER" for 60 seconds | |
timeout 60 tshark -i bond0 -R sip.CSeq.method=="REGISTER" | |
# more tuning |
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
# removes stuff I didn't want to read | |
apache2ctl status | grep -v '\.\.' | grep -v '^\"' | grep -v 'Scoreboard' | |
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
# original - this is all bad logins - ever! | |
lastb -i | grep -Po '\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }' | |
# early prototype - this works - but returns words | |
lastb -i | awk '{ print $3 }' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }' | |
# ah ha! this filters out the blank line and date/time stamp that was causing the words to appear | |
lastb -i | egrep -v '^$|btmp' | awk '{ print $3 }' | sort | uniq -c | awk '{ if ($1 >= 5) print $2; }' |
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
service mysql stop | |
mysqld_safe --skip-grant-tables > /dev/null 2>&1 & | |
# wait for previous command to complete | |
mysql -u root -e "use mysql; update user set password=PASSWORD('NEW-PASSWORD') where user='root'; flush privileges;" | |
service mysql start |
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
# The first method opens file.txt, writes one line, then closes file.txt. Then repeats those three steps 100 times | |
# The second method opens file.txt, writes 100 lines and closes file.txt | |
#!/usr/bin/sh | |
# typical use of a while loop - | |
# while $count is less than or equal to 100. | |
# append the value of $count to file.txt | |
count=1 |