BSD 第三章 補習 Unix Shell

stream: 標準輸入,標準輸出,標準錯誤
pipe: 命令的串聯(lián)使用
process:使用 stream + pipe 寫好的 program 在成功完成或報錯前都是進程

nohup法:使用nohup為例說明 流(Stream)和 重定向(Redirection)


# 末尾加 & 能讓程序在后臺運行的同時,我們也能獲得交互shell的能力,注意要指定路徑 
$ program1 input.txt > results.txt &  # 語法
$ nohup ./detelOut.sh &  # 舉例

# linux中有三種標準流(stream),分別是Standard Input,Standard Out,Standard Error,對應的數(shù)字是0,1,2
$ nohup command > myout.file 2>&1 &     # 將所有輸出的信息寫入myout.file
$ nohup ./Cox.sh > STDOUT.txt 2> stderr.txt &   # 后臺運行Cox.sh,將標準輸出到STDOUT.txt文件中,將標準錯誤輸出到stderr.txt文件中
$ nohup ./Cox.sh >> STDOUT.txt 2>> stderr.txt &     #  >> 不覆蓋文件中原有內(nèi)容,會將新內(nèi)容加在末尾; > 操作符會覆蓋已有內(nèi)容,謹慎使用
$ nohup ./Cox.sh >> /dev/null/STDOUT.txt 2>> /dev/null/stderr.txt &      # /dev/null/ 可以存放這類日志的黑洞!?。。ㄐ轮R呢?。。?

前臺運行 與 后臺運行

# 將后臺運行的進程改為前臺運行
$ jobs  # 查看有哪些后臺運行的進程
$ fg %1   # 將目標進程改為前臺進程,fg + job ID num

# 將前臺運行的進程改為后臺運行
$ # enter Control + Z    #暫停進程 (Control + C 是 kill 進程)
$ bg %1  # 將目標進程改為后臺進程,bg + job ID num

監(jiān)督重定向的標準錯誤輸出文件(在運行比較久的程序時很需要)


# 參數(shù)-n可以設(shè)置查看文件的最末尾的行數(shù)

$ tail -n 

# 參數(shù)-nf (follow),可以實時輸出末尾n行,供參看

$ ail -f 

tee : 保存 program1 的輸出到中間文件和重定向到 program2 的標準輸入

$ program1 input.txt | tee intermediate-file.txt | program2 > results.txt

Exit status

# 語法
$ program1 input.txt > results.txt      # 進程
$ echo $?       # 查看上面進程的 exit status

# 舉例
$ true
$ echo $?
0       # exit status 為0時,表示進程成功運行
$ false
$ echo $?
1       # exit status 不為0時,表示進程中出現(xiàn)錯誤或 occurred

# 使用 邏輯運算符 && 和 ||: a subsequent command in a chain is run conditionally on the last command's exit status.
$ program1 input.txt > intermediate-results.txt && program2 intermediate-results.txt > input.txt        # 只有當 program1 的exit status 為0時(運行成功),program2 才會運行
$ program1 input.txt > intermediate-results.txt || program2 intermediate-results.txt > input.txt        # 只有當 program1 的exit status 不為0時(運行出錯),program2 才會運行

# 舉例
$ true && echo " last command was a success"
last command was a success
$ false && echo " last command was a success"
$ false || echo " last command was not  a success"
last command was not  a success
$ true || echo " last command was not  a success"

命令的替換 $, alias

$ grep -c '^>' input.fasta
416
$ echo "There are $(grep -c '^>' input.fasta) entries in my FASTA file"
There are 416 entries in my FASTA file

$ mkdir results-$(data +%F) 
$ ls
results-2019-06-22

# 在 ```~/.bashrc```(Linux) 或 ```~/.profile``` (OS X) 添加快捷命令
alias mkpr = "mkdir -p {data/seqs,scripts,analysis}"
alias today = "data +%F"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容