Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

linux Shell Script

Even if you think you're not a programmer, Linux has some features to make your life a bit easier. Any time you have a repetitive task that involves entering Linux commands or changing the contents of a file, you should think about automating it with a program.
If you've ever written a word processor macro or a spreadsheet formula, you're a programmer. By taking advantage of Linux's built-in programming features, you can automate repetitive tasks and build simple interactive applications without a degree in computer science.

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 Programming

The Bash shell is your main port of entry to Linux, since the 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 its 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.



This post first appeared on Linux Operating System, please read the originial post: here

Share the post

linux Shell Script

×

Subscribe to Linux Operating System

Get updates delivered right to your inbox!

Thank you for your subscription

×