Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 330
neversaint Avatar asked Jun 23 '10 05:06

neversaint


People also ask

Can you nohup a bash script?

At its most basic, nohup can be used with only a single argument, the name of the script / command that we want to run. For example if we had a Bash script called test.sh we could run it as so. If the script / command produces standard output, then that output is written to nohup.

How do I run a nohup loop?

jmoh/nohup with for loop Example of nohup command when using a for-loop. Basically, put for-loop statement in-between quotes and then throw "bash -c" in front of it.

How do I run a shell script with nohup command?

To run a nohup command in the background, add an & (ampersand) to the end of the command. If the standard error is displayed on the terminal and if the standard output is neither displayed on the terminal, nor sent to the output file specified by the user (the default output file is nohup. out), both the ./nohup.

Can you do for loops in bash?

A bash for loop is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop.


2 Answers

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

nohup sh -c 'for i in mydir/*.fasta; do ./myscript.sh "$i"; done >output.txt' & 
like image 91
Jonathan Leffler Avatar answered Oct 05 '22 17:10

Jonathan Leffler


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 & 
like image 39
msw Avatar answered Oct 05 '22 17:10

msw