This section covers basic Shell script programming and introduces you to other, more powerful programming tools available in Linux, such as Perl and the C and C++ programming languages.
Basic Shell Programmingnce
shell interprets everything you enter on the command line before passing it along to the operating system for execution. But in addition to providing the niceties of command resolution, wildcard handling, and piping, Bash has it
The Bash shell is your main port of entry to Linux, si
s own powerful built-in programming language.
A shell script is a program written for Bash. The simplest shell script is a sequence of Linux commands, but when you add the power of variables and flow control, you can do a lot more with it. Shell scripts are similar to DOS batch files (those files that end in .bat), but shell scripts are more powerful and actually easier to create.
Shell scripts are interpreted, which means that the shell reads each line and acts on it immediately. This process differs from that of a formal programming language like C or C++, where the program is compiled and optimized for faster execution. So there's a tradeoff--it's easier to create quick little shell scripts, but if the task at hand requires serious number crunching or complicated logic, a compiled language is better.
Note: All of the shell script syntax and examples in this section will work in both the Bash and Korn (pdksh) shells. The C shell (tcsh) has subtle differences in many areas, so scripts written for one shell may not work in another. If you decide to use the C shell instead of Bash, use the man tcsh command for more information on writing shell scripts for that environment.
Creating a Shell Script
To create a shell script, use a text editor and enter your Linux commands as if you were typing them at the command prompt. For example, try this:
cd /tmp
echo "Removing temp files..."
ls -al
rm junk*
If you save those four lines in a file named deltemp, you will have a simple shell script that automates the process of switching to the /tmp directory, listing all the files there, and deleting the ones that start with the word junk.