1

我正在尝试运行一个名为 BLASTP 的生物程序,它接收两个字符串(代码中的 fasta_GWIDD 和 fasta_UNIPROT)并比较它们。我遇到的问题是在代码中使用了 echo/system。谁能建议我错过了什么?

    for(i=0;i<index1;i++)
    {
        sprintf(fasta_GWIDD,">%s\\n%s\n",fasta_name1[i],fasta_seq1[i]);
        setenv("GwiddVar", fasta_GWIDD, 1) ;
        sprintf(fasta_UNIPROT,">%s\\n%s\n",fasta_name2[i],fasta_seq2[i]);
        setenv("UniprotVar", fasta_UNIPROT, 1) ;
        system("blastp -query <(echo -e $GwiddVar) -subject<(echo -e $UniprotVar)");
    }

错误是:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `blastp -query <(echo -e $GwiddVar) -subject<(echo -e $UniprotVar)'
4

2 回答 2

5

好像shell看不懂

<(echo -e $GwiddVar)

句法。请注意,该system命令可能使用不同于您习惯的 shell(例如 csh 而不是 bash,等等)。它是您操作系统配置文件和配置文件中某处的所有内容,但我猜不出您有什么。

顺便提一句。我认为您应该能够通过以下任一方式检查system()命令正在使用哪个 shell:

system("echo $SHELL")  // should simply write the path to current shell
system("ps -aux")      // look at it and find what is the parent of the PS

等等

考虑到这在某些外壳上是正确的:

blastp -query <(echo -e $GwiddVar) -subject<(echo -e $UniprotVar)

上面引用的语法显然只是为了将变量作为输入传递。我认为你做得过火了。您正在使用echo -e $GwiddVar打印和捕获数据,这些数据已经在您手头的变量中。您是否尝试过以下简单的方法:

blastp -query $GwiddVar -subject $UniprotVar

我不知道您要使用哪个外壳,但考虑到它echo得到了它的数据,那么它应该是完全相同的。

如果您担心空格,那么各种 shell 通常允许您使用引号:

blastp -query "$GwiddVar" -subject "$UniprotVar"

当然,这取决于外壳。如果您的程序使用不喜欢引号的 shell,那么您必须适应它。不是针对您的外壳,而是针对system()已使用的外壳。

另一件事是使用system非常粗糙。当您有难以正确转义的参数时,您应该使用其他函数,例如 execve,它们能够获取一组真正的原始直接字符串并将它们作为 ARGV 直接传递给进程。使用这些,您将不需要(也不应该)在要传递的字符串中添加任何引号或转义任何空格。

sprintf(fasta_GWIDD,">%s\\n%s\n",fasta_name1[i],fasta_seq1[i]);
sprintf(fasta_UNIPROT,">%s\\n%s\n",fasta_name2[i],fasta_seq2[i]);

char** args = .....; // allocate an array of char*[5], malloc, or whatever
args[0] = "blastp";
args[1] = "-query";
args[2] = fasta_GWIDD;
args[3] = "-subject";
args[4] = fasta_UNIPROT;

int errcode = execve(4, args, null);
if( errcode ) ... // check the error (if any) and react

然而!请注意, execve 来自 exec 系列,因此它会替换您当前的进程。这就是为什么我只写一个草图而不显示整个准备运行的代码。您可能需要fork()在它之前,然后在外循环中等待孩子。

所以,我先检查一下shell和语法;)

于 2014-06-02T19:15:50.743 回答
0

来自man 3 system

DESCRIPTION
    system()  executes a command specified in command by calling /bin/sh -c
    command, and returns after the command has been completed.

在许多系统上,/bin/shis not bash,即使是,它也是不同的配置bashbash如果它被调用,则通常以不同的方式调用/bin/sh)。因此,您将bash语法传递给一个不是bash,或者不允许完整的bash-isms 集的 shell ......此外,之后缺少一个空格-system也可能会造成混淆......而且,我不完全确定环境变量在system()字符串中扩展......

于 2014-06-02T19:17:09.850 回答