Introduction to Shell Script (1)

Source: Internet
Author: User

Preface
In DOS, you may be engaged in some routine overwriting work. At this time, you will write these overwriting commands into batch files, you only need to execute this batch of files to execute these commands. You will ask if there is batch processing in UNIX. The answer is yes. In UNIX, not only is it like DOS batch processing, but it is more powerful and complex than DOS. It is already comparable to general high-level languages. In UNIX, it is not called a batch file, but a Shell Script.

Generally, Shell scripts have the same status as other executable files or commands, except that Shell scripts are stored as text files rather than binary files. When executing a Shell Script, a program must convert its content into a single command for execution, and this program is actually a Shell, this is why we call it Shell Script ). Different Shell scripts are basically different, so we cannot use B shell to execute scripts written to A shell. In UNIX, we usually use the Bourne Shell and C Shell, so this article introduces the writing of these two types of scripts.

Set the text file to an executable Shell Script

If we have already written a Script, how can we set it to an executable file? Because a Script is an executable file, the access permission must be set to executable. You can use the following command to change the access permission:
Chmod u + x filename can only be executed by yourself, but cannot be executed by others
Chmod ug + x filename can be executed only by yourself and the same group, but not by others.
Chmod + x filename can be executed by all users
But how do we specify the Shell to explain the written Script? The following describes how to specify the parameters:
1. If the first non-blank character of the Script is not "#", it uses the Bourne Shell.
2. If the first non-blank character of the Script is "#", but not "#! ", It will use C Shell.
3. If the Script uses "#! ", Then "#! "What is written in the back is the Shell used, and the whole path name should be pointed out.
We recommend that you specify the Shell in the third way to ensure that what is executed is what you want. The path name of the Bourne Shell is/bin/sh, while that of the C Shell is/bin/csh.

1. Use the Bourne Shell
Certificate ---------- certificate ---------- Certificate
│ Echo enter filename │ #! /Bin/sh │
│. │ Or │. │
│. │. │
│. │. │
Certificate ---------- certificate ---------- Certificate
2. Use C Shell
Certificate ---------- certificate ---------- Certificate
│ # C Shell Script │ #! /Bin/csh │
│. │. │
│. │. │
│. │. │
Certificate ---------- certificate ---------- Certificate
3. Use/etc/perl
Certificate ---------- Certificate
│ #! /Etc/perl │
│. │
│. │
│. │
Certificate ---------- Certificate
In addition to specifying the Shell used in the Script, you can also forcibly specify it in the Command column. For example, if you want to use C Shell to execute a Script, you can run this command: csh filename
In this case, the access to the Script is not necessarily executable, and the Shell specified in the Script is invalid. The details will be discussed later.

Basic structure and concept of Script
The Script is a unit of action. The Script we write will be divided into one row for execution. Each line can be commands, annotations, or process control commands. If a row has not been completed, you can add "\" at the end of the row. At this time, the content of the next row will be received at the end of the row and become the same row, as shown below:
Certificate ----------- Certificate
│ Echo The message is \ │
│ Too long so we have \ │
│ To split it into \ │
│ Several lines │
Certificate ----------- Certificate
When "#" appears in the Script, the same line of text after it is annotated, and Shell will not translate it. The method for executing a command in a Script is the same as that in the Command column. You can execute the command in the foreground or background. Some environment variables need to be set when executing the command. The Process Control of a Script is similar to that of a general high-level language.
Sub-program. These make the Script more powerful. To achieve the same effect as a higher-level language, we can also set variables in the Script, so that the Script becomes a higher-level language.


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.