Archive for the ‘Scripting’ Category

25
Jun

How To: Add a “Press Any Key to Continue” Message to a Script

Adding a “Press any key to continue” message to a script is actually quite easy because it’s already built into the read command. Here’s a couple examples of how to use it:

#!/bin/sh

echo "A First Method"
read -s -n 1 -p "Press any key to continue..."

# insert echo here for cleaner output
echo

echo "A Second Method"
echo "Press any key to continue..."
read -s -n 1 any_key
echo "Now exiting"
exit 0

Note: “any_key” was simply a made up name. It can be anything. As always, be sure to take a look at the man page for more information!

11
Jun

Run Shell Script Subroutines in Terminal with Source Command

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…

startmyday(){
   cal
   date
   echo
   /usr/games/fortune -s
}

anotherfunction(){
   echo "put whatever you want in this function call"
   echo "this is just a filler."
}

yetanotherfunction(){
   echo "put whatever you want in this function call"
   echo "this is yet another filler."
}

This is showing the sourced shell script subroutines in action…

# startday
     June 2010
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30         

Fri Jun 11 04:17:56 UTC 2010

Do not drink coffee in early A.M.  It will keep you awake until noon.
# anotherfunction
put whatever you want in this function call
this is just a filler.
# yetanotherfunction
put whatever you want in this function call
this is yet another filler.
#
7
Jun

Script to Wait for Wireless AP Association During System Startup

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.

#!/bin/bash
#
# CONNECT TO WIRELESS NETWORK
# Author: Errol E. Burrow II <eburrow@gmail.com>
#
# call this script from /etc/rc.local
# this script uses fping which can be installed
# with sudo apt-get install fping
#  

# globals
MYIP="192.168.0.121"
WAPNAME="SSVR3"
GATEWAY="192.168.0.141"
PINGTESTIP="10.10.10.1"
RETRYCOUNT=10

# turn on extra regex features
shopt -s extglob

until [ $RETRYCOUNT -lt 1 ]; do
	ifconfig wlan0 $MYIP
	iwconfig wlan0 essid $WAPNAME
	# get the access point mac address for wlan0
	ap=$(iwconfig wlan0 | sed "s/Access/~+&/" | tr "~" "n" | grep "+" | cut -c16- | tr -d " ")
	echo "access point mac: [$ap]"
	# check if the ap actually has an expected value
	if [[ $ap =~ [0-9a-f]{2}[:-] ]]; then
		echo "success: connected to wifi access point!"
		route add default gw $GATEWAY
		echo "added default gateway $GATEWAY"

		echo "testing ping to $PINGTESTIP"
		if fping -a -r1 $PINGTESTIP > /dev/null
			then
				echo "success: $PINGTESTIP is alive!"
				break
			else
				echo "warning: $PINGTESTIP cannot be reached!"
		fi
	else
		echo "warning: not connected to wifi access point"
		# try again
	fi
	echo "retrying connection: $RETRYCOUNT"
	let RETRYCOUNT-=1
	# sleep a bit
	sleep 1
done
17
May

Picup: A Keyword Scanning Script to Synchronize or Upload Photos to an Online Gallery

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.

Download picup
(Right-Click and “Save Link As…”)

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
   username@domainname.com:/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.

10
May

Echo or Cat Multiple Lines or Paragraph of Text from within a Shell Script

If you find yourself needing to echo out multiple lines to the console, or even to another file, then you’ll want to use the following method which is much cleaner and much more efficient. It’s very useful in creating another document, script, or file without having to use echo for each line.

#!/bin/sh

cat > new_file << EOF
This will be line one
This will be line two
This will be line three
   This will be line four indented
Notice the absence of spaces on the next line
EOF

cat new_file

Basically, what the above script will do is use cat to write the following lines up to a delimiter, in this case EOF, to a file called new_file. The most important thing to keep in mind is that for this to work, there cannot be any spaces before the delimiter. The output of running this script looks like:

This will be line one
This will be line two
This will be line three
   This will be line four indented
Notice the absence of spaces on the next line

Naturally, this method will also work in the terminal command line and is not restricted to only shell scripts.

7
May

sfdisk: The Scriptable fdisk

There was a brief mention of scripting partition editing in my post a couple of days ago about setting up an SD card for a Beagle Board. I revisited the script that was used to prepare the SD card partitions, took a closer look at it, and figured it was important enough to give it a mention in it’s own post — sfdisk.

Simply put, sfdisk is a scriptable fdisk which allows you to automate steps taken to create a partition layout. Here’s the standard blurb from the man page:

sfdisk has four (main) uses: list the size of a partition, list the partitions on a device, check the partitions on a device, and – very dangerous – repartition a device.

sfdisk accepts commands from stdin and the format is mostly comma separated for each field that is available which is:

<start> <size> <id> <bootable> <c,h,s> <c,h,s>

