Tutorials, Guides, Tips, and Tricks from Everyday Experiences

Archive for the ‘Scripting’ Category

I used this script the other day when I wanted to randomize a group of photos from within my current working directory in the terminal (so my digital photo frame displayed randomly instead of sequentially). You could certainly spend some extra time making this more robust, but it suited my needs. You’ll probably want to modify it a bit to suit yours.

Edit: Thanks to the commenter “thewanderer”, I revisited this script to solve the issue of duplicate $RANDOM values. Now, there’s a recursive function added so files won’t be overwritten!

This is a shell script that outputs your current unix color scheme. This is handy for when you’re trying to come up with your own theme. It comes from http://www.frexx.de/xterm-256-notes/. I’m taking no original credit for this script. I simply want to share it with the readers since I found it very clean and useful. You simply copy the code to a script, use ‘chmod +x colortheme.sh’ as usual, and then run it with ./colorscheme.sh. I’m going to duplicate the script here for redundancy (I want to make sure it’s always there for myself too!):

#!/bin/bash
#
# Description:
#
# Prints a color table of 8bg * 8fg * 2 states (regular/bold)
#
# Copyright:
#
# (C) 2009 Wolfgang Frisch <[email protected]>
#
# License:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
echo
echo Table for 16-color terminal escape sequences.
echo Replace ESC with \033 in bash.
echo
echo "Background | Foreground colors"
echo "---------------------------------------------------------------------"
for((bg=40;bg<=47;bg++)); do
for((bold=0;bold<=1;bold++)) do
echo -en "\033[0m"" ESC[${bg}m | "
for((fg=30;fg<=37;fg++)); do
if [ $bold == "0" ]; then
echo -en "\033[${bg}m\033[${fg}m [${fg}m "
else
echo -en "\033[${bg}m\033[1;${fg}m [1;${fg}m"
fi
done
echo -e "\033[0m"
done
echo "--------------------------------------------------------------------- "
done
echo
echo

A valuable tool when developing and debugging scripts is set. In particular, the -x or -xtrace options of set will “print a trace of simple commands and their arguments after they are expanded and before they are executed”. Here’s an example of using set -x:

If you run a set of commands frequently, you might want to think about creating a sourced shell script with subroutines. For example, maybe you like to see the current date, a calendar, and a quick fortune (with the fortune package installed). Instead of typing cal and then date and then /usr/games/fortune -s manually each time, simply include it in a shell script that contains a subroutine that will do it all for you. All you need to do is create the script and subroutines and then source it like this:

. /my.subr

Notice the space between the period [.] and the script name. I usually use the .subr extension on my sourced scripts to tell them apart, but you can use whatever filename you want (it doesn’t even have to have an extension).

Then, simply call your subroutine within your shell script. Continuing with the example scenario described above, here is the shell script called my.subr that was sourced…

The following is a script that could prove to be useful in an embedded Linux environment utilizing a wireless adapter. In order to connect to a wireless network, the wireless adapter needs to associate with an AP which can take some time occasionally. If you’ve edited the /etc/network/interfaces file to automatically obtain an IP address via DHCP, and it doesn’t seem to be getting an IP address during system startup, then this script might be able to help. This script is intended to run on system startup and wait for the access point association. If one is not found, it will eventually timeout.

Picup is a script that scans for photos that match a specified keyword and then uploads it to a gallery on a remote server. Although this script serves a specific purpose to myself, I believe it will be very handy for others to use as well. There is only one dependency: rsync and sh, meaning it will run on just about any UNIX system like Linux or Mac OSX.

I’ve included two options for getting your photos uploaded to a website: 1.) directly upload the photos that have been found that match the keyword and 2.) synchronize all the photos (even the untagged photos) to a general location on the server and then have the cream of the crop (photos marked with keyword) displayed in another location on the server such as a gallery. The picup script works quite well with photo gallery websites like ZenPhoto where a database is not necessary and photos can be directly uploaded to an album folder such as /public_html/zenphoto/albums/.

There are many other options available as well that can be easily configured. These are all documented in the configuration file called picup_config which is automatically generated and populated with examples when the script is ran for the first time.

Here’s an example run to give you a better idea of what the script actually does:

$ picup
Searching for pictures marked with "favorites" in:
/MyFiles/Pictures/2010/05 - May/
This might take a while depending on the amount of files in directory...
2010/05 - May/My Pictures in May/IMG_0001.jpg
2010/05 - May/My Pictures in May/IMG_0002.jpg
2010/05 - May/My Pictures in May/IMG_0003.jpg
Please VERIFY the steps that are ABOUT TO BE EXECUTED!
This script is about to synchronize the directory
/MyFiles/Pictures/
with the remote site
[email protected]:/home/username/public_html/pictures
with the options of
-avzh -e ssh --numeric-ids -i --delete-after --exclude-from=/MyFiles/Pictures/excludes
which will appear on the server as
/home/username/public_html/pictures/2010/05 - May/My Pictures in May/IMG_0001.jpg
When finished, the following command will ran on the server
ln -s "/home/username/public_html/pictures/2010/05 - May/My Pictures in May/IMG_0001.jpg" "/home/username/public_html/gallery/albums/2010/05 - May/My Pictures in May/IMG_0001.jpg"
Do you wish to continue? <yes,no> yes
building file list ... done
<rsync output snipped>
Uploading and executing script on server...
server_exec 100% 1071 1.1KB/s 00:00
Finished uploading your pictures. Enjoy!
$

I hope you find some use in this script. One of the goals that I was working hard for was to make this as easy as possible to use as well as keep the script speedy in execution. If you have questions, please let me know. I’d like to know if this gets any attention in the ZenPhoto community or elsewhere.

Also, if you’ve seen my old embarrassment of the picup script , I believe this to be a massive improvement.