Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
duringnone
shell-note
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
shell-note
/
function2.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (48 loc) · 2.15 KB
master
Breadcrumbs
shell-note
/
function2.sh
Copy path
Top
File metadata and controls
Code
Blame
65 lines (48 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#这是函数第二部分
# 1) 所有函数必须先定义再使用,shell与php不同,调用函数的代码位置必须函数定义的代码位置之后
# 2) shell函数return返回值:有数值的大小限制, 0~255循环计数,数值超过 255 ,重新从0开始算,所以如果返回超过255,就不能用这种方式,建议采用echo输出。
# 3) 报错/调试信息 须重定向到指定日志文件中,不要直接在 脚本/函数中 返回。不能向标准输出一些不是结果的东西(也就是说,不能随便echo一些不需要的信息),比如调试信息,这些信息可以重定向到一个文件中解决,特别要注意的是,脚本中用到其它类似grep这样的命令的时候,一定要记得1>/dev/null 2>&1来空这些输出信息输出到空设备,避免这些命令的输出。
# 4) $?
### a)用于输出返回值
### b) 用于命令执行反馈,即判断命令是否出错,正> 常返回0,出错返回非0值
## $# 函数入参个数
## $n 第n个入参,当 n >= 0,必须写成 ${n}
## $* 将所有入参的值输出到一个字符串中,并以空格分隔开每个入参
# #func1 # 报错,函数此时未定义
# function func1()
# {
# return 111;
# }
# # exit; 断点
# func1
# echo "函数返回值:$?"; # $? :1)用于输出返回值 2) 用于命令执行反馈,即判断命令是否出错,正常返回0,出错返回非0值
#
function func2()
{
echo '第1个参数:'$1;
echo '第9个参数:'$9
echo '第9个参数:'${9}
echo '第10个参数:'$10
echo '第10个参数:'${10}
echo "共有 $# 个入参"
echo "所有入参的值依次是:$*"
echo "所有入参的值依次是:$@"
# return '这是当前函数的入参信息'; # 非数值,reutrn会报错,可以用echo
# return '211'; # 输出:211
# return '255'; # 输出:255
# 0~255循环计数,数值超过 255 ,重新从0开始算
# return '256'; # 输出:0
# return '257'; # 输出:1
return '300'; # 输出:44
}
func2 'name' 2 3 4 5 6 7 8 9 'mail' '人生'
echo "func2函数的返回值:$?";
function func3()
{
echo $-;
# echo $$;
# echo $!;
# echo "$@";
}
func3
You can’t perform that action at this time.