为什么我不能将 Unix Nohup 与 Bash For-loop 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3099092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:23:26  来源:igfitidea点击:

Why can't I use Unix Nohup with Bash For-loop?

linuxbashunixnohup

提问by neversaint

For example this line fails:

例如这一行失败:

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

What's the right way to do it?

正确的做法是什么?

回答by Jonathan Leffler

Because 'nohup' expects a single-word command and its arguments - not a shell loop construct. You'd have to use:

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

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

回答by msw

You can do it on one line, but you might want to do it tomorrow too.

您可以在一条线上完成,但您可能明天也想这样做。

$ 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 &

回答by Martin

For me, Jonathan's solution does not redirect correctly to output.txt. This one works better:

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

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

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