Suite101

Simple Scripting


© Robin Friedman

Your Unix shell (The program that takes your commands) is a powerful tool. One of its greatest strengths is its ability to use scripts. Anything you can do by typing in commands one by one can be done in a script. You can use them to automate tasks that you need to preform often, or you can use them to create programs that work for you.

First create your file and choose which shell you want to interface with. Since bash is the most often used shell, it's a good idea to run a script with it. The first line of your script should be: #!/bin/bash where /bin/bash is the full path to bash. Here is a sample script program. echo "here are some files for you, $USER"echo is a command that displays text on the screen, and ls lists the files in your current directory. Notice two peculiarities here, the $USER and the # list files. $USER is what's called a variable, it holds a piece of information. USER is a special variable, that in UNIX will always hold the user's login name. So the statement above will print out the ls # list files user's name instead of the literal text, $USER. All text which comes after a # is considered a comment. So above, the text list files doesn't really do anything, it just clarifies the command, 'ls', for when you are looking at the code.

Save your file and quit, and make it executable by typing chmod (filename) -u -x+ .

Then simply execute your file, for example by typing ./(filename) . You should see on your screen, here are some files for you, $USER, followed by a list of files.

Remember that you can use any command in scripts that you can in normal UNIX use. So you can simply save yourself time in doing many commands. For example, one common use of shell scripts is to create an internet dialer that goes through all of the tedious commands involved in dialing an ISP.

This guide was not meant to show you all of the programming tools available with shell scripting, there are several guides on the internet for that. This is simply supposed to show what a shell script can do and how easy it is to implement one.

Go To Page: 1


The copyright of the article Simple Scripting in Linux/Unix is owned by . Permission to republish Simple Scripting in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo