我刚开始大发雷霆,一段时间以来,我一直被困在一个简单的if;然后声明上。我使用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;然后声明,四处寻求帮助,但似乎没有任何效果。你们中有谁知道为什么“那么”命令不运行?而且,我在一个集群中运行它。
下面是映射良好时的输出,第二个命令没有运行:
checking mapping file and demultiplexing
n_words:9 tmp.txt
Error(s) in map谢谢
发布于 2017-10-24 10:10:13
检查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脚本。我不熟悉奇美,但他们应该提供一个有意义的退出状态。尝试:
validate_mapping_file.py -m $PWD/map.tsv -o $PWD/mapcheck > /dev/null
exit_status=$?
echo "exit status: $exit_status"发布于 2017-10-24 09:34:22
我认为代码可以改进。代码中的错误主要是用于在设置变量后调用变量的美元运算符。
您正在计算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"
fihttps://stackoverflow.com/questions/46906854
复制相似问题