if then bash 语句不适用于 qiime 命令
if then bash statement does not work with qiime commands
我刚开始 bash 一段时间以来,我一直停留在一个简单的 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
谢谢
我认为代码可以改进。您的代码中的错误主要是在设置变量后用于调用变量的美元运算符。
您正在计算 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
复习 shell 语法,尤其是 double quotes and parameter expansion。尽管嵌入了 space,但您需要一美元来扩展 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"
我刚开始 bash 一段时间以来,我一直停留在一个简单的 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
谢谢
我认为代码可以改进。您的代码中的错误主要是在设置变量后用于调用变量的美元运算符。
您正在计算 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
复习 shell 语法,尤其是 double quotes and parameter expansion。尽管嵌入了 space,但您需要一美元来扩展 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"