Shortcut Script for Sprunge Pastebin
I discovered the scripting-friendly pastebin http://sprunge.us a few days ago via One Thing Well. It's extremely convenient to use, but in my laziness I've tried to make it even easier. Here is a script that will upload to sprunge and return the URL. It accepts piped data, STDIN redirection, filenames as arguments, and text strings as arguments. EDIT: while using the script, I just realized that using either form of STDIN input (pipe or redirection) will treat tabs as spaces. While this will occasionally break code, it will almost always make it less legible. So, if you need to preserve tab whitespace, I suggest using a filename as an argument to the script (e.g. sprunge myscript.txt). (Click "show source")
Using ls to Show Directory Size – Updated & Explained
Note - I've written about this before (here). In my opinion, this is a vast improvement over that earlier version.
This title of this post is not entirely accurate. The post is actually about a script that will list files and directories in the same format as ls -l (the "long listing" format of the ls command), except it will correctly report the size of directories (and all of the files within them). Technically, it is actually in the format of ls -lhL.
This is functionally very different than the output of ls. Typically ls lists the size of directories not including their contents. This produces a very small number, which is the amount of disk space the directory's meta-data takes up (i.e. the names of files within the directory). Here is an example:
ls -lhL
total 116K drwxr-x--- 1 james users 44K 2010-04-22 17:21 movies drwxr-x--- 1 james users 48K 2010-04-03 22:22 music drwxr-x--- 1 james users 0 2009-12-11 16:20 photos drwxr-x--- 1 james users 4.0K 2009-12-13 22:36 data drwxr-x--- 1 james users 20K 2010-04-27 00:56 tv
MPC Script – Quickly Find and Play by Album
This is a simple script for MPD & MPC users. It allows you to quickly queue and play an album. I name the script "album" and put it in ~/bin (which is included in my $PATH).
#!/bin/bash ## Shortcut to search, add, and play an album (after clearing current playlist). ## Use -a (for "add") to add the album to the playlist without clearing current playlist. if [ $1 = -a ] then shift mpc search album "$*" | mpc add ; mpc play exit fi mpc clear mpc search album "$*" | mpc add ; mpc play
BASH – Get Size of Directory in "ls -l" Output

(The above photo is "flowring cabbage regular", by abundantlyabove29 (C) via Flickr).
UPDATE: I've written a newer and much better version of this script. You can find it at: http://www.shellperson.net/show-directory-size-with-ls/
I found a function like this a long time ago in some forum, but I remember it not working with filenames that include spaces. There's almost certainly a better and more-efficient way of doing this, but here's draft one.
This will output the same data as "ls -lh", in almost the exact same format, except directories will show the size of the sum of their contents (du -sh) instead of the 4.0K of space the directory "file" takes up. The only noticeable difference in format is that there is a slightly larger gap between the size and the date columns.
#!/bin/bash ## mimic "ls -l" except display size of sum of directory's contents for x in *; do echo -e "$(ls -lL | grep -w "$x" | cut -d ' ' -f 1-4) $(du -sh "$x" | cut -d ' ' -f 1)" "\t" "$(ls -lL | grep -w "$x" | cut -d ' ' -f 5-20 | sed s/^\ *// | cut -d ' ' -f 2-20)"; done
It's hard to enter code in a presentable way in wordpress - and this code doesn't appear perfectly. Note that the multiple-space gap that delimits (-d) the second "cut" command is a tab. You can add a tab at the command line by using CTRL+v followed by TAB. The code does work if you use the copy-to-clipboard button on the code block.
I saved this as "lsd", made it executable, and put it in a directory in my $PATH (~/bin).
Easily Change Text Color with BASH

