项目
在 GitHub 查看
SixArm/unix-shell-script-tactics
关于
Unix shell script tactics - best practices style guide
学习与教育实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- -1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Unix shell script tactics - best practices style guide
This Unix shell script style guide helps us write better Unix shell script code for speed, security, stability, and portability.
This guide is by SixArm.com, a software consultancy. Our customers use shell scripts within a wide variety of Unix shells, so we aim for POSIX for simple scripts.
Highlights:
- Aim for POSIX when possible because of portability and standardization
- Protect scripts by using
setflags such asset -euf - Print output with
printfnotecho - Trap signals and exit by using
trap trap_exit EXIT - Use a
main()function - Run executable with no file name extension
- Source with dot operator
.not keywordsource - Version name: use semantic versioning
- Help: use a function and HERE document
- Heredoc with cat not read
- Date & time format: use UTC and ISO8601
- Booleans: use true and false
- Colors: use terminal escape codes & NO_COLOR & TERM=dumb
- Subshells: use parentheses
$()not backticks - Trace using set -x then set +x without printing
- Hunt for bugs by using ShellCheck
- Fix some bugs automatically, if you want, by using Shellharden
Recommendations (details of these are TODO):
- Quote liberally such as
"$var"instead of just$var, for safety. - Bulletproof scripts to handle characters such as a quote, newline, leading dash.
- Enable a user to customize commands by using env vars such as
${FOO:-foo}. - Create temporary files by using
mktempinstead oftempfileet. al.
Demo:
Topics
Argument parsing:
Environment variables:
Directories:
- Script directory: use realpath or cd
- Log directory: use $LOG_HOME
- Data directory: use $XDG_DATA_HOME
- Cache directory: use $XDG_CACHE_HOME
- Configuration directory: use $XDG_CONFIG_HOME
- Runtime directory: use $XDG_RUNTIME_HOME
- Temporary directory:: use
mktemp
Files:
- Temporary file using
mktempandtrap - Find files with filter for permission denied
- Find files with special characters
- Find files with readable permissions
- Find files with executable, perm, test, exec
References:
- Writing safe shell scripts
- Rich’s sh (POSIX shell) tricks
- Google Shell Script Style Guide
- Filenames and Pathnames in Shell: How to do it Correctly
- Standard Command-Line Options
- How to do things safely in bash
- pure sh bible & pure bash bible
Conventions:
- Program name using a string or basename
- Git top level directory using rev-parse
- Functions: out, err, die, big, log, now, sec, zid, cmd, etc.
- Assert functions: assert_empty, assert_equal, assert_match
- sec() function portability
Control flow statements:
Functions:
Command snippets:
Compatibilities:
readlinkon macOS behaves differently- [
realpathis` not available on older macOS default](doc/realpath-is-not-available-on-older-macos-default/)
PostgreSQL psql:
探索相关项目
加载中…