Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Shell Grammar

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of the sample analysis of Shell grammar. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Escape character:

When parsing the text, the double quotation marks parse the string, and the single quotation marks keep the literal meaning.

Echo $? The last process exit code of this bash

Double quotation marks are used to maintain the literal values of all characters within quotation marks (carriage returns are no exception), except in the following cases:

Conditional test: exit code 0 indicates success, non-0 indicates failure

For the value statement of × ×

-gt means greater than that is great

-ge means greater than or equal to great equal

-eq stands for equal equal

-lt means less than that is little

-le means less than or equal to

Read val means to read from standard input to val

For string comparison: directly use =, (recommended), or! =

You can choose $str1==$str2, or "X$str1" = = "X$str2" (it is recommended to prevent direct comparison when $str1 is empty, X is any character)

Single quotation marks cannot be used here. Single quotes only take literal meaning.

Running result:

Running result:

-d to determine whether it exists and is a directory

-f to determine whether it exists and is an ordinary file

Dir exists and is a directory, test does not exist, test.sh exists as a normal file

Comparing strings is best written as follows:

If-elseif statements: branching

$1 is an automatic variable that is created when the script is generated: the first parameter, test.sh.

Serial number container: vector, array subscript starts at 0

Associated number container: script container (map: use string as subscript)

Reprint:

What is the difference between [[]] and [] in shell?

1. Conceptually,

"[" is the keyword, and many shell (such as ash bsh) do not support this approach. Ksh, bash (it is said that support for [support] has been introduced since 2.02, etc.

"[" is a command that is equivalent to test and is supported by most shell. In most modern sh implementations, "[" and "test" are builtin commands.

two。 Same: both support arithmetic comparison and string comparison expressions (the specific use may be a little different)

(1) "- gt", "- lt" are arithmetic comparison operators that are used to compare the size of integers.

(2) ">", "

True

2.2 usage of "["

$`2-gt 10` & & echo true | | echo false

False

$`2-lt 10` & & echo true | | echo false

True

Still compare by string

$[2

< 10 ]]&&echo true||echo false false $ [[ 2 >

10]] & & echo true | | echo false

True

3. Same: both support simple pattern matching

Pattern matching here is much simpler, similar to the extension rules of wildcards for file names. Also note that the pattern at the right end of the equal sign cannot be enclosed in quotation marks, and the use of references turns off the special functions of some metacharacters

3.1 "[" usage

$[test = test] & & echo true | | echo false

True

$[test = tweet] & & echo true | | echo false

True

$[test = t..] & & echo true | | echo false # not match.

False

$[test = "tweeter"] & & echo true | | echo false # alert: don't quote the pattern, closed with reference? Special function of

False

3.2 usage of "["

$[[test = test]] & & echo true | | echo false # normal compare

True

$[[test = tweet]] & & echo true | | echo false # pattern match.

True

$[[test = t..]] & & echo true | | echo false # not match.

False

$[[test = tweeter]] & & echo true | | echo false

True

$[[test = "tweeter"] & & echo true | | echo false # alert: don't quote the pattern, closed with reference? Special function of

False

4. Differences

4.1 Logic and and logic OR

(1) "[": logic and: "- a"; logic or: "- o"

(2) "[": logic and: "&"; logic or: "| |"

$[1

< 2 && b >

A]] & & echo true | | echo false

True

$[1

< 2 -a b >

A]] & & echo true | | echo false

Bash: syntax error in conditional expression

Bash: syntax error near `- a'

$[1

< 2 -a b >

A] & & echo true | | echo false

True

$[1

< 2 && b >

A] & & echo true | | echo false # wrong syntax

Bash: [: missing `]'

False

$[1

< 2 \&\& b >

A] & & echo true | | echo false # aslo wrong

Bash: [: &: binary operator expected

False

4.2 Command line arguments

(1) [...] is a shell command, so the expression in it should be its command line argument, so the string comparison operator ">" and "

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report