The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirect to a Variable? Or is it cmd cap? oxoxo UNIX for Dummies Questions & Answers 2 11-22-2008 08:59 AM
Redirect Query o/p to variable Swapna173 UNIX for Dummies Questions & Answers 2 08-01-2008 07:48 AM
redirect cat to variable shailesh_arya Shell Programming and Scripting 7 07-10-2008 05:34 AM
Redirect to variable MrAd UNIX for Dummies Questions & Answers 2 05-07-2007 05:18 PM
please help: how to redirect input into a variable artur80 Shell Programming and Scripting 2 11-17-2002 10:18 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
noose noose is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
Question 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.

Code:
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:

Code:
#!/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:
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.
  #2 (permalink)  
Old 4 Weeks Ago
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
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.

Code:
eval "${line}"

  #3 (permalink)  
Old 4 Weeks Ago
noose noose is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
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.
Reply

Bookmarks

Tags
redirection, variable expansion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0