Bioinformatics Data Skills by Oreilly学习笔记-12
作者:互联网
Chapter12 Bioinformatics Shell Scripting, Writing Pipelines, and Parallelizing Tasks
We’ll see how to write rerunnable Bash shell scripts, automate fileprocessing tasks with find and xargs, run pipelines in parallel, and see a simple makefile.
Basic Bash Scripting
Writing and Running Robust Bash Scripts
1. A robust Bash header
#!/bin/bash
set -e
set -u
set -o pipefail
(1) This is called the shebang, and it indicates the path to the interpreter used to execute this script.
(2) 若某一行shell命令error,shell默认仍会执行完整个命令。set -e 使得error时直接终结整个命令,但某些可能不会有影响的error,如test -d file.txt will return a nonzero exit status if its argument is not a directory,却不会终结。
(3) 若某一命令含有未定义路径,shell仍会执行,如 rm -rf $TEMP_DIR/. If the shell variable $TEMP_DIR isn’t set, Bash will still substitute its value (which is nothing) in place of it. The end result is rm -rf /。使用set -u阻止类似命令的执行。
(4) 即使设置了set -e ,但在pipe中也只有最后一个program出现error时,才会导致程序跳出。set -o pipefail使得pipe 中任一program error都会终止程序。
2. Running Bash scripts
two ways:
bash script.sh: can run any script (as long as it has read permissions) with bash script.sh
./script.sh: might use interpreters other than /bin/bash (e.g., zsh, csh, etc.)
executable permissions:
$ chmod u+x script.sh
标签:12,script,Skills,error,set,bash,Bioinformatics,shell,Bash 来源: https://blog.csdn.net/weixin_42953727/article/details/100802214