Linux If then elif then else bash中的语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9691872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If then elif then else statement in bash
提问by
then, elif, else statement that I have programmed in a bash script. I know that it works because I can run the same command in the terminal interface and see that it is doing what I want it to do. However when I run it in a script it seems to always jump to the else statement and not detect anything. Can anybody help explain why this is so? Here is my script code:
然后,elif,我在 bash 脚本中编写的 else 语句。我知道它有效,因为我可以在终端界面中运行相同的命令,并看到它正在做我想要它做的事情。但是,当我在脚本中运行它时,它似乎总是跳转到 else 语句并且没有检测到任何内容。任何人都可以帮助解释为什么会这样吗?这是我的脚本代码:
if [ -e "" ]
then
for line in `samtools view -H `
do
if [[ "$line" == *NCBI-Build-36\.1* ]]
then
echo "hg18"
break
elif [[ "$line" == *hg19* ]]
then
echo "hg19"
break
else
echo "Reference was not found, manual entry required: "
read ans
echo "$ans"
break
fi
done
else
echo -e "Usage: \e[1;32mreadRef.sh \e[1;36mbamfile.bam"
fi
No matter what file I plug in it always skips to the else and asks me for manual entry.
无论我插入什么文件,它总是跳到 else 并要求我手动输入。
Here is the command I ran on terminal:
这是我在终端上运行的命令:
for line in `samtools view -H $bignormal`; do if [[ "$line" == *NCBI-Build-36\.1* ]]; then echo "YES - $line"; else echo "NO - $line"; fi; done
And the output is like this:
输出是这样的:
NO - @HD
NO - VN:1.0
NO - GO:none
NO - SO:coordinate
NO - @SQ
NO - SN:1
NO - LN:247249719
YES - AS:NCBI-Build-36.1
YES - UR:http://www.bcgsc.ca/downloads/genomes/9606/NCBI-Build-36.1/bwa_ind/genome/
NO - SP:Homo
NO - sapiens
.
.
.
Why is the script not detecting the string I am looking for, but it is in terminal?
为什么脚本没有检测到我正在寻找的字符串,但它在终端中?
EDIT: I tried what Charles said, this is the output:
编辑:我尝试了查尔斯所说的,这是输出:
:+'[' -e /projects/rcorbettprj2/DLBCL/CNV/RG065/normal/A01440_8_lanes_dupsFlagged.bam ']'
::+samtools view -H /projects/rcorbettprj2/DLBCL/CNV/RG065/normal/A01440_8_lanes_dupsFlagged.bam
:+for line in '`samtools view -H `'
:+case "$line" in
:+echo 'Reference was not found, manual entry required: '
Reference was not found, manual entry required:
:+read ans
采纳答案by Gordon Davisson
I think your code has a logic error nobody's spotted yet. I'm not sure, since you haven't told us what the script's supposed to be doing, but it looks to me like what you want is to ask for manual entry only if you don't find a match to either of your patterns anywhere in the output, but what you're actually doing is examining only the first word of outputfor a match. And from your sample output, the first word is "@HD", which doesn't match either pattern, so the script is doing exactly what I'd expect.
我认为你的代码有一个没有人发现的逻辑错误。我不确定,因为您还没有告诉我们脚本应该做什么,但在我看来,您想要的是仅在找不到与您的任一模式匹配的情况下才要求手动输入输出中的任何位置,但您实际所做的只是检查匹配的输出的第一个单词。从您的示例输出中,第一个单词是“@HD”,它与任一模式都不匹配,因此脚本正在按照我的预期执行。
Now, assuming I'm right and that the point is to look for either pattern anywhere in the output, you can actually simplify things a bit. Mainly, you don't need the loop, you can just do a single comparison to look for the pattern in the entire output at once:
现在,假设我是对的并且重点是在输出中的任何位置查找任一模式,您实际上可以稍微简化一下。主要是,您不需要循环,您只需进行一次比较即可立即在整个输出中查找模式:
#!/bin/bash
if [ -e "" ]
then
output="$(samtools view -H "")"
if [[ "$output" == *NCBI-Build-36.1* ]]
then
echo "hg18"
elif [[ "$output" == *hg19* ]]
then
echo "hg19"
else
read -p "Reference was not found, manual entry required: " ans
echo "$ans"
fi
done
else
echo -e "Usage: \e[1;32mreadRef.sh \e[1;36mbamfile.bam"
fi