当前位置:网站首页>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
边栏推荐
- Jeecgboot startup popup configuration is still incorrect
- Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode
- ARM V7 连续加载/存储
- Digital currency exchange -- digital currency exchange system development source code sharing
- 19. Implementation of MVVM architecture based on WPF event to command
- Micro engine generates QR code
- Go novice exploration road 2
- Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection
- Explain AHP in human language (very detailed principle + simple tool implementation)
- Huikan source code -- Huikan app system secondary development source code sharing
猜你喜欢

Windows下MySQL的安装和删除

A commonly used statistical modeling method -- difference analysis
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

Flutter common commands and problems

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

一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推

Penetration tool environment - installing sqli labs in centos7 environment

Understanding and construction of devsecops and Devops

Swagger document generated by node project API in vscode

ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
随机推荐
Upgrade opsenssh to 8.8p1
Mysql database logs binlog save aging (expire\u logs\u days)
Wechat forbids sharing
Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
Zhangxiaobai's way of penetration (VIII) - detailed operation steps of SQL injection - Boolean blind injection of blind injection
Arm immediate
Zhangxiaobai's way of penetration (III) -- detailed explanation of SQL injection vulnerability principle (SQL Server)
When MySQL queries fields in JSON format, it takes a property value of JSON data
ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode
JQ verifies whether the input color is legal
QT TCP UDP network communication < theory >
Zunpin Yongyao advertising e-commerce system -- Zunpin Yongyao advertising e-commerce app system development source code sharing
ARM 立即数
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
[data midrange] what is the oneid of the data midrange? Isn't the master data fragrant?
An easy-to-use seal design tool - (can be converted to ofd file)
Thinkphp3 use phpword to modify the template and download it
The network traceroute command is used to determine the path through which IP packets access the destination address.
Thinkphp3 count ` *'problem