BASH 语句单独执行但返回“no such file”在 for 循环中

发布于 2024-08-26 07:49:52 字数 889 浏览 6 评论 0原文

又一个我找不到答案,感觉我已经疯了。
我有一个 BASH 脚本,使用 for 循环在很多文件(~5000)上运行复杂的命令(许多蛋白质序列比对)。循环生成的语句将在单独给出时执行(即从错误消息复制粘贴到命令提示符),但在循环内返回“没有这样的文件或目录”。下面的脚本;实际上还有几个参数,但这包括一些代表性的参数和文件参数。 <代码>

#!/bin/bash

# 将目标目录作为 FASTA 序列作为参数传递。 # psiblast 的参数 # 常见的 db=本地/db/nr/nr outfile="/mnt/scratch/psi-blast" e=0.001 线程=8 itnum=5 pssm="/mnt/scratch/psi-blast/pssm。" pssm_txt="/mnt/scratch/psi-blast/pssm。" 伪=0 pwa_inclusion=0.002

对于 ${1}/* 中的 i 做 文件名=$(基本名$i) “本地/ncbi-blast-2.2.23+/bin/psiblast\ -查询${i}\ -db $db\ -out ${outfile}/${文件名}.out\ -评估$e\ -num_threads $线程\ -num_iterations $itnum\ -out_pssm ${pssm}$文件名\ -out_ascii_pssm ${pssm_txt}${文件名}.txt\ -伪计数$伪\ -inclusion_ethresh $pwa_inclusion" 完毕

运行此脚本会给出“行<'done'之前的最后一行>:<尝试的命令>:没有这样的文件或目录。如果我将尝试的命令粘贴到提示符上,它将运行。
每个命令都需要几分钟才能运行。

Another one I can't find an answer for, and it feels like I've gone mad.

I have a BASH script using a for loop to run a complex command (many protein sequence alignments) on a lot of files (~5000). The loop produces statements that will execute when given alone (i.e. copy-pasted from the error message to the command prompt), but which return "no such file or directory" inside the loop. Script below; there are actually several more arguments but this includes some representative ones and the file arguments.

#!/bin/bash

# Pass directory with targets as FASTA sequences as argument. # Arguments to psiblast # Common db=local/db/nr/nr outfile="/mnt/scratch/psi-blast" e=0.001 threads=8 itnum=5 pssm="/mnt/scratch/psi-blast/pssm." pssm_txt="/mnt/scratch/psi-blast/pssm." pseudo=0 pwa_inclusion=0.002

for i in ${1}/* do filename=$(basename $i) "local/ncbi-blast-2.2.23+/bin/psiblast\ -query ${i}\ -db $db\ -out ${outfile}/${filename}.out\ -evalue $e\ -num_threads $threads\ -num_iterations $itnum\ -out_pssm ${pssm}$filename\ -out_ascii_pssm ${pssm_txt}${filename}.txt\ -pseudocount $pseudo\ -inclusion_ethresh $pwa_inclusion" done

Running this scripts gives "<scriptname> line <last line before 'done'>: <attempted command> : No such file or directory. If I then paste the attempted command onto the prompt it will run.

Each of these commands takes a couple of minutes to run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的轨迹 2024-09-02 07:49:52

尝试不带引号。你忘记了一些斜杠。

for i in ${1}/*
do
filename=$(basename $i)
local/ncbi-blast-2.2.23+/bin/psiblast \
 -query "${i}" \
 -db "$db" \
 -out "${outfile}/${filename}.out" \
 -evalue "$e" \
 -num_threads "$threads" \
 -num_iterations "$itnum" \
 -out_pssm "${pssm}/$filename" \
 -out_ascii_pssm "${pssm_txt}/${filename}.txt" \
 -pseudocount "$pseudo" \
 -inclusion_ethresh "$pwa_inclusion"
done

try without the quotes. and you forgot some slashes.

for i in ${1}/*
do
filename=$(basename $i)
local/ncbi-blast-2.2.23+/bin/psiblast \
 -query "${i}" \
 -db "$db" \
 -out "${outfile}/${filename}.out" \
 -evalue "$e" \
 -num_threads "$threads" \
 -num_iterations "$itnum" \
 -out_pssm "${pssm}/$filename" \
 -out_ascii_pssm "${pssm_txt}/${filename}.txt" \
 -pseudocount "$pseudo" \
 -inclusion_ethresh "$pwa_inclusion"
done
意中人 2024-09-02 07:49:52

如果您正在迭代的文件名中有空格,就会发生您所观察到的行为。因此,您需要正确引用文件名,如以下最小示例所示:

#!/bin/bash
for i in *
do
  filename="$(basename "$i")"
  command="ls -lah '$filename'"
  echo "filename=$filename"
  echo "Command = $command"
  eval "$command"
done

The behavior you're observing will occur if there are spaces in the filenames you're iterating over. For this reason, you'll want to properly quote your filenames, as in the following minimal example:

#!/bin/bash
for i in *
do
  filename="$(basename "$i")"
  command="ls -lah '$filename'"
  echo "filename=$filename"
  echo "Command = $command"
  eval "$command"
done
征﹌骨岁月お 2024-09-02 07:49:52

使用 for 循环时,向文件名添加引号不会有帮助。为了克服这个问题,每当我需要循环文件名时,我总是执行类似于以下示例的操作:

ls -1 directory | { while read line; do echo $line; done; }

Adding quotes to filenames will not help when using a for loop. To overcome this, I've always done something similar to the following example whenever I needed to loop over filenames:

ls -1 directory | { while read line; do echo $line; done; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文