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 }}
dc0453
codeSnippet
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
codeSnippet
/
shellPractice.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
80 lines (66 loc) · 2.24 KB
master
Breadcrumbs
codeSnippet
/
shellPractice.sh
Copy path
Top
File metadata and controls
Code
Blame
80 lines (66 loc) · 2.24 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
#注释包含以下几项
#脚本的参数
#脚本的用途
#脚本的注意事项
#脚本的写作时间,作者,版权等
#各个函数前的说明注释
#一些较复杂的单行命令注释
#判断参数个数
if [[ $# != 1 ]];then
echo "Parameter incorrect."
exit 1
fi
#source /etc/profile
#export PATH=”/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/apps/bin/”
#另起一行使用\
#./configure \
#–prefix=/usr \
#–sbin-path=/usr/sbin/nginx \
#–conf-path=/etc/nginx/nginx.conf \
#在使用”$”来获取变量的时候最好加上双引号
#shell的结构
func1() {
#do sth
}
func2() {
#do sth
}
main() {
func1
func2
}
main "$@"
#变量作用域,默认都是全局的
var=1
func() {
var=2
}
func
echo "$var"#输出是2,不是1
#使用local readonly这类的命令,或者使用declare来声明变量
#heredocs的使用,在”<<”后定一个标识符,接着我们可以输入多行内容,直到再次遇到标识符为止
#cat >> /etc/rsyncd.conf << EOF
#log file = /usr/local/logs/rsyncd.log
#transfer logging = yes
#log format = %t %a %m %f %b
#syslog facility = local3
#EOF
#获取当前脚本的路径
script_dir=$(cd $(dirname $0) && pwd)
script_dir=$(dirname $(readlink -f $0 ))
#尽量使用func(){}来定义函数,而不是func{}
#尽量使用[[]]来代替[]
#尽量使用$()将命令的结果赋给变量,而不是反引号
#在复杂的场景下尽量使用printf代替echo进行回显
#路径尽量保持绝对路径,绝多路径不容易出错,如果非要用相对路径,最好用./修饰
#优先使用bash的变量替换代替awk sed,这样更加简短
#简单的if尽量使用&& ||,写成单行。比如[[ x > 2]] && echo x
#当export变量时,尽量加上子脚本的namespace,保证变量不冲突
#会使用trap捕获信号,并在接受到终止信号时执行一些收尾工作
#使用mktemp生成临时文件或文件夹
#利用/dev/null过滤不友好的输出信息
#会利用命令的返回值判断命令的执行情况
#使用文件前要判断文件是否存在,否则做好异常处理
#不要处理ls后的数据(比如ls -l | awk '{ print $8 }'),ls的结果非常不确定,并且平台有关
#读取文件时不要使用for loop而要使用while read
You can’t perform that action at this time.