0

我刚开始抨击,我已经被一个简单的 if;then 语句卡住了一段时间。我使用 bash 运行用 python 编写的 QIIME 命令。这些命令允许我处理微生物 DNA。从测序的原始数据集中,我首先必须检查它们是否与 QIIME 可以处理的格式匹配,然后才能继续执行其余命令。

module load QIIME/1.9.1-foss-2016a-Python-2.7.11
echo 'checking mapping file and demultiplexing'
validate_mapping_file.py -m $PWD/map.tsv -o $PWD/mapcheck > tmp.txt
n_words=`wc -w tmp.txt`
echo "n_words:"$n_words
if [ n_words = '9 temp.txt' ];then
split_libraries_fastq.py -i $PWD/forward_reads.fastq.gz -b $PWD/barcodes.fastq.gz -m $PWD/map.tsv -o $PWD/demultiplexed
else
  echo 'Error(s) in map'
  exit 1
fi

如果地图很好,我希望得到以下输出(9 个字):

No errors or warnings were found in mapping file. 

如果不好(16个字):

Errors and/or warnings detected in mapping file.  Please check the log and html file for details.

我想使用此输出来调节以下命令 split_libraries_fastq.py。

我尝试了许多不同版本的 if;then 语句,寻求帮助,但似乎没有任何效果。你们中的任何人都知道为什么不运行“then”命令?我也通过集群运行它。

这是我的地图很好时的输出,第二个命令没有运行:

checking mapping file and demultiplexing
n_words:9 tmp.txt
Error(s) in map

谢谢

4

2 回答 2

0

我认为代码可以改进。您的代码中的问题主要是在设置变量后用于调用变量的美元运算符。

您正在计算 temp.txt 中的行数。更好的版本是:

n_words=$(wc -l temp.txt)
if [ "$n_words" -eq 9 ]; then
  echo "${n_words} equal to 9"
else
  echo "${n_words} is not equal to 9"
fi
于 2017-10-24T09:34:22.187 回答
0

查看 shell 语法,特别是双引号参数扩展。尽管有嵌入空间,但您需要一美元来扩展n_words和双引号以使其成为单个字符串。例如:

if [ "$n_words" = '9 temp.txt' ]; then
    echo "good"
else
    echo "bad"
fi

或者,考虑省略文件名并进行整数比较:

n_words=`wc -w < tmp.txt`
echo "n_words: $n_words"
if [ "$n_words" -eq 9 ]; then
#...

最后,让我警告你,计算单词的数量是一个糟糕的技巧,因为 Python 脚本中的无辜更改可能会破坏你的 shell 脚本。我不熟悉 Qiime,但它们应该提供有意义的退出状态。尝试:

validate_mapping_file.py -m $PWD/map.tsv -o $PWD/mapcheck > /dev/null
exit_status=$?
echo "exit status: $exit_status"
于 2017-10-24T10:10:13.220 回答