RHCE path -- shell programming

Source: Internet
Author: User
What is shell script? Shell is a tool that allows us to communicate with the system on the command line interface. shell scripts are programs written using shell functions, which use plain text files. Write some shell syntaxes and commands in it, and use regular expressions, pipeline commands, and data stream redirection to achieve our goal. Why should we learn shell scripts? If Linux is only usable for you, it doesn't matter if you don't want to learn shell scripts. However, if you want to understand Linux's logic, shell

What is shell script? Shell is a tool that allows us to communicate with the system on the command line interface. shell scripts are programs written using shell functions, which use plain text files. Write some shell syntaxes and commands in it, and use regular expressions, pipeline commands, and data stream redirection to achieve our goal.

Why should we learn shell scripts? If Linux is only usable for you, it doesn't matter if you don't want to learn shell scripts. However, if you want to find out the logic of Linux, shell scripts are not unknown. Because:

1. Important Basis for automated management:

Needless to say, you know that managing a host is not a simple task. Daily tasks include querying login files, tracking traffic, monitoring user Usage Status, host device status, host software update query, and resolving temporary requirements of other users, these tasks can be handled manually, or a simple program can be compiled to automatically process and analyze the tasks on a daily basis, so that you will be notified of any problems. Of course, the system will work better automatically, this requires shell scripts for use.

2. Important Tasks of tracking and management systems:

The Service start interface of the Linux system, in/etc/init. in the d directory, all files are scripts. In addition, the boot process also uses shell scripts to search system-related settings, and then substitute the settings parameters of various services. Therefore, to learn more about Linux, you must learn scripts.

Of course, there are many other functions and functions. For example:

SHELL is used for simple Host Intrusion Detection, continuous command simplification, simple data processing, cross-platform support and shortening of learning processes. The following describes how to use shell.

As mentioned above, shell is a plain text file. Note the following when writing a shell.

1. Multiple blank spaces between commands and parameters will be ignored

2. blank lines (tab) will also be ignored

3. If you read an enter (CR), try to execute this line of command

4. If a row contains too many contents, you can use \ enter to expand the next row.

5. # can be used as a comment. Any content appended with # Will be ignored as comments

In this way, the program we write in the script will be executed in one row. Assuming the file name is example. sh, how can we execute this file?

The method is as follows:

1. Add the permission to read and execute example. sh, and then use./example. sh for execution.

2. Run bash example. sh directly.

The following is a simple shell script.

#! /Bin/bash
Echo-e "hello, how are you? "
Exit 0

In this simple way, a simple shell script is written. The first line is required, indicating the name used by the script. Because bash is used, #! is required #! /Bin/bash declare the syntax in this file using the bash syntax. This sentence is very important. Without this sentence, the program may not be able to run.

The main program is the second line, that is, the echo, that is, to display a sentence.

The third line tells the execution result. In bash knowledge, can a command be successfully executed? $? Variable to observe, we can also use the exit command to interrupt the program and return a value to the system. In this example, exit 0 indicates to exit the script and return a 0 value to the system, therefore, after the script is executed, if echo $? You can get the value 0, use the exit n function, and customize the error information to make the program more flexible.

Shell is mainly used together with regular expressions, grep, and pipelines.

On the basis of learning expressions, shell can quickly get started

Related Article

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.