Here's a script to easily add color to other BASH scripts. The only trick to it is that it uses a character that I don't know how to put on the web.
NB - If you select the "view plain" link at the top of the container surrounding the script, it will open a new link with the plain text of the script. You should use this plain text. Not only does it preserve the special character the script uses to change colors, it also preserves other characters that WordPress changes (note line 5, between "cat" and "EOF"). If you don't use the "view plain" link and try to cut, but instead paste the script directly from this page, it won't work. Also, if you need to create the special character I use to make it display different colors, you can make it in gnome-terminal by pressing "ctrl+v" followed by "ctrl+[".
#!/bin/bash
## Specify color to write in using arguments
function --help {
cat << EOF
ERROR: $0 requires a color argument.
USAGE: Changes the color of the text piped into it.
These color arguments are availabe:
ARGUMENT SHORTCUT
white ------ w
red ------ r
green ------ g
yellow ------ y
blue ------ b
purple ------ p
teal ------ t
bold ------ bb
The "bold" argument will modify any color.
Use a max of 2 arguments (one color and bold).
EOF
}
function bold {
# make the color bold
BOLD=1\;
}
function white {
COLOR=1
}
function red {
COLOR=31
}
function green {
COLOR=32
}
function yellow {
COLOR=33
}
function blue {
COLOR=34
}
function purple {
COLOR=35
}
function teal {
COLOR=36
}
## shortcuts
function bb {
bold
}
function w {
white
}
function r {
red
}
function g {
green
}
function y {
yellow
}
function b {
blue
}
function p {
purple
}
function t {
teal
}
## Execution
if [ "$#" = 0 ]
then
--help
fi
while (($#));
do
$1
shift
done
echo -n "["$BOLD""$COLOR"m"
cat
echo -n "[0m"
Here's a link to an example of what it looks like in action. Note that the input must be piped into the script.
Quickly Check Gmail with BASH

A very simple script to check if I have any new gmail:
#!/bin/bash ## Quickly checks if I have new gmail echo -e "Checking for new messages... \c" atomlines=`wget -T 3 -t 1 -q --secure-protocol=TLSv1 \ --no-check-certificate \ --user=USERNAME --password=PASSWORD \ https://mail.google.com/mail/feed/atom -O - \ | wc -l` echo -e "\r\c" [ $atomlines -gt "8" ] \ && echo -e " You have new gmail. \c" \ || echo -e " No new gmail. \c"
Just replace USERNAME and PASSWORD with your information (caution - your password is stored in plain text). I use colors on mine, but I'm not sure I can correctly post the requisite special character in WordPress. Here's what it looks like in gedit.

I can make that special character in gnome-terminal by pressing CTRL+v and then CTRL+[
There's probably an easier way to use colors, I just don't know it.
In case it's not clear from the script, this just downloads the atom feed (an XML file) and checks if it's more than eight lines long. If it is, then you have unread mail. (At least on my account, when my inbox has no unread items, the atom feed is eight lines long). The number may need to be adjusted if gmail changes the format of their feeds.
ssh Shortcuts in Bash

(The above photo is "Good and Evil", by Abbie F (C) via Flickr).
This is the bash script I use to ssh into my fileserver (without having to type the whole IP address, etc). It works for simply logging in with ssh, or for sending a command only. If you want to send a series of commands, you'll need to separate each command with \; or else it (a lone semi-colon) will be interpreted as an instruction to run the command on the local machine after the first command is sent via ssh (you're 'escaping' the semi-colon, so it gets sent through ssh as a regular character, instead of its typical role of separating commands). This method works for me, but I'd love to learn a better way if there is one.
I name the script something short and put it in my path. I also use it in conjunction with password-less login. I know it's not such a big deal to write out:
ssh user@ipaddress "command -arguments"
but it saves time if you're constantly ssh'ing to that machine.
Here it is in a more-legible format, that shouldn't be copied and pasted (because of the way WordPress changes quotation marks):
#!/bin/bash
SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo`
ssh administrator@192.168.0.202 "$SSH_ARGUMENTS"
And here it is in a way that's easier to copy and paste (but harder to read, in my opinion):
#!/bin/bash SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo` ssh administrator@192.168.0.202 "$SSH_ARGUMENTS"
Also, here's a slight variation that's useful if you have multiple machines with a sequential naming scheme, where you can specify the machine number:
#!/bin/bash ## for example: comp 4 shutdown -r now ## note: in my setup, the username correlates with the computer's number WORKSTATION_NUMBER=$1 shift SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo` ssh $WORKSTATION_NUMBER@192.168.0.10$WORKSTATION_NUMBER "$SSH_ARGUMENTS"
Edit: I suppose anyone who knows anything about BASH would know that the entire SSH_ARGUMENTS line could be removed and the variable $SSH_ARGUMENTS replaced with $*
...every day learning a little bit more.