Redirect contained in variable not expanded as expected

Hi all,

I have a selection of files that contain several commands that write to a file and are started as background processes similar to below.

command_to_run_simulator -simulator_arguments > log_file 2>&1 &
command_to_run_simulator -simulator_arguments > log_file 2>&1 &
command_to_run_simulator -simulator_arguments > log_file 2>&1 &

I want to write a wrapper script that will read each file a line at a time, execute that line and store the PID for each line so that after a certain amount of time the processes can be killed.

I haven't implemented a full script for this but what I have written is here:

#!/bin/sh

while read line
do
 $line
  # command_to_run_simulator -simulator_arguments > log_file 2>&1 &
  echo $!
done < $1

I was expecting that each line in the file would be read output and run. If I insert the commented line, an exact copy from the original file, it works fine:). If I leave the variable in there then I get the following error:mad::
Unexpected option '>'

It seems that when $line is expanded somehow the meaning of the redirection symbol is lost. Can anyone explain how $line is expanded and why the redirection is lost? As a secondary thing, is there a better way to achieve what I'm trying to do?

Please ask if anything isn't clear or I've missed relevant information. On that note, my shell is GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu).

Many thanks,
Geoff.

I can't reproduce your error message but you need to use "eval" to execute a command you have generated after the script started running.

eval "${line}"

Thanks for that methyl, that solved my problem.

I'm still interested in the mechanics of what was going on/going wrong if anyone can explain.