Register
It is currently Mon Jul 28, 2014 6:35 pm

using eval to launch background subshell?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Apr 14, 2009 12:39 pm   

Joined: Tue Apr 14, 2009 12:04 pm
Posts: 1
I am trying to write a script that would run a series of programs many times. Namely, there will be about 4 programs run in series, each taking about 1 hour. I want to carry this out about 40 times with different input files, variable values, etc.

Also, I am running this on an 8-processor computer, so I would like to carry these jobs out in parallel, by running 8 jobs at a time, then the next 8 jobs, then the next 8 jobs, and so on.

If I were running a single command, then I could

Code:
for (( j = 1; j<=40; j+=1))
do
  if [ $(( $j % 8 )) -eq 0 ]
    then
    backgroundchar="&"
    else
    backgroundchar=""
  fi

  eval command1 $backgroundchar
   
done


The reason for including
Code:
eval command1 $backgroundchar
is to make sure that the shell interprets the $backgroundchar as an actual "&".

Otherwise, if it is run as simply "command1 $backgroundchar", an error will be reported before the variable is expanded (I think this is the reason for the error).

But what if I want to run multiple subshells, using the ( ... ) & construction? What I would like to do is this:
Code:
for (( j = 1; j<=40; j+=1))
do
  if [ $(( $j % 8 )) -eq 0 ]
    then
    backgroundchar="&"
    else
    backgroundchar=""
  fi

  (
    command1
    command2
    command3
  ) $backgroundchar
   
done


But this gives an error. Using "eval" does not work either:
Code:
$ av="&"
$ eval ( echo "a"; sleep 1; echo "b"; sleep 1; echo "c"; sleep 1; echo "d" ) $av
-bash: syntax error near unexpected token `echo'
$


Is there a way I can begin a subshell, using a variable value to determine whether to run it as
( ... ) as opposed to ( ... ) & ?


Top
 Profile  
 PostPosted: Tue Apr 21, 2009 1:05 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Why not simply use a function...
What you intend to do is nonsense in bash, as bash can not parallelize processes...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP