Archives for May, 2010

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.

6
May

Set an “Alarm Clock” for a Program to Run at a Certain Time

If you ever have the need to setup a program to run at a certain time, then the at is the command for you. Here’s the standard blurb from the man pages explaining what it is:

       at and batch read commands from standard  input	or  a  specified  file
       which are to be executed at a later time, using /bin/sh.

       at      executes commands at a specified time.

       atq     lists  the  user's  pending  jobs, unless the user is the supe-
	       ruser; in that case, everybody's jobs are listed.   The	format
	       of  the	output	lines (one for each job) is: Job number, date,
	       hour, queue, and username.

       atrm    deletes jobs, identified by their job number.

Now, I won’t reinvent the wheel on how to utilize the command. Instead, I’ll simply point you to this reference over at nixcraft (cyberciti.biz). Have fun!

5
May

Beagle Board Quick Start Guide with Setup Scripts

I recently got my hands on a Beagle Board to play around with at work, so I figured I would document and create a set of scripts that anybody can use to quickly setup a Beagle Board to boot from an SD card (2GB+). I’m going to assume you know the standard blurb of instructions: copy/download scripts, give them executable rights, and then run with root privileges while taking caution as to not mess up your Linux box. Also, please keep in mind that images and file locations change over time. Let’s begin!
[Read more →]

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
« Previous Page