LINUX BASH SHELL SCRIPT BASICS Part 1:
What is BASH?
BASH is a shell that runs in the Linux terminal. BASH is not the only shell that users can choose but it’s the one most commonly shipped with Linux distributions. BASH is also available on Unix, Mac OS and Windows. It is a universal way to interact with a computer. The shell serves as a Command Line Interpreter which means it takes humanly readable commands and passes them to the Linux kernel. The kernel then sends instructions directly to the hardware. BASH doesn’t just run commands and other programs, it has its own programming language too.
What is a BASH shell Script?
BASH does not care where the commands it runs come from. It will happliy accept input from a human being typing on a keyboard, another program running on the system or it can get its instructions from a special kind of text file called a BASH Shell Script. Shell scripts can be used to automate common tasks and they make accomplishing complex computing tasks possible with just one command or the click of a mouse. You can also set shell scripts to run at start up so special parameters can be set or certain programs are opened automatically. They can range from just a line or two all the way up to full-fledged programs.
Yes, You can learn how to write BASH shell scripts!
There are only a few hard and fast rules to shell scripting. Once you learn them, you can feel free to experiment and come up with new ways to do wherever it is you’re trying to do. BASH is very flexible. There are often many ways to do the same thing and you should not let anyone tell you’re doing it “wrong” if your scripts run and do what you need them to. Keep in mind, we all have to start somewhere.
THE BASIC SCRIPT
You will need to use a plain text editor to write your scripts. Every Linux distribution offers one in the menu and there are excellent text editors that run in the terminal. What you use is entirely up to you. A script starts as a plain text file. A basic script looks like this:
The first line is called the “sha-bang” or “hash-bang” and it tells the system that this is a script and that it needs to use the BASH shell to run the following commands. Any line that starts with a # is treated as a comment and will not be considered as executable code by the shell. You can also place a # after a command to add a comment telling what that command is doing.
MAKING A TEXT FILE RUN LIKE A PROGRAM
Once the script is written, it should be saved with a unique filename. You may elect to add .sh as an extension but it is not necessary. I usually only add the .sh to scripts that will not be permanently installed as programs on my machine. The file you saved to must now be made executable before you can test it. You do this with the chmod command in one of two ways:
chmod +x script.sh
chmod 755 script.sh
TESTING THE SCRIPT
Now that the proper permissions are set, the script can be run form a terminal. You will have to use ./ to tell the shel to run it from your working directory, be cause it hasn’t yet been moved to some place in your executable path:
If the script runs as you hopes it would then congratulations, you’ve just written your first script! Sometimes it doesn’t work that easily and you’ll get errors. If you can’t figure out where the errors are coming from, you can activate tracing in your script to get more detailed output telling you just what it’s doing. Open it in an editor and add -x to your sha-bang:
Don’t forget to remove the -x when you’re done fixing the problem.
WHERE TO PUT YOUR SCRIPTS.
You’ll want to move your script to someplace with in your path statement so you can run it like any other installed program on your system. There are two preferred places for scripts you write, programs you compile yourself and any other electable that is not handles by your distribution’s package manager:
Ubuntu and many other Linux distributions will discover that you have created a directory in your home directory called ‘bin’ and add it to the path statement at startup. Executables that you put in ~/bin will be only available to you and you cannot run them with sudo in front of them.
Any executable that you put in /usr/local/bin will be available to all users and can be run with sudo privileges.
If your system is not automatically seeing the newly created ~/bin directory simply open or create a file called .bashrc in your home directory and add these lines to it:
# set PATH so it includes user’s private bin if it exists
test -d “$HOME/bin” && PATH=”$HOME/bin:$PATH”
Restart the system to make this take effect.
Now your script should be ready to run just by typing its name into the command line.
THINGS TO REMEMBER
* BASH is very well documented. Don’t hesitate to search the web for help.
* Always use absolute paths in a script. Ex. /full/path/to/file
* Write detailed comments so you’ll know what you did later on.
* Save a copy of everything you write so you can draw from it later.