当前位置:网站首页>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
边栏推荐
- Characteristics and related specificity of Papain
- PTA class a simulated fifth bomb: 1148-1151
- 如何制定一个可实现的年度目标?
- About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi
- Pointnet/Pointnet++学习
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
- 接口测试用例设计
- socket demo01
- Prompt to update to the latest debug version during vscode debugging
- Introduction to gun make (1)
猜你喜欢

The 19th - 22nd week of scientific research training - about tnet and memnet

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

Pointnet/Pointnet++学习

On the difference between strlen and sizeof

Tengwenze, a hot-blooded boy, was invited to serve as the image ambassador of the global finals of the sixth season perfect children's model

SDRAM控制器——仲裁模块的实现

Playful girl wangyixuan was invited to serve as the Promotion Ambassador for the global finals of the sixth season perfect children's model

V4L2+QT视频优化策略

Dataframe extracts data from a column and converts it into a list

【js】免费api判断节假日、工作日和周六日
随机推荐
Redis7.0的安装步骤
Detailed explanation of memory leak check tools
SDRAM控制器——添加读写FIFO
Chinese and English instructions of collagen enzyme Worthington
qtvtkvs2015测试代码
Mot - clé C facile à comprendre statique
Other codes,, VT,,, K
LeetCode 41 ~ 50
求n的乘阶
UN make (6) conditional execution of makefile
tos cos dscp 区别和作用
连接投影仪
NDK20b FFmpeg4.2.2 编译和集成
Data analysis - similarities and differences between C-end and b-end data analysis
shell学习记录(四)
PTA class a simulated 8th bomb: 1164-1167
shell学习记录(一)
连接投影仪
The role of xargs
Convert Weishi camera pictures