Tag: Bourne
What domains are awaiting mail from my server?
by someguy on Jun.10, 2008, under Uncategorized
Here’s a less than elegant solution to get a summary of the number of emails per domain that have mail sitting in your postfix queue.
#!/bin/sh
postqueue -p|egrep '^([[:space:]])+([[:alnum:]]|.|-|_)+@([[:alnum:]]|-)+.([[:alnum:]]+|.|-)*([[:alpha:]]){2,4}$’|awk ‘{ print $1 }’
|awk -F@ ‘{ print $2 }’|sort|uniq -i -c
| awk ‘BEGIN {sum=0} {sum += $1} {print $0} END {printf(”—————————n “sum” Total E-mails that are properly formattedn”)}’
Thank you, awk
Flickr-ify your Gnome Screen Saver
by someguy on Oct.28, 2007, under Uncategorized
I’ve been reading about the estarling, a digital photo frame that will sit on a wireless network and grab photos from an RSS feed, on say, Flickr, or Picasaweb. What a fantastic idea this is — and really it’s the product I’ve been waiting to come to market for a long time if its features work as promised. Imagine: stick it in the home of your grandparents, parents, etc. - and _you_ can send the photos to their frame - keeping them up to date with all the latest photos from the lives of their family and friends.
To go along with this, I wanted a simple screen saver for my new install of Ubuntu Feisty-Fawn that did the same thing — grabbed latest images from an RSS feed on the web and then serve them when the screen was locked, or the system idle for more than a few minutes. It seemed like every Mac in my office has been taunting me with a feature like this, but maybe all those photo slideshows I’ve been seeing are from local images.
So, this is nothing short of a hack, but if you want this functionality on your desktop, maybe you’ll think it’s worth it like I did. The end result is Gnome Pictures Screen Saver with a modified version of bashpodder that grabs the latest photos from any feed(s) I specify to become the content for my screensaver.
The script takes a config file which consists of the URL to the feed you want to subscribe to and the xsl file which pulls out the URLs to the photos themselves from the feed. Currently RSS feeds/xsl transformations from Gallery, Picasaweb, and Flickr have been tested. When it runs, it checks the feed against files previously downloaded and only downloads the new stuff. It could be run from cron or perhaps when you log in. I suggest running it by hand the first time to make sure it is doing what you expect.
Package available in this thread:
Prerequisite pkg: libwww-perl (I used GET instead of wget - long story)
To install:
$tar -xzvf rss_photos.tar.gz
Edit rss_photos.sh and change the values of CONF_FILE and LOG_FILE to a permanent location. Create the CONF_FILE you just specified.
# Lines in CONF_FILE should be of the pipe-delimited format:
#
# e.g.
# http://api.flickr.com/services/feeds/photos_public .gne?id=YOURIDHERE&lang=en-us&format=atom|parse_flickr_enclosure.xsl
Obviously, you will need to enable the “Pictures Folder” screen saver in Gnome for this to work - the script simply grabs photos for the screen saver.
Variable Variables: Double Interpolation in Bash
by someguy on Sep.12, 2006, under Uncategorized
Ok, so most of you are wondering why I’d be even bothering to do double interpolation in Bash rather than a real language. Well, I already had some scripts in progress and I didn’t want to start from scratch. What is double interpolation and why would I bother?
In my case, I knew that I was going to have a dynamic list (an array), and that for every array member, there would be a number of other things I would need to know about it (yes, like a multi-dimensional array).
Perhaps we want to do some simple monitoring of an unknown amount of servers in each data center and for each server we need to know how to reach it -- the IP address, and the way we want to reach it to test (i.e.http/ping/etc.). So ideally, we need to be able to keep a separate config file which just holds X number of shell variables with server configurations in it -- but that the contents of that config file might be wildly different (number of servers, server specs, etc.) between environments and keep the script simple.
So, in one environment we want to monitor the servers, moe, larry, and curly (woop, woop, woop) :
We write a config file that has one array for the server names, then for each of those we define variables to hold the IP and check-type.
The config file, servers.cfg, looks like:
SERVERS="moe larry curly"
moe_IP=10.10.10.10
moe_CHECKTYPE=http
larry_IP=10.10.10.11
larry_CHECKTYPE=ping
curly_IP=10.10.10.12
curly_CHECKTYPE=ping
Then we write a script which just loops through the given servers and does the check based on whatever check-type and IP we’ve configured.
#!/bin/bash
# Source our config file
. servers.cfg
# Loop through the servers and test them!
for server in $SERVERS;
do
IP=$(eval echo $`echo ${server}`_IP);
CHECKTYPE=$(eval echo $`echo ${server}`_CHECKTYPE);
if [ "$CHECKTYPE" = "http" ];
then
# Do dome kind of http check - this is oversimplified for this example
wget $IP
elseif [ "$CHECKTYPE" = "http" ]
then
# Ping it! Normally we would test the success/failure of the ping here
ping $IP
fi
done
The beauty of what we have here is that we don’t need to reconfigure the script to add or remove servers, just maintain a simple config file.
The “if” statement: Shell-scripter’s Hammer
by someguy on Feb.25, 2006, under Uncategorized
One of the most common control structures in shell programming (and in many popular programming languages). Why? Because it is versatile and easy to understand. You can use it to test for many conditions that might exist. The syntax for an “if” statement can take several forms, but perhaps the most common is this one:
if [ <some test switch> <some file, number, or string of characters> ];
then
do something
fi
To see an exhaustive list of the comparison operators and switches here, issue “man test” at the command line. If it’s not obvious, the shell evaluates what happens between the brackets and only runs the code between “then” and “fi” if it evaluates to true.
Here’s a real example:
MYFILE=/etc/hosts
if [ -e $MYFILE ];
then
echo "/etc/hosts exists."
fi
and here’s what it looks like at a command line:
[me@myhost directory]$ MYFILE=/etc/hosts; if [ -e $MYFILE ]; then echo "$MYFILE exists." ; fi
/etc/hosts exists.
The “-e” flag tests if the file given exists.
What are some of the other things you can test for? Here’s a partial list:
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and is readable
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t [FD]
file descriptor FD (stdout by default) is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
For a complete list, run “man test” at your shell prompt. The “if” statement is commonly used in most other languages as well. Easy to use and a powerful friend.
Want to know how to do something at the shell? Just ask.
Intro to Bourne Shell
by someguy on Feb.01, 2006, under Uncategorized
$ echo ‘Hello, World!’
Hello, World!
Welcome to the Hello, World script as written at a UNIX or Linux shell prompt. Today we’ll learn some shell basics. What is the shell? A shell is a way of interacting with an operating system, just as you might use your mouse and keyboard for. Here, commands take the place of what you might otherwise do by pointing or clicking with a mouse.
Why bother? The shell is powerful and allows the user and the administrator to do some incredible things quite easily — that many times are not even possible through a graphical interface.
| command | What is it? | What does it do? | Sample Use |
|---|---|---|---|
| man | Manual Page Viewer | Gives you the instructions on how something works. | $ man pwd |
| pwd | Print Working Directory | Tells you where you are in the file system | $ pwd |
| cd | Change Directory | Takes you to another place in the file system | $ cd /tmp |
| ls | List Directory Contents | What’s in this directory | $ ls |
| less | Page through a file | Show me what is in the file one screen at a time | $ less myfile.txt |
“That’s nice, but show me what it looks like.”
OK, In the following examples, I am using the Cygwin Bash shell on a Windows XP system. Yes, you can even run a shell on Windows, and in fact it will blow you away how useful it can be. And by the way, Cygwin, like most Open Source software is free to download, install, use, etc.!

