Thursday, April 3, 2008

The Noobs Guide to Bash-Scripting - Part 2

For those who don't know, this is a continuation of a series, the first of which is located here.

So far we've covered the very basics on how to get a script up and running. We've had it display some trivial information too! But this is hardly useful for everyday applications.


The Situation

For instance, lets say your working in a group of programmers. You are working on a large project, and your team is about to put all your files with the rest of the projects files. You suddenly realize that you forgot to put your name on each of your files! We can write up a quick script that will add a tag on the end of all of you files within a certain directory. Note: Unlike the last tutorial, I will not be telling you what your files should be called, you should be able to decide for yourself.


Arguments + Variables

To do this, we need to somehow tell the script what directory to process. This can easily be done by adding arguments on the end of the command. eg:
./example_script argument1 argument2
You can have as many arguments as you want. You can also use "*" as an argument, but we'll go into that later... Arguments can be easily accessed in the script, they are automatically assigned to a variable.

Whoa! Timeout! What the fart is a variable?

Well, a variable is basically a value (1, "three", "foobar", 1.43), that is represented by a name. In bash scripting, you can tell something is a variable by the fact it has a $ in front of the rest of the name. Variables can be created very easily:
example_variable="example_value"
echo $example_variable
The output of the above script would be "example_value". Notice how when we created the variable, we didn't include the $ at the beginning. This is very important, don't forget it!

Continuing on... whenever your script is started with arguments, variables are automatically created for you! They are: $1, $2, $3, $4 ... etc. There will only be as many variables as there arguments. Lets put this to use! Create a script with the following:
#!/bin/bash

echo $1
Then run it like this:
./script_name example_argument

The important thing about arguments, is that they can be file or directory names too! Do you see where we're going with this? We can specify a file for the script to "tag" with our name, therefore eliminating the need to open every single manually.
#!/bin/bash

echo "//Written By A Programmer" >> $1

So now we can tag that message on the end of each file with one command...
./tagging_script example_file

So far it's looking good! But there are still a few problems,
  • We still need to run this command for every single file... (This will be fixed in our next tutorial)
  • If we give this script to our friends, they will have to change the script so that their name is there
The second problem is addressed fairly easily! Along with the argument variables, there a whole bunch of other variable that accessible. You can see them by going into your console, and typing "$" and then pressing [TAB] twice. This will list all the available variables, but we're only looking for one... "$USER". This is the current user running the program, we can easily put this into our script.
#!/bin/bash
echo "//Written By $USER" >> $1

Great! Now our script is more portable! Portability is very important when it comes the script writing, or any programming in general. It takes a little more work now, but it will most likely save you lots of work later on!


This concludes the second part of our bash scripting tutorial. Next time: For loops and if statements! The fun never stops! Hope to see you here next time!

No comments: