Tag Archive: shell script


This is a nifty trick you can use to impress your friends.  Imagine you have some smallish program that you want to wrap up into a single file for simplicity.  Part of final output perhaps relies on a clever bit of shell scripting and another part relies on a binary of some sort that you need to get the job done.  Here’s a way to wrap it all up in a tidy little package:

#!/bin/sh
grep SRC: $0 | grep -v “grep SRC:” | sed -e s/SRC://g > /tmp/hello.c
gcc -o hello /tmp/hello.c
./hello
exit
SRC: #include <stdio.h>
SRC: int main(int argc, char *argv[])
SRC: {
SRC: printf(“Hello World\n”);
SRC: return 0;
SRC: }

Everything below exit is completely ignored by your shell.  The grep statement goes through the shell script line by line and returns the all the lines starting with “SRC: ” to sed.  Sed uses a simple regex to strip the “SRC: ” off your source code and sends the output to a file in your /tmp directory.  Then the file is compiled and ran.  You could go further and delete the source file out of the /tmp directory if you like or don’t bother.

My last article is nice for checking for a few dependencies in a shell script but if your user has to keep running your script and then go back and install something and run it again, this can get very tiresome.  It’s much nicer to give your user a big list of deps they need to install in one shot.  Here is some code to do just that.

failed_dep=0
if ! which pkg1 > /dev/null; then
echo “Please install pkg1 or make sure it is in your path”
failed_dep=1
fi
if ! which pkg2 > /dev/null; then
echo “Please install pkg2 or make sure it is in your path”
failed_dep=1
fi
if ! which pkg3 > /dev/null; then
echo “Please install pkg3 or make sure it is in your path”
failed_dep=1
fi
if ! failed_dep==1; then
exit
fi

Obviously remember to change the “which pkg” to the dependency you are looking for and change the echo statement to reflect this as well.  Just keep adding if blocks if you need to check for more than three packages.  If you have a LOT of dependencies, your end user will thank you for using this method.

I’ve been toying around with my ZIPIT Z2 Messenger since I received it on March 10th.  The first thing I did after un-boxing it was to hack it of course.  It’s a linux-based device at heart but by replacing the bootloader with the openzipit bootloader and imaging a micro sd card with Irongeek’s side-track distribution the Zipit shows it’s untapped potential.  It’s certainly not perfect in many regards but at the same time it’s quite usable.  One complaint I had was that the wireless configuration utility that is included with side-track is clunky and confusing.  I ran into rkdavis on the #zipit channel on freenode and he has been writing a small script to make it a bit easier to connect to a wifi network.  I played with the script a bit and he has updated it with some of my suggestions to make it more compatible with Debian-based distributions on the Zipit Z2 such as side-track.  It’s a neat little script that uses the dialog utility for a polished interface.  Anyone who has ever “make menuconfig’d” something is familiar with what dialog looks like.

We still have some hitches to work through but here is a link to the Easy Wifi Configurator a.k.a. EWoC.  Look around rk’s site though because he may have a newer version by the time you read this post.  Small word of warning, you will need to “apt-get install dialog” in order to run the script.

Powered by WordPress. Theme: Motion by 85ideas.