Introduction to bash shell programming

Source: Internet
Author: User

Like other shells in Linux, Bourne Again shell is not only an excellent command-line SHell, but also a scripting language. Shell scripting allows you to take full advantage of shell functions and automate multiple tasks that require many commands. There are many shell programs on your Linux machine. If you are interested in learning how they work, or modifying them, the basic element is that you must understand bash syntax and semantics. In addition, to understand the bash language, you can write your own programs to complete what you want to do.
Programming or scripting?
New users who are new to programming are often confused about the differences between programming and scripting languages. The Programming language is generally more powerful and faster than the Scripting language. For example, C, C ++, and Java are both Programming languages. The Programming language usually starts from the source code (a set of text files that contain how the final program executes) and is compiled (established) into an executable file. This executable file cannot be easily transplanted to different operating systems. For example, if you have written a C program on Linux, you will not be able to execute this C program on Windows 98. To do this, you must recompile the original program code under Windows 98. The Scripting language also starts from the source code, but does not need to be compiled into executable files. Instead, a literal interpreter reads the commands in the source code file and then executes each command. Unfortunately, the literal translation program is generally slower than the compiled program, because the literal translation must read every instruction. The main advantage is that you can easily convert source code files to any operating system and immediately execute them in literal translation. Bash is a scripting language. It is good for writing small programs, but if you plan to develop large applications, the programming language may be helpful to you. Other scripting languages include Perl, Lisp, and Tcl.
What do you need to know? /What do you need to know?
To write your own shell program, you need to know the most basic Linux commands. For example, you should know how to copy, move, and generate new files. One more thing you must know is how to use the document editing program. In Linux, there are three main text file editing programs-vi, emacs, and pico. If you are not familiar with vi or emacs, you can use pico or some other easy-to-use text file editing programs.
WARNING !!! /Warning !!!
Don't exercise as root! Any situation may happen! If you encounter an unexpected error when writing a program and the system crashes, I am not responsible. You have been warned! Be sure to use a general user account without the root permission. You can even generate a new user dedicated to practicing shell program design. In this way, the worst case is that the user's directory is gone.
Your first bash program/FIRST BASH PROGRAM
Our first program will be a typical "Hello World" program. Don't doubt, if you already have programming experience, you have to have a headache now. However, this is a tradition. How can I change it? The "Hello World" program only prints the word "Hello World" to the screen. Open your text file editing program and enter the following content:
#! /Bin/bash
Echo "Hello World"
The first line is to tell Linux to execute this program using the bash literal interpreter. In this example, bash is in the/bin directory. If bash is in different directories on your system, make appropriate changes to this line. In addition, it should be particularly noted that this literal interpreter is very important, so please make sure that the directory is correct or not, it tells Linux which literal interpreter is used to execute the commands in the program. The next step is to archive the program. Call it hello. sh. After completion, you need to make this program executable:
Xconsole $ chmod 700./hello. sh
If you do not know how to change the permissions of an archive, refer to the chmod manual (manual page ). Once the change is complete, you only need to enter the program name to execute:
Xconsole $./hello. sh
Hello World
This is the light! This is the light! Your first program is complete! It's really boring, it's useless, but everyone started like this. Just remember this program. Write a program code, save it as an archive, and use chmod to make it executable.
COMMANDS, COMMANDS, COMMANDS/command, command, command
What will your first program do? Print the word "Hello World" to the screen. But what should we do? Instructions. The only line of code written in the program is echo "Hello World ". Okay, what is a command? Echo. The echo program carries a parameter and prints it to the screen.
A parameter is anything after the name of the program you typed. In this case, "Hello World" is a parameter that is passed into the echo command. If you enter such command ls/home/root,/home/root is a parameter for ls. What does this mean? If you have a program that prints parameters to the screen, you do not need to use the echo program. We assume that a program called foo can input a parameter, a string, and print it to the screen. We can rewrite our program similarly:
#! /Bin/bash
Foo "Hello World"
Archive it and change the access mode (chmod), and then execute:
Xconsole $./hello
Hello World
The results are exactly the same. Is there a unique program code? No. Can you really write any program? Unless you are the author of The echo program. What you do is to put the echo program in your shell program and give a parameter. In the real world, another option of echo commands is printf commands. Printf provides a lot of control. If you are familiar with the C language program design, it will be clear. In fact, to get identical results, you don't have to write a shell program:
Xconsole $ echo "Hello World"
Hello World
Bash shell programming provides a variety of control methods and is easy to learn. As you just saw, you used Linux commands to write your shell program. Your shell program aggregates other programs to execute specific tasks.
A more useful program/more useful programs
We will write a program to move all files to a directory, delete the Directory and its content, and then generate the directory. This can be done by the following commands:
Xconsole $ mkdir trash
Xconsole $ mv * trash
Xconsole $ rm-rf trash
Xconsole $ mkdir trash
You do not need to input all the commands on the negotiated shell. Instead, you can write a shell program:
#! /Bin/bash
Mkdir trash
Mv * trash
Rm-rf trash
Mkdir trash
Echo "Deleted all files! "
Use it as clean. sh archive. Now all you need to do is execute clean. sh, which moves all files to a directory, deletes them, generates a directory, and prints a message indicating that it has successfully deleted all files. So remember, if you find that you need to do something one by one, and then do it three times, consider using a shell program for automatic execution.
COMMENTS/Annotation
Annotations make your program clearer, without affecting the output results of the program. The purpose is to help you understand the program. For all the annotations in bash, the first character uses the hash symbol: "#", except the first line (#! /Bin/bash. The first line is not an annotation. After the first line, any line starting with "#" is annotated. See the following program snippets:
#! /Bin/bash
# This program records from 1 to 10:
For I in 1 2 3 4 5 6 7 8 9 10; do
Echo $ I
Done
Even if you don't know the bash program design, you can immediately know what the above program is doing because of the annotation relationship. Annotations are a good habit. You will find it easier to annotate your program in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.