Get the App
SLTechnology News&Howtos  ›  Servers  › 

Linux shell basic Grammar Amur2

Shulou Source: shulou.com Published: 2022-06-02 19:28:54 09月10日 Update

Amur1 talked about some basic grammar, and A2 continues what it said last time.

Shell is a programming language, and naturally there are structured representations. The common programming language structure is judgment and loop. The judgment in shell can be divided into three categories: numerical judgment, character judgment and file judgment.

Several forms of judgment

If command

Then

Command

FiIf command

Then

Command

Else

Command

Fi

If command

Then

Command

Elif command

Then

Command

Else

Command

Fi

(1) numerical judgment

Example: simple guessing of numbers

(2) character judgment

Example: determine the input option

(3) document judgment

Example: check whether the destination path exists before creating the log file

#! / bin/bash

If [!-d / mnt/123testlog/2019-08-31]

Then

Mkdir-p / mnt/123testlog/2019-09-31

Fi

Still need to pay attention to

The usage of If test xxxx

The parameters are worth judging and dealing with.

Compound condition judgment

If [] & & []

If [] | | []

Double parentheses

-- double parentheses (advanced features of mathematical expressions)

-- double parentheses (string advanced features)

It is more complicated to use if structure in multi-choice judgment, and it is better to use case.

Case

Case xxx in

P1)

Cmd

P2)

Cmd

*)

Default cmd

Esac

You can use case when dealing with the input parameters of the script, and then shift the parameters

Input can also be processed as follows, and the usage of getopt and getopts will be described in more detail later.

Getopt

Getopts

Read

Tags: Processing parameters characters instances files structures inputs advanced functions parentheses numbers programming languages languages structures programming syntax tedious content both sides parentheses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple macOS Shulou Tech Info vpn