当前位置:网站首页>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
边栏推荐
- 如何制定可实现中长期目标?
- Exploring temporary information for dynamic network embedding
- shell curl 执行脚本,带传参数,自定义参数
- Weishi camera display
- shell学习记录(一)
- It's better to finish one than start thousands of times (reprinted from Douban)
- The answer skills and examples of practical cases of the second construction company are full of essence
- Differences and functions of export set env in makefile
- A solution to cross domain problems
- Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
猜你喜欢

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

Abnova actn4 DNA probe solution

About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi

【无标题】vsbiji esp....32

recvmsg & sendmsg

cv==biaoding---open----cv001

Sunshine boy chenhaotian was invited to be the spokesperson for the global finals of the sixth season perfect children's model

jenkins汉化及汉化无效解决方案

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

Sweet girl lisixia was invited to be the little host of the global finals of the sixth season perfect child model
随机推荐
连接投影仪
Connecting the projector
Finding the sum of N multiplications
Dataframe converts the data type of a column
Abnova actn4 DNA probe solution
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!
recvmsg & sendmsg
记录一个诡异的图片上传问题
如何高效的完成每日的任务?
Find the multiplication order of n
跨域问题的一种解决方案
Abnova anti GBA monoclonal antibody solution
程序 编译的步骤 预编译 编译 汇编 连接
宁要一个完成,不要千万个开始(转载自豆瓣)
Reverse output an integer
如何制定可实现中长期目标?
Detailed explanation of WiFi related knowledge
Chemical properties and application of trypsin
The role of xargs