search
Homephp教程PHP开发How to use parentheses in Shell

How to use parentheses in Shell

Nov 23, 2016 pm 05:41 PM
shell

Brackets

Brackets are divided into single brackets [] and double brackets [[]]. Square brackets are generally used in expression judgment, and parentheses can also be used in expressions, which will be mentioned later.

Single square brackets []

A=5
B=3
[ $A -eq $B ]    判断A是否等于B
[ ${A} -eq ${B} ]  作用同上
[ "${A}" -eq "${B}" ]  作用同上

     

A=ABC
[ $A == "ABC" ]   或写成  [ ${A} == "ABC" ]   或写成  [ "${A}" == "ABC" ]
[ $A \> "ABC" ]

   

[ -d ./aaa.sh -a -e ./bbb ]  测试aaa.sh是否为目录并且bbb是否存在,如果都符合则返回真
[ -d ./aaa.sh -o -e ./bbb ]  测试aaa.sh是否为目录并且bbb是否存在,满足一个条件则返回真

 

We can see some rules for using single square brackets from the above:

There must be a space between the variable and the square bracket

variable There must also be a space between the operator and the operator

Variables can be referenced directly with $, or they can be referenced with ${} and "${}". Although ${} can be used directly, it is recommended to use "${} ", or use $ directly to quote variables

Strings should be enclosed in double quotes

String comparison generally uses == to test whether the strings are the same, and use for size comparison, in a single To compare characters in parentheses for greater than and less than, use transfer > and <. the comparison principle is to compare ascii.>

In addition, you can also use logical operators like -a and -o in single square brackets


Double square brackets [[]]:

A=5
B=3
[[ $A -eq $B ]]    判断A是否等于B
[[ ${A} -eq ${B} ]]  作用同上
[[ "${A}" -eq "${B}" ]]  作用同上

                                                                   

A=ABC
[[ $A == "ABC" ]]   或写成  [[ ${A} == "ABC" ]]   或写成  [[ "${A}" == "ABC" ]] 或写成 [[ $A == ABC ]]
[[ $A > "ABC" ]]

The rules for using double square brackets are similar to the rules for using single square brackets

The greater-than-less comparison of strings between double square brackets does not need to be escaped, and the strings can be quoted without double quotes

Can be used inside double square brackets &&, || are used as logical operators

You can also do fuzzy matching inside double brackets, use =~Expression 2 can be a regular expression

[[ -d ./aaa.sh && -e ./bbb ]]  测试aaa.sh是否为目录并且bbb是否存在,如果都符合则返回真
[[ -d ./aaa.sh || -e ./bbb ]]  测试aaa.sh是否为目录并且bbb是否存在,满足一个条件则返回真

A=ABC
[[ $A =~ "A" ]]   做模式匹配,这个表达式为真
或写成  [[ ${A} =~ "A" ]]  或写成  [[ "$[A]" =~ "A" ]]
[[ $A = A* ]] 表达式为真  [[ $A = B* ]] 表达式为假

Single brackets

Brackets

Single parentheses used alone represent a command group, and the commands in the brackets will be executed sequentially through a subshell

[ 表达式1 -a 表达式2 ] 等效于 [[ 表达式1 && 表达式2 ]] 等效于 [ 表达式1 ] && [ 表达式2 ] 等效于 [[ 表达式1 ]] && [[ 表达式2 ]]

The most common usage is to use it in command substitution. Backticks can also be used for command substitution. ``

[ 表达式1 -o 表达式2 ] 等效于 [[ 表达式1 || 表达式2 ]] 等效于 [ 表达式1 ] || [ 表达式2 ] 等效于 [[ 表达式1 ]] || [[ 表达式2 ]]

Double parentheses

are commonly used in arithmetic operations and also used in for loops

(ls ./;echo "hello";mkdir -p /tmp/ccc)

For statement

1 to 10 Loop execution

echo "当前目录是:$(pwd)"
echo "当前目录是:`pwd`"

While statement form

1 to 10 loop

A=$((5+3))
B=$(($A+4))


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools