为什么我不能在Bash For-loop中使用Unix Nohup?

nev*_*int 30 unix linux bash nohup

例如,此行失败:

$ nohup for i in mydir/*.fasta; do ./myscript.sh "$i"; done > output.txt&
-bash: syntax error near unexpected token `do
Run Code Online (Sandbox Code Playgroud)

什么是正确的方法呢?

Jon*_*ler 73

因为'nohup'需要单字命令及其参数 - 而不是shell循环结构.你必须使用:

nohup sh -c 'for i in mydir/*.fasta; do ./myscript.sh "$i"; done >output.txt' &
Run Code Online (Sandbox Code Playgroud)

  • 这将覆盖每个文件的output.txt吗?如果其中有重要信息不希望被覆盖,我将使用“ >>”代替“>”。 (2认同)

msw*_*msw 9

你可以在一条线上完成,但你明天也可能想要这样做.

$ cat loopy.sh 
#!/bin/sh
# a line of text describing what this task does
for i in mydir/*.fast ; do
    ./myscript.sh "$i"
done > output.txt
$ chmod +x loopy.sh
$ nohup loopy.sh &
Run Code Online (Sandbox Code Playgroud)

  • 除非`loopy.sh`在路径中,否则你需要像`./ loopy.sh`那样调用它,至少在我刚试过的Red Hat系统上. (3认同)

Mar*_*tin 5

对我来说,乔纳森(Jonathan)的解决方案无法正确重定向到output.txt。这个效果更好:

nohup bash -c 'for i in mydir/*.fasta; do ./myscript.sh "$i"; done' > output.txt &