Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LinuxBash脚本中使用if和else检查目录名并分配变量_Linux_Bash_Shell_Variables_Directory - Fatal编程技术网

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

在LinuxBash脚本中使用if和else检查目录名并分配变量,linux,bash,shell,variables,directory,Linux,Bash,Shell,Variables,Directory,我正在创建(我认为是)一个简单的脚本来检查我当前的目录,其中包含了更多有名称的目录 SAMPLE_ANN-A10, SAMPLE_ANN-B4 etc. SAMPLE_NEM-E7, SAMPLE_NEM-H2, etc. SAMPLE_TODE-H02 etc. etc. 我想为每个示例分配一个查询变量并运行一个命令。所有TODE将使用相同的变量,所有NEM将使用相同的变量,所有ANN将使用相同的变量 这是我当前的脚本和错误消息。谢谢你的关注 谢谢, 乔双括号[[应该做这项工作]=>

我正在创建(我认为是)一个简单的脚本来检查我当前的目录,其中包含了更多有名称的目录

SAMPLE_ANN-A10, SAMPLE_ANN-B4 etc.
SAMPLE_NEM-E7, SAMPLE_NEM-H2, etc.
SAMPLE_TODE-H02 etc. etc.
我想为每个示例分配一个查询变量并运行一个命令。所有TODE将使用相同的变量,所有NEM将使用相同的变量,所有ANN将使用相同的变量

这是我当前的脚本和错误消息。谢谢你的关注




谢谢, 乔

双括号[[应该做这项工作]=>

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:您只是迭代所有给定参数,但据我所知,您希望迭代当前目录中的所有目录,这类似于:

for dir in ./*
还需要一些是直接检查。 另一种方法是使用find-like

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

find.-type d
将查找当前目录下的所有目录。若要仅查找当前目录中的目录,请使用深度参数:
find.-type d-depth 1--执行…
@StephenP谢谢。更新了答案。问题是
if
测试紧随其后的命令的退出代码;它正在尝试执行n
$dir
(不管该值是什么)作为一个命令,带有参数
=
和shell glob
示例的扩展。\u ANN*/
。您需要的是使用程序
test
,即/bin/test或shell内置,或用于测试的别名
[/code>(/bin/[或内置)或者,像SleepProgger一样,使用bash“关键字”
[[
(注意[[是不可移植的,但这通常不是问题,因为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
done
for dir in ./*
find . -type d -depth 1 --execute yourscript {} \;