At work, was asked to count lines of code for a web development project using specific requirements — only include certain directories and files, exclude files in .git directories, don’t report blank (empty) lines or commented lines and include files which aren’t always clearly identified as belonging to a particular language. Strictly speaking, it’s not counting just LOC, but lines that aren’t blank or commented, which in this case translates to a fairly accurate LOC count.

Read the rest of this entry…

Had a disk fail in a MD RAID array and all I knew was that it’s /dev/sdh1. Needed to know the serial number to know which disk to swap, but because the disk was dead, “smartctl -i /dev/sdh” didn’t work. Another way to find out is to look in “/dev/disk/by-id/”. For example:

Read the rest of this entry…

This is actually just about renaming files using regular expressions (regex) to match files to rename. The specific use case was just so they sort better. Turned out I already had a similar little a post on the subject (rename files with file names matching regular expression). Anyway, the method below is a slight variation on the previous post because in this case I was renaming directories whose names consisted purely of digits.

Read the rest of this entry…

Sometimes, I want to repeatedly echo a single characther in the shell a certain number of times (like 72 dashes to make a line separator, for instance). But actually running echo followed by 72 dash characters is too annoying and I can never remember the proper fprint syntax to just whip it out on demand. So here’s a quick script to repeatedly print a character (or a string) an arbitrary number of times.

A lovely line separator, 72 characters wide (print dash 72 times):

ak@loon:~$ repeat - 72
------------------------------------------------------------------------

Characters that have a special meaning for the shell are best quoted (or escaped). To print round quotes 10 times:

ak@loon:~$ repeat "()" 10
()()()()()()()()()()

String example, to echo the obligatory “Hello World!” 5 times:

ak@loon:~$ repeat "hello world! " 5
hello world! hello world! hello world! hello world! hello world!

And here’s the script:

Read the rest of this entry…

This script is very similar to the ab wrapper script to test website performance except it uses iperf to test just the network throughput, it’s only value add is running a bunch of tests in a row, parsing out just the requests per second and caclulating an average across all runs. Simple script for a simple benchmark.

Example command line:

ak@loon:~$ iperf.sh 10 lark

Output:

==================================================================
 Results
==================================================================
 target host ... lark
------------------------------------------------------------------
 run 1: 	 937 Mbits/sec
 run 2: 	 937 Mbits/sec
 run 3: 	 937 Mbits/sec
 run 4: 	 937 Mbits/sec
 run 5: 	 937 Mbits/sec
 run 6: 	 936 Mbits/sec
 run 7: 	 936 Mbits/sec
 run 8: 	 937 Mbits/sec
 run 9: 	 937 Mbits/sec
 run 10: 	 937 Mbits/sec
------------------------------------------------------------------
 average ....... 936.8 Mbits/sec
 
see iperf.lark.log for details

Here’s the script:

Read the rest of this entry…

This script is basically a simple wrapper around ab (apache bench), it’s only value add is running a bunch of tests in a row, parsing out just the requests per second and caclulating an average across all runs. Simple script for a simple benchmark. The script doesn’t verify the sanity of arguments provided to it, so if you’re not sure, just use the command line in the example — 100 reqs with concurrency of 50 — good enough for a quick and dirty benchmark.

Example command line:

ak@hawk:~$ sudo ./ab.sh 10 100 50 http://giantdorks.org/

Output:

==================================================================
 Results
==================================================================
 site .......... http://giantdorks.org/
 requests ...... 100
 concurrency ... 50
------------------------------------------------------------------
 run 1: 	 4028.36 reqs/sec
 run 2: 	 4234.42 reqs/sec
 run 3: 	 4280.27 reqs/sec
 run 4: 	 4270.77 reqs/sec
 run 5: 	 4080.47 reqs/sec
 run 6: 	 4197.80 reqs/sec
 run 7: 	 2265.78 reqs/sec
 run 8: 	 4194.45 reqs/sec
 run 9: 	 3858.47 reqs/sec
 run 10: 	 4083.80 reqs/sec
------------------------------------------------------------------
 average ....... 3949.46 requests/sec
 
see ab.giantdorks.org.log for details

Here’s the script:

Read the rest of this entry…

Specific example here is that I want to insert a string inside a second (last) instance of another string, while ignoring another string, e.g. for the input example below, I want to replace every last instance of group_ or field_ on each line, but skip any that contain _type.

The problem is negation is accomplished with:

sed '/pattern to ignore/!s/pattern to replace/replace with/'

This works fine with single quotes, but need double quotes to use shell variables inside sed. With double quotes, the shell complains about the exclamation mark:

Read the rest of this entry…

Here’s a super simple shell script to check for APC cache fragmentation and send an alert email root. Depends on apc_info.php from http://code.google.com/p/munin-php-apc/.

Read the rest of this entry…

Bash can only compare integers (e.g. 0, 1, 2, etc), not floating point numbers (e.g. 0.0, 1.3, 2.99, etc). Searching the web produced all sorts of solutions that involved either too much code or required other programs like “bc”. Most servers won’t have “bc” installed, so I came up with the following simple solution that seems to work.

My use case is to check if a value is greater than zero, unfortunately the Bash built in “-gt” won’t work because “0″ in this situation can be reported as “0.00″. My solution is to simply check for presence of an integer greater than 0, regardless of location:

Read the rest of this entry…

I frequently need to rename files by moving a string that’s part of the existing file’s name to the beginning of the filename. Here’s a script that will take a string as an argument and rename files by moving the string to the start of the file name.

Read the rest of this entry…