Linux (vii) __shell script programming

Source: Internet
Author: User

First, what is a shell script

The shell is a programming language in addition to the command interpreter, and a program written in Shell resembles a DOS batch program.

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

The Shell scripting language is very good at handling text-type data, because the configuration files in Linux are text files, so the Shell scripting language

Has played a huge role in managing Linux systems.

Second, why learn shell script

    • Automated management
    • Monitoring management
    • Log data processing
    • Automatic data backup

Three, basic grammar

The generic file begins with #!/bin/bash, which indicates that the file uses bash syntax and is not set, but this is a specification.

The shell script is in the behavior unit. Note # variables support string variables and integer variables

VI Edit shell script file

Call Mode:

SH script-file

You can add the-X option to view the execution of this script, which facilitates debugging.

./Script-file

Execute shell scripts in./mode you must set file permissions chmod +x filename into an executable file

The. bin file will be used./Call

Shell variables

Name=string

The name variable string is assigned a value with no spaces on either side and is treated as a command

Cases:

V=centos

echo= $v ($ flag a variable)

Date command:

Date

Date +%h:%m:%s

Shell expression:

Simple arithmetic:

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

Or you can use $[].

Processing of strings

and user interaction:

Read command

P is the abbreviation for prompt, which is the cue message given by this switch item, such as
Read-p ' please input choice:\n ' Choice
The screen will display when you enter:
Please input choice:
Then the stuff you type will be saved in the choice variable.

Judge:

Test judgment

Cases:

Filename=/home/hello.java

Test-f $filename && echo ' Exit ' | | echo ' Not exit '

[] Judging

In particular, note the first, there must be a space between the variable constant and the symbol.

Judge

Cases:

If [-F $filename];then

Echo ' The file is exist '

Fi

Linux (vii) __shell script programming

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.