Sunday, 4 March 2012

Beginners' Guide to Shell Scripting

It has come to my attention that a lot of people need a beginners' guide to shell scripting, therefore I am obliged to provide one. 

This tutorial will provide a grounding for shell scripting.

Shell scripting in Linux isn't to difficult to master, although it helps if you have some programming background. We'll start by explaining the shell.

The shell is a program which actually processes commands and returns output. Bash is the most popular Linux shell as it contains standard features such as foreground and background processes and command history.

A question that is asked a lot is: "what's the difference between the Console and the Terminal?"

The Terminal is a wrapper program which runs a shell. This was once a physical device which consisted of not much more then a monitor and keyboard. As time moved on this became software and can be used to access the shell and run commands.

The Console is a special kind of Terminal, the Console used to be used for low level communication with the operating system. These days it is used in much of the same way as the Terminal.

In conclusion there isn't really much difference between the two, they both have separate pasts but are now essentially used for the same thing.

Writing Scripts
I recommend using gedit to write your scripts as it has a lot of great tools to make use of, although if you are unfortunate enough to be using a Windows system I recommend Notepad++ (I use this at work to write scripts and it's very similar to gedit).

At the top of each script the we have a Shebang line.  It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line. Then we would have the command we wish to execute.

echo Hello Linux!

The above script when run will display the words "Hello Linux" in the console/terminal. It uses the echo command which does what it says on the tin, it will echo out the words Hello Linux!.
This is one way of doing it, however the use of variables is more elegant. A variable stores data in memory and can be called at differnt times within the script, it can also be changed and amended throughout the script. Below is an example of Hello Linux with variables.

VAR="Hello Linux!”
echo $VAR

Next we must learn how to use the chmod command, this is use to set the permissions for the script we have just created.

the chmod command (change mode) is a command that lets the user control how much access it should permit to a file. In order to run our script we must first use chmod to make it executable  to do this I use the command

chmod +x

the +x makes the scipt executable for ALL users, if you just want it to be exe for you then u+x is for you.

After this command a simple ./ will run the script.
Hopefully this should give you a good grounding for writing your own shell scripts. 

Any questions are welcome!

No comments:

Post a Comment