And a Cygwin Bash shell on windows is essentially a DOS or Command window for those familiar with them that allows you to run a bash shell instead of the default windows shell and have access to many UNIX/Linux type command line programs. In fact, the Cygwin installer lets you pick which other free programs you would like to install as well! Here’s a sample Cygwin bash shell window:

“Well, OK, but what does all that mean?”
The first thing you see in the shell window pictured is “someguy@mysystem”. By default, the Cygwin Bash shell customizes your prompt for you a little bit. The format in this case is “username@hostname”. That is to say, “who is logged in” (to Windows in my case) at “the name of this computer”. The next thing you see after that is the tilde(”~”). That is shell for “Home directory”.
On the next line you will see “$ pwd”. The “$” is the shell prompt itself! In most shells, the prompt will all be on one line - in this particular case it’s two. “pwd” tells me where I am on the system. At the shell you press “enter” on your keyboard after each command to make it run. In my case, the pwd command happily reports that I am in my Windows home directory, C:Documents and Settingssomeguy, or in Cygwin speak, “/cygdrive/c/Documents and Settings/someguy”.
I type “cd My Documents/personal” command and hit enter, which changes the directory I am in, to “/cygdrive/c/Documents and Settings/someguy/My Documents/personal”. You’ll notice I had to use a backslash after the word “My”. I do this to “escape” the space, because otherwise the shell would interpret this as two arguments to the “cd” command and try to put me into a directory called “My”, which does not exist.
The “ls” command gives a listing of what excatly is in the current directory.
I am interested in how the “less” command works, so I run “man less” to see the manual page about less. Most common UNIX commands are documented via “man” pages. To see the documentation, type “man ” where you replace ”
This completes today’s shell lesson. Stay tuned for many more…