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

What are the basic knowledge of shell programming

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

Share

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

This article mainly introduces the basic knowledge of shell programming, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

1. Introduction to file types

Linux system mainly includes the following seven file types: d directory-ordinary file l symbolic link s socket file b block device file c character device file p named pipe file

2. Regular expressions

Regular expressions are very important in shell programming.

When extracting or filtering text from a file or command output. Regular expressions (RE) are collections of special or not-so-special string patterns.

Basic metacharacter set:

^Matches only the beginning of the line.

$matches only the end of the line.

* A single character followed by a * matches zero or more of these single characters.

[] matches the characters in [], either a single character or a sequence of characters. A range within [] can be represented by-, e.g.[1-5] is equivalent to [1, 2, 3, 4, 5].

Masks the special meaning of a metacharacter, such as $for the character $, rather than matching the end of a line.

. Matches any single character.

pattern{n} Number of occurrences of matching pattern n

pattern{n,}m matches the number of occurrences of pattern, but indicates a minimum of n

pattern{n,m} matching pattern occurs between n and m (n, m is 0-255)

Common examples:

Show executable files: ls -l| grep …x... x.. x

Show only folders: ls -l| grep ^d

Match all blank lines: ^$

Match all words: [A-Z a-z]*

Matches any non-alphabetic character: [^A-Z a-z]

Eight character lines: ^…….$ (8...)

For more information, see Regular Expressions for details.

3. Character class description

List of commonly used available character classes:

[:alnum:] alphanumeric [a-z A-Z 0-9]

[:alpha:] letters [a-z A-Z]

[:blank:] Space or tab key

[:cntrl:] Any control character

[:digit:] digit [0-9]

[:graph:] Any visible character (no spaces)

[:lower:] lowercase [a-z]

[:print:] non-control character

[:punct:] punctuation character

[:space:] Space

[:upper:] capital [A-Z]

[:xdigit:] hexadecimal digit [0-9 a-f A-F]

Use character classes to better accommodate non-English locales (including certain required accented characters, etc.).

4. Quotation type

There are four types of references in the shell:

" " Double quotes

' ' single quote

` ` Back quotes

backslash

Description:

" " can refer to any character or string except $,`,,, variables in " " can display variable values normally.

' ' is similar to " " except that the shell ignores any reference values.

For example: GIRL='girl'

echo "The '$GIRL' did well"

The 'girl' did well

Description:

` ` Used to set the output of system commands to variables, the shell will take the contents of ` ` as a system command and execute the prime.

For example, echo `date` prints the current system time.

Description:

Characters used to mask special meanings: & * + ^ $ ` "| ?

For example: expr 12 * 12 will output 144

5. Several modes of variable setting

Different modes of variable setting:

valiable_name=value Set the actual value to variable_name

valiable_name+value If variable_name is set, resets its value

valiable_name:? value If variable_name is not set, an undefined user error message is displayed first.

valiable_name? value If variable_name is not set, a system error message is displayed

valiable_name:=value If variable_name is not set, set its value

valiable_name-value Same as above, but the value is not set to variable_name

Note: The flexible application of the above modes will greatly enhance programming skills.

6. Conditional testing

The test command tests strings, file status, and numbers, expr tests, and performs numeric output.

Test format: test condition or [ condition ](special attention should be paid to both sides of the condition must have a space, otherwise an error will be reported), test command returns 0 means success.

The following describes the three types of test:

File status test (common)

-d Test whether folder

-f Test whether general file

-L tests whether files are linked

-r Test whether the file is readable

-w Test whether the file is writable

-x test file executable

-s Test file is not empty

1. String test

Five formats: test "string"

test string_operator "string"

test "string" string_operator "string"

[ string_operator "string" ]

[ "string" string_operator "string" ]

where string_operator can be:

= Two strings are equal

!= Two strings are unequal

-z empty string

-n Non-empty string

2. Numerical test

Two formats: "number" number_operator "number"

[ "number" number_operator "number" ]

where: number_operator can be: -eq, -ne, -gt, -lt, -ge

For example: NUMBER=130

[ "990" -le "995" -a "NUMBER" -gt "133" ]

(where-a represents the antecedent result phase "and")

The expr command is generally used for integer values, but can also be used for strings.

Format: expr srtoken operator argument

Exp r 10 + 10

expr 10 ^ 2 (10 squared)

expr $value + 10

Increment Count--expr The most basic usage of expr in loops

For example: LOOP=0

LOOP=`expr $LOOP + 1`

Pattern matching: Counts the number of characters in a string by specifying the colon option

For example: value=account.doc

expr $value : `(.*). doc`

Output account

7. Order of execution

&& execute one command successfully before executing the next

|| One command fails and then another is executed.

( ) Executes a set of commands in the current shell (format: (Command 1; Command 2;...))

{ } Same as ( )

For example: comet mouth_end|| ( echo "hello" | mail dave ;exit )

If there is no ( ), the shell will execute the last command (exit) directly.

In shell programming, the order in which commands are executed sometimes determines the outcome of a program's execution, while && and|| The most widely used.

8. Script debugging

script debugging

The most useful tool for debugging scripts is the echo command, which prints information about variables or actions at any time to help locate errors.

You can also print the final status ($?) using Command to determine whether the command is successful, then pay attention to is to execute the command to be tested immediately after

Output $?, Otherwise $? will change.

The Set command can also be used to aid script testing:

Set -n Read command but not execute

Set -v Displays all rows read

Set -x Displays all commands and their parameters

To turn off the set option, simply replace-with +.

Thank you for reading this article carefully. I hope that Xiaobian's sharing of "What are the basics of shell programming?" This article is helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

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