当前位置:网站首页>(8) Shell function
(8) Shell function
2022-07-27 11:22:00 【Hungry Taibai Xingjun】
#!/bin/bash
# 1. Shell function
# 1.1 Function definition format
:<<EOF
[ function ] funcname [()]
{
action;
[return int;]
}
# 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 , The result of the last command will be returned as a result .
# 3. return Rear root value n(0-255)
EOF
# 1.2 example 1
demoFun()
{
echo " This is my first Shell function "
}
echo "-------- The function starts executing --------"
demoFun
echo "-------- End of function execution --------"
# 1.3 example 2( belt return sentence )
funWithReturn()
{
echo " Enter a number "
read aNum
echo " The number you enter is ${aNum}"
return ${
aNum}
}
funWithReturn
echo "funWithReturn The return value is $? !" # The return value of the function passes through $? To obtain a .
# Function must be defined first , After the call
# 1. Function parameter
# 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 ...
funWithParam()
{
echo " The first 1 The parameter is $1 !"
echo " The first 2 The parameter is $2 !"
echo " The first 3 The parameter is $3 !"
echo " The eleventh parameter is ${11} !" # 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 .
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 73
:<<EOF
$# Number of arguments passed to script or function
$* Display all the parameters passed to the script in a single string
$$ The current process the script runs ID Number
$! Of the last process running in the background ID Number
[email protected] And $* identical , But use quotation marks , And return each parameter in quotation marks .
$- 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 .
EOF
边栏推荐
- 求组合数 AcWing 886. 求组合数 II
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- 最长上升子序列模型 AcWing 1016. 最大上升子序列和
- Longest ascending subsequence model acwing 272. longest common ascending subsequence
- Luogu p3052 [usaco12mar]cows in a skyscraper G
- Digital triangle model acwing 275. pass note
- Ansible
- NFT leaderboard -nft real offer latest address: NFT leaderboard.com
- 4 search insertion location
- Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
猜你喜欢

SQL Server2000 database error

XXX packages are looking for funding run 'NPM fund' for details solutions

Win10 vscode code code format setting and remote breakpoint debugging

Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen

Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review

Digital triangle model acwing 275. pass note

Interval problem acwing 906. Interval grouping

Wechat push - template message parameter configuration

Instructions for mock platform

记忆化搜索 AcWing 901. 滑雪
随机推荐
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
A deep analysis of the soul of C language -- pointer
4 search insertion location
properties文件
最长上升子序列模型 AcWing 1012. 友好城市
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
Luogu p1896 non aggression
Introduction to software vulnerability analysis (I)
什么是私域流量?
Raw socket grabs packets, and packets on some ports cannot be caught
7 row K with the weakest combat effectiveness in the matrix
Solve importerror: cannot import name'abs'import tensorflow error
TensorFlow张量运算函数集
A verification test of the relationship between iteration number and entropy
SQL Server2000 database error
求组合数 AcWing 885. 求组合数 I
49字母异位分组和242有效的字母异位词
6 find the smallest letter larger than the target letter
Maximized array sum after 13 K negations
PAT(乙级)2022年夏季考试