shell編程
開頭
#!/bin/bash
set -e
set -u
set -o pipeline
中間
將日常中不斷重復的工作寫到腳本中。但是為了腳本具有普遍適用性,所以要用到
變量和命令參數
條件語句
循環語句和文件名替換
# set work path
PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
work_dir=$1
sample_info=$2
reference=${work_dir}/data/reference/TAIR10_chr_all.fas
命令參數
就是命令后接的部分,例如cat some.file
的命令參數就是some.file。我們可以利用這個功能從外界傳入自定義的內容。shell腳本以$0,$1,$2,$3..
分別代表命令自身,第一個參數,第二個參數,第三個參數等。
新建一個argument_test.sh,輸入如下內容。
#! /bin/bash
echo "command is $0"
echo "first argument is $1"
echo "second argument is $2"
echo "third argument is $3"
運行結果:
Bash
$ bash argument_test.sh A B C
command is argument_test.sh
first argument is A
second argument is B
third argument is C
條件語句
正常情況下最好加上條件語句,提高腳本的適用性。
if [條件]
then
commands
else
comgmands
fi
[條件]可以用bash下的命令,例如
# 單個條件
if grep "pattern" some.file >/dev/null
then
echo "found 'pattern' in some.file"
fi
# 多個條件
# 或 || ; 與&& ;非 !
if grep 'pattern1' some.file > /dev/null &&
grep 'pattern2' some.file > dev/null
then
echo "found 'pattern1' and 'pattern2' in some.file"
fi
除了bash下的命令外,還可以用`test`或`[]`判斷條件是否成立,如下
# -f 判斷是否為文件
if test -f some.file
then
....
fi
# 或[] 記住里面的左右一定要有空格
if [ -f some.file ]
then
....
fi
《鳥叔的Linux的私房菜》第三版的380頁中提高了許多判斷方式,一般常用的如下:
| 測試的標志 | 代表意義 |
| ------------ | ------------ |
| -d | 是否為文件夾 |
| -f | 是否為文件 |
| -e | 文件是否存在 |
| -h | 是否為連接 |
| -w | 能否寫入 |
| -x | 能否執行 |
| str1 = str2 | 字符是否相同 |
| str1 != str2 | 字符是否不相同 |
| -eq | 數值是否相等 |
| -ne | 數值是否不等 |
| -lt | 小于 |
| -gt | 大于 |
| -le | 小于等于 |
| -ge | 大于等于 |
一般而言,創建一個pipline處理文件需要三個步驟:
選擇目標文件
使用指定命令處理目標文件
追蹤處理后的輸出文件
而循環語句就是在選擇目標文件后,用于重復相同的指令處理目標文件,還可以記錄輸出文件。
其中選擇目標文件有兩種方式:1. 提供記錄目標文件信息的文本,2.從含目標文件的文件夾內篩選。
方法1:
# 假設你現在在mbs-2017-4/script下
sample_info=sample.txt
sample_names=($(cut -f 1 "$sample_info" ))
echo "${sample_names[@]}"
BC.fg.reads1 BC.fg.reads2 BC.bg.reads1 BC.bg.reads2
說明 ${sample_names[@]}
的表示顯示所有變量內容,我們可以選擇特定的元素,將@
替換成0,1,2,3(以0開始),此外 ${#sample_names[@]}
表示共有多個元素, ${!sample_names[@]}
表示各個元素的索引。
方法2比較粗暴,直接利用通配符列出文件。
sample_names=$(ls *.fq)
for sample in ${sample_names[@]}
do
- 比對的參數需要根據具體情況修改,
- 要求具有特定的目錄構造
- 缺少日志信息輸出,不利于調試