当前位置:网站首页>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
边栏推荐
- 揭秘GaussDB(for Redis):全面对比Codis
- Image tagging to obtain the coordinates of the image in the ImageView
- Swagger document generated by node project API in vscode
- Thinkphp3 use phpword to modify the template and download it
- R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter, spe
- Renrenyue -- renrenyue system development source code sharing
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information
- Windows下MySQL的安装和删除
- How can we make an annual income of onemillion yuan by making our own media video?
- [论]Learning Dynamic and Hierarchical Traffic Spatiotemporal Features with Transformer
猜你喜欢

(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)

Jeecgboot startup popup configuration is still incorrect

Understanding and construction of devsecops and Devops

K8s, docker compose install MySQL 8.0.18

(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code

Singleton mode in PHP to reduce memory consumption

The server reported an error 503 service unavailable:the system returned: (71) protocol error

Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection

揭秘GaussDB(for Redis):全面对比Codis

(3) Pyqt5 tutorial -- > signal and slot preliminary test
随机推荐
揭秘GaussDB(for Redis):全面對比Codis
Laravel multi project mutual access
Qiantang Pingou source code -- Qiantang Pingou app system development source code sharing
Summary of common MySQL database commands (from my own view)
Polling and long polling
When MySQL queries fields in JSON format, it takes a property value of JSON data
JS array length is defined
ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities
laravel 9
Learning notes 2022 overview | automatic graph machine learning, describing AGML methods, libraries and directions
Image tagging to obtain the coordinates of the image in the ImageView
(6) Pyqt5--- > window jump (registration login function)
Zhangxiaobai's road of penetration (IV) -- detailed explanation of XSS cross site script vulnerabilities
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
Renrenyue -- renrenyue system development source code sharing
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
Hook technology
Thinkphp3 count ` *'problem
Lighten the source code -- lighten the app system development function introduction to the beautiful world lighten the app system development source code in China
15. Notes on the button style of WPF