惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recent Announcements
Recent Announcements
D
Docker
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
B
Blog
博客园_首页
罗磊的独立博客
D
DataBreaches.Net
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Bash Script Note 1
2020-04-27 · via jdhao's digital space

Writing robust scripts#

We should use the following options in our bash script for better quality:

Meaning of these option:

  • -e: Exit immediately if a command fails.
  • -u: If a variable is not set, this is considered an error1.
  • -x: Expand variables in a command and print it before executing the command (better for debugging if command fails).
  • -o pipefail: If any command in a pipe fails, this pipe will fail.

For more options, see bash set command doc.

common check conditions for files#

Some commonly used options for checking file conditions:

optionmeaning
[ -d FILE ]True if FILE exists and is a directory
[ -f FILE ]True if FILE exists and is a regular file
[ -r FILE ]True if FILE exists and is readable
[ -w FILE ]True if FILE exists and is writable
[ -x FILE ]True if FILE exists and is executable
[ -z STRING ]True if length of STRING is zero
[ -n STRING ]True if length of STRING is not zero

Note that to check if the opposite is true, use [ ! condition ].

Use [ ] or [[ ]] for condition check?#

Both [ ] and [[ ]] can be used to check some conditions, but [ ] is POSIX compatible, and [[ ]] is not. Both bash, zsh support [[ ]].

[[ ]] is also more versatile and powerful than [ ]. For example, it supports &&, || and grouping command using () etc.

Compare string equality#

We can use = and != to compare the equality of strings. Unlike common programming languages, use of == is non-standard (see here).

How to set boolean variable?#

In bash, there is not really a boolean type. We can just use literal string true to set a bool variable and check if variable is equal to string true. For example:

use_python=true
if [[ $use_python = true ]]; then
    # do something
else:
    # do another thing
fi

How to check if a command exists?#

We can use command -v YOUCOMMAND to check if YOURCOMMAND exists. For example:

if [[ ! "$(command -v rg)" ]]; then
    # install ripgrep
else
    echo "ripgrep already exists. No action needed."
fi

According to here, which command is not a reliable way to do this, since it may return an exit code of 0 even if no command is found.

References#