Linux (7) _ shell script programming, __shell Script Programming

Source: Internet
Author: User
Tags processing text

Linux (7) _ shell script programming, __shell Script Programming

I. What is a shell script?

In addition to a command interpreter, shell is also a programming language. A program written in shell is similar to a batch processing program in DOS.

It is an interface between the user and the operating system.

The shell script language is very good at processing text data. Because the configuration files in linux are all text files, the shell script language

It has played a huge role in linux system management.

 

Ii. Why do I learn shell scripts?

  • Automated Management
  • Monitoring Management
  • Log Data Processing
  • Automatic Data Backup

 

III. Basic syntax

The general file is #! It starts with/bin/bash, which indicates that the file uses the bash syntax and does not have to be set. However, this is a standard.

Shell scripts are in the unit of action. Note # variable supports string and Integer Variables

Vi editingShell script file

Call method:

Sh script-file

You can add the-x option to view the script execution process, which is conducive to debugging.

./Script-file

To execute a shell script in A./way, you must set the File Permission chmod + x filename to an executable file.

The. binfile must be called ./.

 

Shell variable

Name = string

The string value of the name variable is assigned with no space on either side. It is considered as a command.

Example:

V = centos

Echo = $ v ($ indicates a variable)

 

Date command:

Date

Date + % H: % M: % S

 

Shell expression:

Simple arithmetic operation:

B = $(5*5 + 5-3/2 ))
Echo $ B
29
In linux shell, we can use $ () to put the expression in brackets to achieve the operation function.

Or use $.

String processing

 

Interaction with users:

Read command

P is the abbreviation of prompt, that is, the prompt information is provided by this toggle, for example
Read-p 'Please input you choice: \ n' choice
After you press enter, the screen will be displayed:
Please input you choice:
Then the input will be saved to the choice variable.

Judgment:

Test judgment

Example:

Filename =/home/hello. java

Test-f $ filename & echo 'exit '| echo 'not exit'

[] Judgment

 

In particular, note that there must be spaces between variable constants and symbols.

 

Judgment

 

Example:

If [-f $ filename]; then

Echo 'The file is exist'

Fi

 

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.