Sponsored Content
Top Forums UNIX for Advanced & Expert Users Redirect contained in variable not expanded as expected Post 302367769 by noose on Tuesday 3rd of November 2009 11:37:21 AM
Old 11-03-2009
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 fineSmilie. If I leave the variable in there then I get the following errorSmilie:
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

please help: how to redirect input into a variable

I'm trying to write a simple script program (C shell). I have a problem redirecting input into a variable. Say I have a variable called J, and there is file called result which contains just some number, say 5. Which command should I use to assign J value 5 from the file result. I tried the... (2 Replies)
Discussion started by: artur80
2 Replies

2. UNIX for Dummies Questions & Answers

Redirect Output In Variable

how make assign the output of the command (for example: grep "file" "string" ) in a variable ($name)? i thing how put the result of the command (grep , cut, find ecc) in a variable.. IT's Possible ?? (1 Reply)
Discussion started by: ZINGARO
1 Replies

3. UNIX for Dummies Questions & Answers

Redirect to variable

how do you redirect stdout into a variable. whenever I try I get an ambiguous redirect error :( I am trying to validate some user input and failing miserably. cal $MONTH $YEAR | grep -c "$DAY" if the above is 1 then it is valid if 0 then not valid. I have been trying to redirect the output... (2 Replies)
Discussion started by: MrAd
2 Replies

4. Shell Programming and Scripting

after grep i want to redirect it to some variable

for the below grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1 the result of the grep i want to redirect into some variable, i tried to do veri=grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1 but it is... (2 Replies)
Discussion started by: mail2sant
2 Replies

5. Shell Programming and Scripting

redirect cat to variable

hello just i saw a really strange for cat i have file (file1) contains line /home/rajiv/proj1/*.txt now applied a commonds DDPATH="$(cat file1)" echo $DDPATH it shows all the txt files in that folder like /home/rajiv/proj1/read1.txt /home/rajiv/proj1/read2.txt... (7 Replies)
Discussion started by: shailesh_arya
7 Replies

6. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

7. UNIX for Dummies Questions & Answers

redirect for "[: : integer expression expected" error

Hi, I read in this forum that for "https://www.unix.com/shell-programming-scripting/156008-integer-expression-expected-regular-expression.html thanks! (7 Replies)
Discussion started by: Vijay81
7 Replies

8. Shell Programming and Scripting

Using sed in a loop/to remove lines contained in variable from file

I've tried numerous commands, but I am not sure how to use sed in a loop. This is what I have: VARZ contains CARD_FILE_LIST and it also contains CARD_FILE_LIST2 so echo "$VARZ" CARD_FILE_LIST CARD_FILE_LIST2 I have a file with 60 lines in /tmp/testfile it and I want those lines deleted... (3 Replies)
Discussion started by: newbie2010
3 Replies

9. Shell Programming and Scripting

Assigning variable to output gives error with expected result

Hello, I am trying to print out the first string matching query with grep and I need your help. My scenario: Database John F 4433 Street No 88 CA Elisabeth Taylor 7733 Street No 26 ON Jack Nicholson 0133 Green Park No 34 AR John F 2 9399 Southpark No 02D UT test.sh... (6 Replies)
Discussion started by: baris35
6 Replies

10. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies
DtMsgLogOpenFile(library call)											    DtMsgLogOpenFile(library call)

NAME
DtMsgLogOpenFile -- opens a log file SYNOPSIS
#include <Dt/MsgLog.h> FILE* DtMsgLogOpenFile( const char* type, char** filename_return); DESCRIPTION
The DtMsgLogOpenFile function calls fopen to open a log file with type open mode. If fopen opens a log file successfully and returns a non- NULL filename_return, DtMsgLogOpenFile calls malloc to allocate space for filename_return. DtMsgLogOpenFile then copies the log file name to filename_return. The caller must use the free function to release the space allocated for filename_return, if it is not NULL. The caller must also close the file pointer returned by DtMsgLogOpenFile. Use caution in doing this because stderr may be returned. For a description of the algorithm used to determine the log file to open, see DtMsgLogMessage(3). Note that if a log file cannot be opened, filename_return will be set to NULL. ARGUMENTS
type Specifies the file open flag. See fopen(3) for more information. filename_return Specifies the variable to receive the returned log file name. ENVIRONMENT VARIABLES
None. RESOURCES
None. ACTIONS
/MESSAGES None. ERRORS
/WARNINGS None. EXAMPLES
The following code fragment opens the log file, logs the log file name, closes the file, and frees the space allocated for the file name. char * log_file; FILE * fp = DtMsgLogOpenFile ("a+", &log_file); if (log_file) { DtMsgLogMessage (argv[0], DtMsgLogInformation, "The log file name is: %s", log_file); free (log_file); } if (fp && fp != stderr) fclose (fp); RETURN VALUE
If DtMsgLogMessage completes successfully, it returns a pointer to the opened log file. If it cannot open a log file, it returns stderr. FILES
None. SEE ALSO
DtMsgLogMessage(3), DtMsgLogSetHandler(3) DtMsgLogOpenFile(library call)
All times are GMT -4. The time now is 05:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy