当前位置:网站首页>Shell learning notes (latest update: 2022-02-18)
Shell learning notes (latest update: 2022-02-18)
2022-06-25 12:38:00 【haoming Hu】
Article reference PHP Chinese net
Catalog
1. shell brief introduction
shell It's a command line interpreter , Parse user commands into the actual execution of the operating system , Realize the interaction between users and operating system , When you need to repeat several commands , You can assemble commands , Add certain control statements , Become shell Script files , hand shell Batch execution
2. first demo
create a file vi test.sh
Write output statement
echo "hello world" echo "first demo"Authorize scripts chmod +x ./test1.sh
Execute the script ./test1.sh
Output

3. shell Variable
3.1 Variable definitions
There can be no spaces between variable names and equal signs , This may not be the same as any programming language you are familiar with . meanwhile , The naming of variable names should follow the following rules :
- The first character must be a letter (a-z,A-Z).
- No spaces in between , You can use underscores (_).
- You can't use punctuation .
- Out of commission bash Keywords in ( You can use help Command to view reserved keywords ).
myname="huhaoming"
3.2 Use defined variables
Use a defined variable , Just precede the variable name with a dollar sign , Such as :
myname="huhaoming"
echo $your_name
echo ${your_name} #{} not essential , Curly braces are used to help the interpreter identify the boundaries of variables
Variables defined , Can be redefined
myname="tom"
echo $myname
myname="alibaba"
echo $myname
A read-only variable use readonly modification
myname="tom"
readonly myname
myname="huhaoming"

Delete variables
Use unset Command to delete variables , Variable cannot be used again after being deleted .unset Command cannot delete read-only variables .
echo "huhaoming hello world"
myname="huhaoming"
readonly myname
echo $myname
unset myname
echo $myname

echo "huhaoming hello world"
myname="huhaoming"
echo $myname
unset myname
echo $myname

3.3 shell character string
Restrictions on single quoted string :
- Any character in a single quotation mark will be output as is , Variables in a single quoted string are invalid ;
- A single quotation mark cannot appear in a single quotation mark string ( You can't escape a single quotation mark ).
The advantages of double quotes :
- You can have variables in double quotes
- Escape characters can appear in double quotes
echo "huhaoming hello world"
myname="huhaoming"
str="Hello, I know your are \"$myname\"! \n"
echo $str

String concatenation
echo "huhaoming hello world"
myname="huhaoming"
greeting="hello, "$myname" !"
greeting_1="hello, ${myname} !"
echo $greeting $greeting_1

Get string length
echo "huhaoming hello world"
myname="huhaoming"
echo ${
#myname}

Extract substring
echo "huhaoming hello world"
myname="huhaoming"
echo ${myname:1:4}

3.4 Array
bash Supports one dimensional array ( Multidimensional arrays are not supported ), And there's no limit to the size of the array . Similar to C Language , The subscripts of array elements are 0 Numbered starting . To get the elements in an array, you need to use subscripts , Subscripts can be integers or arithmetic expressions , Its value should be greater than or equal to 0.
Define an array
stay Shell in , Use parentheses to represent arrays , For array elements " Space " Symbol split . The general form of defining an array is : Array name =( value 1 value 2 … value n)
array_name=(value0 value1 value2 value3)
Individually define
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen
Read array
${ Array name [ Subscript ]}
Use @ The symbol can get all the elements in the array , for example :
echo ${array_name[@]}
The length of the array
# Get the number of array elements
length=${
#array_name[@]}
# perhaps
length=${
#array_name[*]}
# Get the length of a single element of an array
lengthn=${
#array_name[n]}
echo "huhaoming hello world"
myname=(0,1,2,3,4)
echo ${
#myname[*]}
echo ${
#myname[@]}
echo ${
#myname[1]}

4. Shell Pass parameters
In execution Shell Script time , Pass parameters to script , The format of obtaining parameters in the script is :$n.n Represents a number ,1 For executing the first parameter of the script ,2 For executing the second parameter of the script , And so on ……, among $0 For the filename of the execution :
echo "huhaoming hello world"
echo " File name of execution :$0"
echo " The first parameter is zero :$1";
echo " The second parameter is :$2";
echo " The third parameter is zero :$3";

A few special characters are used to handle parameters :
| Processing parameters | explain |
|---|---|
| $# | The number of parameters passed to the script |
| $* | Display all the parameters passed to the script in a single string . Such as "$*“ use 「”」 In a nutshell 、 With "$1 $2 … $n" Output all parameters in the form of . |
| $$ | The current process the script runs ID Number |
| $! | Of the last process running in the background ID Number |
| [email protected] | And ∗ phase Same as , but yes send use when Add lead Number , and stay lead Number in return return Every time individual ginseng Count . Such as " * identical , But use quotation marks , And return each parameter in quotation marks . Such as " ∗ phase Same as , but yes send use when Add lead Number , and stay lead Number in return return Every time individual ginseng Count . Such as "@“ use 「”」 In a nutshell 、 With "$1" “ 2 " … " 2" … " 2"…"n” Output all parameters in the form of . |
| $- | Show Shell Current options used , And set Same command function . |
| $? | Display the exit status of the last command .0 No mistakes , Any other value indicates an error . |
5. shell Operator
|
| @ ∣ And @ | And @∣ And * identical , But use quotation marks , And return each parameter in quotation marks . Such as "[email protected]“ use 「”」 In a nutshell 、 With "$1" “ 2 " … " 2" … " 2"…"n” Output all parameters in the form of . |
| $- | Show Shell Current options used , And set Same command function . |
| $? | Display the exit status of the last command .0 No mistakes , Any other value indicates an error . |
5. shell Operator
边栏推荐
- Mysql database logs binlog save aging (expire\u logs\u days)
- Tidb common commands
- An example of using dynamic datalist
- VIM common commands and shortcut keys
- A commonly used statistical modeling method -- difference analysis
- Repair the error that ECSHOP background orders prompt insufficient inventory when adding goods. Please reselect
- JQ verifies whether the input color is legal
- New and old cluster migration of Minio data
- The network traceroute command is used to determine the path through which IP packets access the destination address.
- Gradle knowledge points
猜你喜欢

Total number of MySQL statistics, used and unused

MySQL common interview questions

Explain AHP in human language (very detailed principle + simple tool implementation)

Singleton mode in PHP to reduce memory consumption

MySQL and excel tables importing database data (Excel for MySQL)

What is the primordial universe

K8s, docker compose install MySQL 8.0.18

The first techo day Tencent technology open day in 2022 will be held online on June 28

Swagger document generated by node project API in vscode

A commonly used statistical modeling method -- difference analysis
随机推荐
Image tagging to obtain the coordinates of the image in the ImageView
20. MVVM command binding of WPF
2022 meisai e topic ideas sharing + translation
Development with courtesy -- share the source code of the secondary development of the app system of the imitation shopping mall
Dynamic proxy
15. Notes on the button style of WPF
Huile optimization system -- sharing of secondary development source code of huile optimization app system
Recyclerview scrolls to the specified location
2022 Baidu collection batch automatic push assistant
An easy-to-use seal design tool - (can be converted to ofd file)
JQ dynamic setting radio does not take effect when selected
Penetration tool environment -- use of cknife Chinese kitchen knife
Go defer little knowledge
Renrenyue -- renrenyue system development source code sharing
(6) Pyqt5--- > window jump (registration login function)
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the lrtest function of epidisplay package to perform multiple model
Polling and long polling
The server reported an error 503 service unavailable:the system returned: (71) protocol error
Zhangxiaobai's way of penetration (III) -- detailed explanation of SQL injection vulnerability principle (SQL Server)
ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities