Software by jason, for jason.
Software
iPlaylist CopierJava Libraries
ItunesUtilitiesScripts
Bash Scripts
File Size File Size Test Free Space Line Counter List Copy MD5 Tool OS X Rotate Logs Rename It SnapshotPHP Scripts
Write Variable To File WWS MailerJavascript Scripts
WWS Background ScalerInteresting People
Admins
Phil GlocknerDesigners
Tonya Browning Catherine Chu Justin Cox Richard Georges Stephen Gray Jonathan Horak Justin Kiehl Andrea Spencer MeatheadDevelopers
Chris Austin Joost van Dongen Noah Figg Rick Hogge John Quarles Wayne Rittiman Joe Rivera Brent Schneeman David Scott Andy Skelton Pavan TumatiHow To Set Up Your Computer To Run Bash Scripts
I have written several useful bash scripts and published them here on the World's Worst Software website for you to freely download and use. Most computer users do not know what a bash script is and most computers cannot run bash scripts without first installing the bash program itself. This article will help clear up what a bash script is and help you through the steps necessary to get bash scripts running on your computer.
Bash scripts are programs. Most any program you run on your computer can run other programs if it wants to once executed. That means a bash script can be just as malicious as a computer virus or any other malicious program. For example, a bash script could delete files off of your hard drive or compromise your system's security if it was written with a malicious intent. It is important that you treat bash scripts just like you would treat any other program you install on your computer. Make sure you are confident the programs you are using are authored by reputable sources and always always always scan programs with a virus scanner before you run them.
Fortunately bash scripts are a readable in any text editor. You can always open a bash script in your favorite text editor such as notepad and read through the script's contents to figure out what it's trying to do. If your are not confident in a script's intent or not confident that you understand code you see in a script then seek out the knowledge you need to gain that confidence. I would recommend the Advanced Bash Scripting Guide written by Mendel Cooper as a good starting place to figure out how a bash script is working.
What Is A Bash Script?
In its simplest form, a bash script is a list of commands. If you're familiar with MS-DOS batch files then one way to think of bash scripts is to think of them as the unix version of batch files. If you haven't heard of batch files or even MS-DOS then consider the following example...
Let's say you have a file that you want to copy to three locations every once in a while. Each time you copy the file you have to right click on the file, select copy, then right click in the place you want to copy the file and select paste. If you're copying the file to three different places then you're going to be hunting for folders and doing a lot of clicks to finish that copy job! That could be considerably frustrating if you had to copy those files multiple times per day or week. With an MS-DOS command prompt (or Terminal prompt for OS X/Unix users) you could type out the following commands rather than making all of those clicks:
copy myfile.txt c:\location1\myfile.txt
copy myfile.txt c:\location2\myfile.txt
copy myfile.txt c:\location3\myfile.txt
But typing all of that out isn't any better than making all of those clicks to do the copy paste version of the job! That's where batch files (or bash scripts) come in. With a batch file you can type those three commands into a text editor like notepad and save the text file as something like CopyMyFiles.bat. Then you could double click on that saved file and your operating system would read and execute all three of the commands you typed into the batch file for you!
That's the beauty of bash scripts. Bash scripts are basically simplified programs that help us get some grunt work done in an easier way than doing all of the work over and over manually.
Another great thing about bash scripts is that they're portable because bash and a lot of other great open source software is portable. Portable software is software that can run on multiple systems. For example, Microsoft Word is sort of portable because you can run a version of Word on your Windows computer and you can also run a version of Word on your OS X computer. Bash and the programs my scripts use are totally portable and can be downloaded and installed on your computer for free no matter if you're running Windows, OS X, or some flavor of unix. Portability is a nice feature of bash and other open source software because you can use it to write bash scripts on one computer (like a windows machine) and then still use the bash script on other machines (like a linux machine) without much hassle.
Bash Environment Setup 101
To get Bash and the programs needed for my bash scripts running on your computer we are going to do the following things:
- Install bash & other open source programs needed by my scripts.
- Create a folder for my scripts and adding it to your PATH.
- Download and test run one of my scripts.
Installing Bash & Other Software On Your Computer
As I mentioned previously in this article, all of my bash scripts rely on the Bash program to run. But many of the programs also rely on other open source tools to run. In most cases you'll be able to easily find and install any of the programs my bash scripts need to run. Every bash script on World's Worst Software has a list of program dependencies on it's description page. For example, the MD5Tool.sh script lists the following dependancies:
bash, echo, cd, pwd, test, rm, touch, md5sum, find, xargs, grep, sed
A lot of those dependencies are actually parts of bash. For example, Bash knows how to do echo, cd, and pwd, but md5sum, find, and grep are not a part of bash. Those commands are seperate programs that must be installed along with bash. Most of the programs used by my bash scripts come as a standard part of your operating system or package system's default installation selections, but you may run one of my bash scripts and see something like the following:
bash: md5sum: command not found
If you see a message like that when running one of my scripts then you probably need to go find and install the program that bash is complaining about not finding. In this case you'd need to install the md5sum program.
The process of installing open source software such as Bash on your computer varies depending on your Operating System. Read the section below that applies to you.
Installing On Windows
Windows users will need to download and install several packages that come from the cygwin system. The gracious people at cygwin have made a easy to use package installing system for windows users to get cross platform open source software easily installed on their computer. You simply download the setup.exe file, run it, select the packages you want to install and wait for the installer to update your system for you. The Cygwin User's guide will help guide you through the process of installing cygwin packages on your computer.
Bash is part of the default packages that are automatically selected when you start the cygwin installation program, but some tools like bc are not, you'll need to select those packages to install in the Select Packages window. For example, in the default category view the bc tool is under the Math part of the package list.
Installing On OS X
OS X users will already have most of the stuff needed by my bash scripts installed on their system since OS X is a unix derivative. However, there are occasionally typical unix utilities that you'll find missing in certain flavors of OS X. For example the md5sum utility used by my MD5Tool.sh bash script is part of OS X 10.3 (panther) on my ibook that I have, but I had to install md5sum manually on my OS X 10.4 (tiger) mac mini computer. There are two packaging systems that I'm aware of for OS X. Those are Mac Ports (formerly DarwinPorts and fink. Last I looked into the issue, it seemed both of those packaging systems were in flux or coming back from a hiatus, so I often install most of my OS X tools from source. Ravi Pratap M's Getting Started With Open Source Development F.A.Q is a great starting point for learning how to install open source programs from source.
The md5sum program is part of the GNU Textutils package. The directions on the Textutils home page say that the contents of the package have been rolled up into another package called Coreutils. I personally made the mistake of installing the entire Coreutils package from source on my OS X 10.4 box and hosed the entire operating system because I didnt understand that Coreutils also contains implementations of base-level operating system programs such as su. DO NOT INSTALL THE ENTIRE COREUTILS PACKAGE ON OS X, IT WILL MESS UP YOUR OS X SYSTEM AND REQUIRE A FRESH OS X INSTALL!!! Apple has specially modified some core system utilities to get OS X working properly and installing the GNU variants of those programs over the OS X defaults will seriously mess up your system. Instead try to find a way to build md5sum by itself and only install that tool.
If you know how to install just md5sum from the GNU Textutils or Coreutils package, please contact me so I can update this section of the article with better instructions for our OS X friends and cite you for your generous help on the matter!
Installing On Unix/Linux
I haven't used pure unix or linux operating systems in a couple of years and I assume most linux users are familiar with their particular linux flavor's packaging system and/or installing software from source. If you're not familiar with installing software from source or with a packaging system then perhaps Pratap M's Getting Started With Open Source Development F.A.Q will help get you on your way. If that doesn't help, feel free to contact me and get me motivated to fill in some better unix-specific instructions here.
Creating A Script Folder And Adding It To Your PATH
Your operating system finds programs to run by looking for them in a list of directories it knows about. It first searches the directory you're working in or your shortcut is in and if it can't find the program there then it tries to find the program you're trying to run in a pre-defined list of directories to look in for programs. That list of directories is called the PATH and you can edit your system's PATH to add or remove directories to look in for programs.
We are going to create a new directory to store my bash scripts in and then add that directory to the PATH so your operating system can find my scripts and run them from anywhere on the computer.
First create a directory, for this example I will use the following directory I created on my personal computer:
C:\wwsScripts\
OS X or Unix users will create a directory that'll look more like the following:
/usr/home/jason/wwsScripts/
Next we simply add our new directory location to the end of our PATH variable. Remember that you only need to add your directory to the end of the directory list and should not delete other entries in the directory list.
Windows user's can read Victor Laurie's Environment Variables in Windows XP article for instructions on altering their PATH variable.
OS X and unix users will put something like the following in their bash .profile and/or .bashrc file depending on their needs.
export PATH="$PATH:/usr/home/jason/wwsScripts/"
Matisse Enzer's Configuring Your OS X Unix Environment article is a good starting point for beginner OS X and unix admins to learn about where to put the PATH additions.
Download And Test Run One Of My Scripts
Installation of my scripts is pretty easy. You simply download the .sh file from the World's Worst Software website and place the file in the scripts directory (for example: c:\wwsScripts\) that we just created and added to the operating system path.
An easy test to make sure everything is working correctly is to download filesize.sh and place it in your new scripts folder. Then open a command prompt (or terminal if you are an OS X or Unix user), and type the following command followed by the enter key:
bash filesize.sh
If you see something like the following, then you've correctly setup your bash environment and are ready to start using bash scripts!
filesize.sh displays the space usage for the file or directory you specify
USAGE
filesize.sh FILE_OR_DIRECTORY
ARGUMENTS
FILE_OR_DIRECTORY - the file or directory to display space usage for
If you don't see output like the above and instead see something like the following then you may not have added the new script directory to your PATH correctly, or you may not have saved the filesize.sh file into your new script directory:
bash: filesize.sh: command not found
If instead you see an error like the following in windows then you may not have setup cygwin correctly and probably need to either reinstall cygwin or add something like c:\cygwin\bin to the FRONT of your PATH list:
'bash' is not recognized as an internal or external command, operable program or batch file.