当前位置:网站首页>Shell learning record (IV)
Shell learning record (IV)
2022-06-26 02:04:00 【_ Bruce】
Shell function
- 1、 You can take function fun() Definition , It can also be direct fun() Definition , Without any parameters .
- 2、 Parameter return , Can display plus :return return , If not , Results will be run with the last command , As return value . return Heel value n(0-255)
#!/bin/bash
funWithReturn(){
a=1
b=5
c=$[$a+$b]
return $c
}
funWithReturn
echo " The sum of the two numbers entered is $? !"
Output : The sum of the two numbers entered is 6 !
The return value of the function passes through $? To obtain a .
Be careful : All functions must be defined before use . This means that you have to put the function at the beginning of the script , until shell When the interpreter first discovered it , Can be used . Call a function using only its function name .
Function parameter
stay Shell in , You can pass parameters to a function when you call it . Inside the body of the function , adopt $n To get the value of the parameter , for example ,$1 Represents the first parameter ,$2 Represents the second parameter ...
Examples of functions with arguments :
#!/bin/bash
funWithParam(){
echo " The first parameter is zero $1 !"
echo " The second parameter is $2 !"
echo " The tenth parameter is $10 !"
echo " The tenth parameter is ${10} !"
echo " The eleventh parameter is ${11} !"
echo " The total number of parameters is $# individual !"
echo " Output all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73Output :
The first parameter is zero 1 !
The second parameter is 2 !
The tenth parameter is 10 !
The tenth parameter is 34 !
The eleventh parameter is 73 !
The total number of parameters is 11 individual !
Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73 !Be careful :$10 Can't get the tenth parameter , Getting the tenth parameter requires ${10}. When n>=10 when , Need to use ${n} To get the parameters .
in addition , There are also a few special characters for processing parameters :

Shell Input / Output redirection Shell

Redirect in-depth explanation

/dev/null file

File contains
Like any other language ,Shell You can also include external scripts . This can easily encapsulate some common code as a separate file .
We can go through .fileName perhaps source fileName Include files
test1.sh The code is as follows :
#!/bin/bash
url="http://www.baidu.com"test2.sh
#!/bin/bash
# Use . Number test1.sh file
. ./test1.sh
# Or use the following include file code
# source ./test1.sh
echo " Baidu official website address :$url"And then execute test2.sh
Output :
Baidu official website address :http://www.baidu.com
边栏推荐
- Redis7.0 installation steps
- Pre ++, post ++ and pre -- and post -- (+a, a++ and --a, a--)
- 字节序问题
- 一分钟了解同步、异步、阻塞和非阻塞的区别
- 将lua print输出到cocos2d控制台输出窗口中
- Find the multiplication order of n
- One stop solution EMQ for hundreds of millions of communication of Internet of things
- 通俗易懂C語言關鍵字static
- How to efficiently complete daily tasks?
- Convert Weishi camera pictures
猜你喜欢

Visual studio 2013 redistributable is installed, but MySQL installation fails

Show spirit chenzitong was invited to be the chief experience officer of the global finals of the sixth season perfect children's model

readv & writev

How to add a "security lock" to the mobile office of government and enterprises?

Pointnet/Pointnet++学习

The answer skills and examples of practical cases of the second construction company are full of essence

Sweet girl lisixia was invited to be the little host of the global finals of the sixth season perfect child model

Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Hangzhou

Make a row of the dataframe a column name

Dataframe extracts data from a column and converts it into a list
随机推荐
Chinese and English instructions of collagen enzyme Worthington
通俗易懂C語言關鍵字static
wifi 的理论速度计算方法
如何制定可实现中长期目标?
Dataframe converts the data type of a column
On the difference between strlen and sizeof
Pointnet/Pointnet++学习
wifi 相关知识详解
Output Lua print to the cocos2d console output window
Tcp网络通信中各个状态的含义
如何使用命令将文件夹中的文件名(包括路径)写入到txt文件中
Find the multiplication order of n
FPGA实现图像二值形态学滤波——腐蚀膨胀
V4L2+QT视频优化策略
求n乘阶之和
Visual studio 2013 redistributable is installed, but MySQL installation fails
Dazhou suggested that we media bloggers do these four things in the early stage
Connecting the projector
SDRAM控制器——仲裁模块的实现
VTK initialization code learning 1