Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

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!

Wednesday, April 2, 2008

The Noobs Guide to Bash-Scripting - Part 1

So let's say that you want to perform tedious file management tasks but you don't want to take the time out to program something in a real language (C,C++). A bash script is the right thing for you!


Setup

Enough talking, lets get started! As always, file structure in your projects is crucial to ensure that you don't accidentally delete something. So lets create a folder called "bash" inside your home directory, this will be where all your bash scripts are kept. Then create a folder called "helloworld" inside your bash folder. Now create a file called "hello_world" inside your "helloworld" folder.

You should end up with a folder structure like this:

---- bash
-------- helloworld
------------ hello_world


Now, open up hello_world with your favorite text editor. I suggest gedit, but there are many (vim, nano, etc...). On most Linux systems, the first line of your bash script should be:
#!/bin/bash

This is location of your bash program. Nothing complicated, just make sure it's the first line.


The Basics

The rest of the bash script consists of commands that you would normally be able to type in a command line.

eg. Typing ls in command line will get the same results as running a bash script that contained:
#!/bin/bash
ls

The commands in a bash script will be run in the directory that you run the script in. So if you run the above script in /home/user , then the script would print the contents of /home/user in your command line.

Back to our hello_world script; we want this script to output a message onto the screen. One suitable command for this is echo. The usage of echo is pretty simple:
echo "Your message here"
So, lets implement this in our hello_world script. Here is the final script:
#!/bin/bash
echo "Hello World!"
For more options on how to use echo, check out it's man page:
man echo


Finishing Up: Running the script


Almost there! Now all we need to is make sure the hello_world file is executable. We can do this with one command while in the helloworld directory.
chmod +x hello_world
Then it's a simple matter of running the script:
./hello_world
Alternatively, you can run the script without having to chmod it:
sh hello_world


Hopefully our script ran successfully, if not, go back and check that your code is correct.

Alright, now that our script is fully functional, we want to be able to run it without having to go into the .../bash/helloworld/ directory. This is relatively simple, all you need to do is copy your script into the /usr/local/bin directory. On most systems, you will need root access.

Once you've copied the script, then just have to enter hello_world into your command line to run the script.



This concludes the first part of The Noobs Guide to Bash-Scripting, there will be more interesting things coming in the next part, such as conditional statements, functions and arguments. So be sure to come back soon, until then, play around with your script!