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 some special symbols and functions common in shell scripts?

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

Share

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

This article mainly explains "what are some common special symbols and functions in shell scripts". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are some common special symbols and functions in shell scripts"?

1. {} curly braces:

Usage 1: wildcard extension

Eg: ls my_ {finger,toe} s

This command is equivalent to a combination of the following commands:

Ls my_fingers my_toes

Eg: mkdir {userA,userB,userC}-{home,bin,data}

We will get userA-home, userA-bin, userA-data, userB-home, userB-bin,userB-data,userC-home, userC-bin, userC-data, these directories.

Usage 2: it can be used to construct sentence blocks, and the sentences are separated by carriage return. If you want to use multiple statements in some places where a single statement is used (such as in an AND or OR list), you can construct a statement block by enclosing them in curly braces {}.

Eg:

{

Grep-v "$cdcatnum" $strack_file > $temp_file

Cat $temp_file > $strack_file

Echo

Cat-n file1

} (Note: the four commands in the above curly braces are enough to form a statement block)

Usage 3: parameter expansion

${name:-default} uses a default value (usually null) to replace the empty or unassigned variable name

${name:=default} uses the specified value to replace the empty or unassigned variable name

${name:?message} if the variable is empty or unassigned, an error message is displayed and the execution of the script is aborted and the exit code 1 is returned.

${# name} gives the length of the name

${name%word} deletes the smallest part that matches the word from the tail of the name, and then returns the rest

${name%%word} deletes the longest part that matches the word from the tail of the name and returns the rest

${name#word} deletes the smallest part that matches word from the header of name, and then returns the rest

${name##word} deletes the longest part that matches word from the header of name, and then returns the rest

(note: name is the variable name and word is the string to match)

Usage 3 is useful when dealing with strings and unknown variables.

2. [] brackets:

Usage 1: wildcard extension:

Allow matching of any single character in formula parentheses

Eg: ls / [eh] [to] [cm] *

Equivalent to executing ls / etc / home (if there is a / eom directory, it is equivalent to executing ls / etc / home / eom)

Note: cannot be extended under the mkdir command

Usage 2: for conditional judgment symbols:

The [] symbol can be understood as a soft link to the test command, so its usage can refer fully to test and replace the test position with [.

Eg: if ["$?"! = 0] is equivalent to if test "$?"! = 0

Then echo "Executes error"

3. `command` backquotes: `command` has the same meaning as $(command), and both return the result of the current command execution

Eg: #! / bin/sh

For file in $(ls f*.sh); do

Lpr $file

Done

Exit 0

This example implements the extension f*.sh to give the names of all files that match the pattern.

4. String' single quotation marks and "string" double quotation marks

Double quotation marks: if you want to add spaces to defined variables, you must use single or double quotation marks

The difference between single quotation marks and double quotation marks is that double quotation marks escape special characters while single quotation marks do not escape special characters.

Eg: $heyyou=home

$echo'$heyyou'

$$heyyou ($is not escaped)

Eg: $heyyou=home

$echo "$heyyou"

$home (obviously, $escapes the value of the heyyou variable)

5. $# its function is to tell you the total number of referenced variables

$its function is to tell you the process number of the shell script

$* displays all the parameters passed by the script in a single string. Equivalent to $1 $2 $3... .

$@ is basically similar to $* (see serial number 7), but with some differences in array assignment

$? Exit code of the previous command

$- displays the current options used by shell

$! The ID number of the last process running in the background.

6. $((...)) Syntax: evaluating expressions in parentheses

Eg:

#! / bin/sh

Xerox 0

Hile ["$x"-ne 10]; do

Echo $x

Xx $(($xx 1))

Done

Exit 0

7. Citation of several special parameter variables in shell

$1, $2, $3. ${10}, ${11}, ${12}. Represents the parameters passed in by the script, and note that the numbers are enclosed in curly braces when you want to represent parameters after two digits.

$@ lists all the parameters, separated by spaces

$*: list all parameters, separated by the first character of the environment variable IFS

8. Command list:

AND list statement1 & & statement2 & & statement3 & & … Execute the latter command only if all the previous commands are executed successfully

OR list statement1 | | statement2 | | statement3 | |... Allow a series of commands to be executed until one command succeeds, and all subsequent commands will no longer be executed

Eg:#!/bin/sh

Touch file_one

Rm-f file_two

If [- f file_one] & & echo "hello" & & [- f file_two] & & echo "there"

Then

Echo "in if"

Else

Echo "in else"

Fi

Exit 0

The output of the above example is:

Hello

In else

With regard to AND lists and OR lists, which are very useful in logical judgment, here is one of the most commonly used examples:

[condition] & & command for true | | command for false:

Execute commandfor true when the condition is true and commandfor false when the condition is false

9. Colon: built-in null instruction, with a return value of 0

Eg: $:

$echo $?

$0

While: (this statement structure implements an infinite loop)

10; semicolon: in shell, the symbol that functions as a "continuous instruction" is a "semicolon"

Eg:cd / backup; mkdir startup; cp ~ /. * startup/.

11 # pound sign: indicates that the symbol is followed by the comment text and will not be executed

* match any character in the file name, including string

? Matches any single character in the file name.

~ home directory that represents the user

12.\ backslash: before the instruction, it has the effect of canceling aliases (alias); before the special symbol, the function of the special symbol disappears; at the end of the instruction, the instruction is connected to the next line (so that the carriage return is invalid and only serves as a newline).

13 、! Exclamation mark: usually it represents the role of anti-logic, such as in conditional detection, using! = to represent "not equal"

14. * * power operation: two asterisks represent the meaning of "power" in operation.

Eg:let "sus=2**3"

Echo "sus = $sus"

$sus = 8-

Thank you for your reading, these are the contents of "some special symbols and functions common in shell scripts". After the study of this article, I believe you have a deeper understanding of some common special symbols and roles in shell scripts, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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