Be sure to check the man page for more information (as always). One quick example is presented in the man page:

sfdisk /dev/hdc << EOF
0,407
,407
;
;
EOF

Since the Beagle Board SD card setup script by XorA gave such a great usage example, I’d like to present it here again in case you missed it, except in a minimal form to avoid distractions:

#!/bin/sh
# Example Usage: create_sd /dev/sdb

DRIVE=$1

dd if=/dev/zero of=$DRIVE bs=1024 count=1024

SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`

echo DISK SIZE - $SIZE bytes

CYLINDERS=`echo $SIZE/255/63/512 | bc`

echo CYLINDERS - $CYLINDERS

{
echo ,9,0x0C,*
echo ,,,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE

mkfs.vfat -F 32 -n "boot" ${DRIVE}1
mke2fs -j -L "rootfs" ${DRIVE}2

Have fun with it! While you’re at it, you may want to check out the parted command with the --script option. It’s very handy if you work with disk partitions frequently.

3
May

Shell Script to Display Multiplication Table

I stumbled upon this while researching some other shell script solutions and thought it was pretty clever and nifty. It came from arachnoid.com and I modified it a bit to take an argument from the command line and adjust the width accordingly so the table doesn’t get messed up.

!/bin/sh

width=`echo $(( $1 * $1 )) | wc -c`
y=1
while [ $y -le $1 ]; do
        x=1
        while [ $x -le $1 ]; do
                printf "% ${width}d" $(( $x * $y ))
                let x++
        done
        echo ""
        let y++
done

Example usage:

$ ./mult_table 12
   1   2   3   4   5   6   7   8   9  10  11  12
   2   4   6   8  10  12  14  16  18  20  22  24
   3   6   9  12  15  18  21  24  27  30  33  36
   4   8  12  16  20  24  28  32  36  40  44  48
   5  10  15  20  25  30  35  40  45  50  55  60
   6  12  18  24  30  36  42  48  54  60  66  72
   7  14  21  28  35  42  49  56  63  70  77  84
   8  16  24  32  40  48  56  64  72  80  88  96
   9  18  27  36  45  54  63  72  81  90  99 108
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144

Just for fun, I went on and modified it a bit more to accept a starting point as well, just in case you’re working with a table that doesn’t fit in your terminal screen (however, some readability improvements could be made):

#!/bin/sh

if [ $# -lt 2 ]; then
        echo "Usage: $0 <start> <finish>"
else
        width=`echo $(( $2 * $2 )) | wc -c`
        y=$1
        while [ $y -le $2 ]; do
                x=$1
                while [ $x -le $2 ]; do
                        printf "% ${width}d" $(( $x * $y ))
                        let x++
                done
                echo ""
                let y++
        done
fi

Example usage:

$ ./mult_table 5 12
  25  30  35  40  45  50  55  60
  30  36  42  48  54  60  66  72
  35  42  49  56  63  70  77  84
  40  48  56  64  72  80  88  96
  45  54  63  72  81  90  99 108
  50  60  70  80  90 100 110 120
  55  66  77  88  99 110 121 132
  60  72  84  96 108 120 132 144
23
Apr

Shell Script While Loop to Wait for a File to Appear or Else Timeout

The following shell script snippit will continually check and wait for the presence of a file until it is found, or until it reaches a timeout period, which in this case, is about 10 seconds, whichever come first.

#/bin/sh

x=0
while [ "$x" -lt 100 -a ! -e /path/to/the/file_name ]; do
        x=$((x+1))
        sleep .1
done

This can be helpful to prevent a race condition in a script if you’re say, mounting a filesystem from an external device. You could take it a step further and print useful messages as well:

#/bin/sh
$FILE="/path/to/the/file_name"
x=0
while [ "$x" -lt 100 -a ! -e $FILE ]; do
   x=$((x+1))
   sleep .1
done
if [ -e $FILE ]
then
   echo "Found: $FILE"
else
   echo "File $FILE not found within time limit!"
fi

Another, maybe more elegant, solution is to use the wait command:

wait [pid]...

Wait for your background process whose process ID is pid and report its termination status. If pid is omitted, all your shell’s currently active background processes are waited for and the return code will be 0. The wait utility accepts a job identifier, when Job Control is enabled (jsh), and the argument, jobid, is preceded by a percent sign (%).

If pid is not an active process ID, the wait utility will return immediately and the return code will be 0.

20
Apr

A Script to Make Use of the “About Me” System Preference Utility

The Linux distributions, such as Fedora or Ubuntu who ship the Gnome desktop environment, include a utility in the “System -> Preferences” menu called “About Me”. This fun little utility allows you to enter in all of your personal and/or work information and also allows you to pick a cute avatar icon which is shown when you login. Beyond setting an avatar icon, I haven’t found much use in it or seen any benefits that it provides. I remember reading a post about it on the Ubuntu forums a while back which asked people if they use it. The response was that nobody used it except to set their login icon. While thinking about this, I figured I would recommend a use for the “About Me” utility: setup a signature script for Evolution. Please feel free to use this as your own starting point. I included some of the basics of what I may want in a signature, but if you need more, it’s not hard to add it. The following script parses out the information from the binary file that’s created within the hidden .evolution home folder and displays the information. You can even choose between “work” or “home” information shown. Enjoy! Oh, and I’d love to hear how else the “About Me” utility can be used, so drop a comment below…

#!/bin/sh

## SELECT TYPE OF SIGNATURE
TYPE=WORK
#TYPE=HOME

## SETUP $ABOUT FILE AND FULLNAME
ABOUT="$HOME/.evolution/addressbook/local/system/addressbook.db"
FULLNAME=`finger `whoami` | grep "Name:" | cut -d : -f 3 | sed 's/^.//'`

## PARSING $ABOUT FILE
ORG=`grep -a "ORG:" $ABOUT | cut -d : -f 2 | cut -d ";" -f 1`
ORG_WITH_DEPART=`grep -a "ORG:" $ABOUT | cut -d : -f 2`
TITLE=`grep -a "TITLE" $ABOUT | cut -d : -f 2`
ROLE=`grep -a "ROLE" $ABOUT | cut -d : -f 2`
MANAGER=`grep -a "X-EVOLUTION-MANAGER" $ABOUT | cut -d : -f 2`
ASSISTANT=`grep -a "X-EVOLUTION-ASSISTANT" $ABOUT | cut -d : -f 2`
EMAIL=`grep -a "EMAIL;TYPE=$TYPE" $ABOUT | cut -d : -f 2`
JABBER_IM=`grep -a "X-JABBER;TYPE=HOME" $ABOUT | cut -d : -f 2`
#FIXME: A grep for "URL" matches two URLs (Blog and Homepage)
WEBSITES=`grep -a "URL:http" $ABOUT | cut -d : -f 2,3`
PHONE=`grep -a "TEL;TYPE=$TYPE,VOICE" $ABOUT | cut -d : -f 2`
CELLPHONE=`grep -a "TEL;TYPE=CELL" $ABOUT | cut -d : -f 2`
STREET=`grep -a "ADR;TYPE=$TYPE:" $ABOUT | cut -d ";" -f 4`
CITY=`grep -a "ADR;TYPE=$TYPE:" $ABOUT | cut -d ";" -f 5`
STATE=`grep -a "ADR;TYPE=$TYPE:" $ABOUT | cut -d ";" -f 6`
ZIP=`grep -a "ADR;TYPE=$TYPE:" $ABOUT | cut -d ";" -f 7`

## BEGIN SIGNATURE
# Evolution expects signature in HTML format, so surround block with
# <p>aragraph markings and all newlines need <br />
echo " <br /><p>"
echo "Best Regards, <br />"
echo "<br />"
echo "$FULLNAME <br />"
echo "$TITLE <br />"
echo "$ROLE <br />"
echo "$MANAGER <br />"
echo "$ASSISTANT <br />"
echo "$ORG <br />"
echo "$EMAIL <br />"
echo "$JABBER_IM <br />"
echo "$WEBSITES <br />"
echo "$STREET <br />"
echo "$CITY, $STATE $ZIP <br />"
echo "$PHONE <br />"
echo "$CELLPHONE <br />"
echo "</p>"

What you’ll see after saving this to a script, giving it executable rights (chmod +x about_me_sig), and then running it:


Best Regards,

My Full Name
My Title
My Role
My Manager
My Assistant
My Organization
My Email
My Jabber
My Website
My Street Name
My City, My State, My ZIP
My Phone
My Cellphone

If you don’t know how to integrate this with Evolution, please see my other step-by-step guide here:
Generate Random Quotes with a Shell Script [Part 3/3: Integrate with Evolution Email].

Also, you could take it a step further and install the fortune program to give a random quote as well.

Another thought was to use the above script to populate the .plan file which is shown when your username is invoked with the finger command. For this, you would need to setup the script a bit differently and experiment with newlines and what-not as well as setup a cron job to make sure that it’s updated regularly for when you update the “About Me” information. To test it out, simply run the commands below (the above script has been named “about_me_sig”):

./about_me_sig > .plan
finger `whoami`

Have fun!

12
Apr

Generate Random Quotes with a Shell Script [Part 3/3: Integrate with Evolution Email]

This is the last part of the “Generate Random Quotes with a Shell Script” series of posts where we looked at 1) setting up user defined/specified quotes and 2) getting quotes from an online source such as quotationspage.com. For this post, I’ll walk you step-by-step on how to integrate an online source of quotes into a signature in the Evolution email client. Let’s get started…
[Read more →]

« Previous PageNext Page »