LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2006, 02:48 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Bash I/O redirection problem


I'm having an issue with a bash script I wrote yesterday that I can't figure out. I need to run a series of experiments, so I wrote this script which calls the Perl script that performs the experiment. The Perl script writes the experimental results to stdout, so I redirect that output to a file that is named individually for each experiment.


The problem is that the redirection on the `eval` line in the else block seems to be directing all output to the same file. I've tried removing the eval, and then what I believe happened is that the files captured the output from this bash script, but the output from the Perl script was nowhere to be found. Here is the code below:

Code:
### Subroutine which runs an experiment for each 
# Parameter #1: The location of the performance data to use
# Parameter #2: The location of the results set to use
# Parameter #3: The experiment name, used also in the name of the output file
perform_experiment() {

	for i in `seq 1 9`
	do
		out_file="$out_dir/$3.${name_opts[i]}.cdf"	
		if [ -f $out_file ];
		then
			echo "WARNING: File $out_file exists! I will not overwrite it"
		else
			echo "$run_exe -d $1 -r $2 ${score_opts[i]} > $out_file"
			eval "$run_exe -d $data_dir/ -r $results_dir/ ${score_opts[i]} > $out_file"
		fi
	done
}
I call this function multiple times to perform each experiment. My question is: what do I need to do to get the `eval` statement to properly re-direct the output from that invocation of the Perl script to stdout? I'm stumped. Thanks
 
Old 10-26-2006, 07:50 PM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I think the problem is elsewhere, and that you do not need eval.

Here's a simplified version of your function and a driver:
Code:
#!/bin/sh

# @(#) s1       Demonstrate output re-direction.

### Subroutine which runs an experiment for each
# Parameter #1: The location of the performance data to use
# Parameter #2: The location of the results set to use
# Parameter #3: The experiment name, used also in the name of the output file
perform_experiment() {

  for i in `/usr/bin/seq 1 9`
  do
    out_file="t${i}"
    echo " Outfile for iteration $i is :$out_file:"
    if [ -f $out_file ];
    then
      echo "WARNING: File $out_file exists!"
    else
      echo echo hi >$out_file
    fi
  done
}

# Remove debris, create t9 as a test.

rm -f t[1-9]
touch t9
echo
echo " Files before function call:"
ls

echo
echo " Run function."
perform_experiment

echo
echo " Files after  function call:"
ls
Which produces:
Code:
% ./s1

 Files before function call:
RCS  Readme.txt  s1  t9

 Run function.
 Outfile for iteration 1 is :t1:
 Outfile for iteration 2 is :t2:
 Outfile for iteration 3 is :t3:
 Outfile for iteration 4 is :t4:
 Outfile for iteration 5 is :t5:
 Outfile for iteration 6 is :t6:
 Outfile for iteration 7 is :t7:
 Outfile for iteration 8 is :t8:
 Outfile for iteration 9 is :t9:
WARNING: File t9 exists!

 Files after  function call:
RCS  Readme.txt  s1  t1  t2  t3  t4  t5  t6  t7  t8  t9
I recommend that you put some intermediate debugging echo statements in the function to see what is going on ... cheers, makyo

( edit 1: typo )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting I/O redirection error blinux1 Programming 13 03-18-2008 08:21 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 12:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 06:13 AM
Bash redirection being taken literally by programs R00ts Programming 13 05-26-2005 12:46 AM
bash output redirection: debian vs freebsd kenners Programming 2 10-07-2004 04:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration