当前位置:网站首页>(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
边栏推荐
- Find the combination number acwing 886. find the combination number II
- Raw socket grabs packets, and packets on some ports cannot be caught
- 数字三角形模型 AcWing 1015. 摘花生
- A deep analysis of the soul of C language -- pointer
- 49 letter ectopic grouping and 242 effective letter ectopic words
- Backpack model acwing 1022. Collection of pet elves
- Game theory acwing 892. Step Nim game
- 中国剩余定理 AcWing 204. 表达整数的奇怪方式
- Force buckle - 10. Regular expression matching
- Knapsack model acwing 1024. Packing problem
猜你喜欢

Installation and use of GTEST and gmock

Pat (Grade B) 2022 summer exam

KEPServer配置

Longest ascending subsequence model acwing 482. Chorus formation

Redis high availability principle

The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd

Longest ascending subsequence model acwing 272. longest common ascending subsequence

Longest ascending subsequence model acwing 1012. Sister Cities

The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence

区间问题 AcWing 906. 区间分组
随机推荐
C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
Gaussian elimination acwing 884. Gaussian elimination for solving XOR linear equations
15 design movie rental system
49字母异位分组和242有效的字母异位词
博弈论 AcWing 894. 拆分-Nim游戏
Description and feelings
Game theory acwing 892. Step Nim game
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
Knapsack model acwing 423. Picking herbs
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
深析C语言的灵魂 -- 指针
15th largest value of data flow
49 letter ectopic grouping and 242 effective letter ectopic words
Wechat push - template message parameter configuration
状态压缩DP AcWing 91. 最短Hamilton路径
Knapsack model acwing 1024. Packing problem
Longest ascending subsequence model acwing 1014. mountaineering
力扣——10. 正则表达式匹配
Wilderness search --- search iterations
Find the combinatorial number acwing 889. 01 sequence satisfying the condition