Perl: Count occurrences of substring (show)  

I needed a quick way to count the number of times a substring appears in a larger string.

$count = @{[$haystack =~ /$needle/g]};
Leave A Reply

Run a cronjob on the last day of the month (show)  

Here is a pretty genius way of running a cron job on the last day of the month. It just checks that tomorrow is the first of the month, and runs your script if it is.

0 8 28-31 * * [ "$(/bin/date +%d -d tomorrow)" = "01" ] && /your/script.sh
Leave A Reply

Pure bash method to get the IP from eth0 (show)  

I needed a pure bash way to get just the raw IP address from eth0

# Get the IP address line of eth0 from ifconfig 
line=$(ifconfig eth0 | head -n2 | tail -n1)

# Regexp to match just the IP part
reg="addr:([0-9\.]+)\s" 

# Run the regexp and print out the match
[[ $line =~ $reg ]]
echo ${BASH_REMATCH[1]}

Or the same thing on one line

line=$(ifconfig eth0 | head -n2 | tail -n1);reg="addr:([0-9\.]+)\s";[[ $line =~ $reg ]]; echo ${BASH_REMATCH[1]}

Or here is another way to do it using cut

# Cut on the spaces and find the 12th field (addr:1.2.3.4) then get the string starting at the 6th char
ifconfig eth0 | head -n2 | tail -n1 | cut -d' ' -f12 | cut -c 6-
Leave A Reply

List all bash environment variables (show)  

There are four easy ways to list all the environment variables in bash.

env
set
export
declare -x

The simple env command is probably the most commonly used.

Leave A Reply

Bash environment variables (show)  

If you want to pull an environment variable into a bash script do

foo=$ENV_FOO;

To do the same, but assign foo a default value if there is no environment variable

foo=${ENV_FOO:-my_default_value}

Note: the - preceding the default value is required.

Leave A Reply

Force a user to change their Linux password (show)  

I had to setup a new user on a Linux server and I wanted to give them a temporary password and make them change it after their first login. You can use the chage command to control when a password expires (and requires a change). Create a new user and give them a default password. Then set that user's password to expire immediately.

chage -d0 username

This will set the password for username to expire in 0 days.

Leave A Reply

Javascript trim() comparison (show)  

This is a pretty cool comparison of javascript trim() implementations.

Leave A Reply

JQuery map items into an array (show)  

I have several elements that I just want to pull the text() out of and put into an array.

var my_items = new Array();
my_items = $(".selector").map(function() { return $(this).text(); });

Or you can do it an alternate way:

var my_items = new Array();
$(".selector").each(function() { my_items.push($(this).text()); });
Leave A Reply

Remove old pidgin logs (show)  

This command will find all your Pidgin (libpurple) log files that are more than 30 days old and remove them.

find ~/.purple/logs/ -type f -ctime +30 -exec rm {} \;
Leave A Reply

Make a directory and change in to it (show)  

$* says to take all the command line arguments and put them together as one string. This is needed if you want to make a directory with spaces in the filename.

mkcd () {
  mkdir -p "$*"
  cd "$*"
}

Stolen from lifehacker.

Leave A Reply

Run a cronjob more than once a minute (show)  

I needed to run a cronjob to log some stats every 15 seconds. Cron only allows you run a command at minimum once a minute. This presents a problem, but you can cheat using sleep to get around it.

* * * * * ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl ; sleep 15 ; ~/loggingscript.pl

Leave A Reply

Sprinkler system (show)  

  • Zone 1 = 4 heads left of drive way, and 8 heads along side walk (9 minutes)
  • Zone 2 = 5 heads in front yard center and along the entry way (10 minutes)
  • Zone 3 = 5 heads in front yard near house and by front door (10 minutes)
  • Zone 4 = 4 heads in the side flower bed, and 5 head in the side yard (5 minutes)
  • Zone 5 = 6 heads in back yard by deck stairs (8 minutes)
  • Zone 6 = 7 heads in back yard center (8 minutes)
  • Zone 7 = 4 big park sprinklers in backyard corner and near garden (10 minutes)
  • Zone 8 = 4 heads in side yard with ivy, and 1 head spraying towards heat pump (4 minutes)

Total run time 64 minutes with a total of 53 sprinklers. The control unit is a Rain Bird ESP-8si.

Leave A Reply

Anatomy of an HTTP request (show)  

This is a standard normal HTTP request

GET / HTTP/1.1
User-Agent: curl/7.20.1 (x86_64-redhat-linux-gnu) libcurl/7.20.1 NSS/3.12.6.2 zlib/1.2.3 libidn/1.16 libssh2/1.2.4
Host: www.google.com
Accept: */*

HTTP/1.1 200 OK
Date: Thu, 10 Jun 2010 17:22:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=5e8e80a9e66b65a2:TM=1276190574:LM=1276190574:S=UwbV5O8HJMVqISMc; expires=Sat, 09-Jun-2012 17:22:54 GMT; path=/; domain=.google.com
Server: gws
X-XSS-Protection: 1; mode=block
Proxy-Connection: close

This is a 404 error HTTP request

GET /thisisa404.html HTTP/1.1
User-Agent: curl/7.20.1 (x86_64-redhat-linux-gnu) libcurl/7.20.1 NSS/3.12.6.2 zlib/1.2.3 libidn/1.16 libssh2/1.2.4
Host: www.google.com
Accept: */*

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Thu, 10 Jun 2010 17:23:35 GMT
Server: sffe
Content-Length: 1369
X-XSS-Protection: 1; mode=block
Proxy-Connection: Keep-Alive
Leave A Reply

How does a transistor work? (show)  

Today I was trying to figure out how a transistor works (with my Arduino) and I found a great explanation. The simple answer is that a transistor is an electronic switch. It has a voltage in (collector), a voltage out (emitter), and a control wire (base). The switch is normally open, but when you apply voltage to the control wire it completes the circuit. This allows you to supply a small amount of voltage (5v) to complete a circuit that's higher voltage (12v).

Leave A Reply

JQuery selectors return standard element type (show)  

For some legacy code I needed a standard a JS object (i.e. not JQuery). You can use JQuery to perform the selection, and then .get() to give you the raw HTML Element Object.

$("#my_selector").get(0)

Note: you have to give .get() a index to return. Even if only one element is in the return list, .get() still returns a an array.

Leave A Reply