简体   繁体   English

使用if和else检查目录名并在linux bash脚本中分配变量

[英]using if and else to check directory name and assign variable in linux bash script

I am creating (what I thought was) a simple script to check through my current directory which contains more directories with the names;我正在创建(我认为是)一个简单的脚本来检查我当前的目录,其中包含更多带有名称的目录;

SAMPLE_ANN-A10, SAMPLE_ANN-B4 etc.
SAMPLE_NEM-E7, SAMPLE_NEM-H2, etc.
SAMPLE_TODE-H02 etc. etc.

I would like to assign a QUERY variable for each SAMPLE and run a command.我想为每个 SAMPLE 分配一个 QUERY 变量并运行一个命令。 All TODEs will use the same variable, all NEMs will use the same and all ANNs the same.所有 TODE 将使用相同的变量,所有 NEM 将使用相同且所有 ANN 相同。

Here is my current script with the error message.这是我当前带有错误消息的脚本。 Thanks for looking.谢谢你看。


#!/bin/bash

for dir in $@
do
QUERY=''
    if $dir == SAMPLE_ANN*/
        then
            $QUERY=annelids.fasta
    elif $dir == SAMPLE_NEM*/
        then
            $QUERY=nemertea.fasta
    else
        $QUERY=nematode.fasta
    fi

    echo $QUERY #used to check if the variable was set correctly

nohup blastn -query $QUERY -subject $dir/spades_output/contigs.fasta -out $dir/spades_output/mito_blast

done

#command from linux --> ./auto_mito_blast.sh SAMPLE_ANN-B4

#error message : 

#./auto_mito_blast.sh: line 7: Sample_ANN-B4/: Is a directory
#./auto_mito_blast.sh: line 10: Sample_ANN-B4/: Is a directory
#./auto_mito_blast.sh: line 14: =nematode.fasta: command not found

Thanks, Joe谢谢,乔

Double brackets [[ should do the work.双括号[[应该可以完成工作。 => =>

for dir in $@
do
    QUERY=''
    if [[ $dir == SAMPLE_ANN* ]]
        then
            QUERY=annelids.fasta
    elif [[ $dir == SAMPLE_NEM* ]]
        then
            QUERY=nemertea.fasta
    else
        QUERY=nematode.fasta
    fi
    echo $QUERY #used to check if the variable was set correctly
done

ATM: you are just iterating over all given parameter, but as far as i understand you want to iterate over all directories in the current directory, which would be something like: ATM:你只是迭代所有给定的参数,但据我所知你想迭代当前目录中的所有目录,这将是这样的:

for dir in ./*

Still need some is dir check.仍然需要一些是目录检查。 Another way would be using find like另一种方法是使用 find like

find . -type d -depth 1 --execute yourscript {} \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM