[ale] scripts, file I/O, and loss of hair

The Don Lachlan ale-at-ale.org at unpopularminds.org
Thu May 12 23:51:52 EDT 2011


On Thu, May 12, 2011 at 08:48:59PM -0400, Jim Kinney wrote:
> On Thu, May 12, 2011 at 6:45 PM, The Don Lachlan <ale-at-ale.org@
> unpopularminds.org> wrote:
> 
> > On Thu, May 12, 2011 at 05:57:42PM -0400, Jim Kinney wrote:
> > > that expects a {binary name} < {input data file}. {input data file} is
> > > variably named and often 20+MB. Changing {binary name} to work better is
> > > political (think Fortran 77 base code - someone in charge got their PhD
> > with
> > > that program).
> > > so things must work as they are with my little tweaks around the edges.
> > > until I blow away the decades old mess and start over in C.
> >
> > That doesn't make any sense.
> >
> > If the Fortran code expects the data as a stream (that's what the redirect
> > does), then why are you trying to get the file name in a shell script? What
> > is your objective with this?
> >
> 
> For logging the combination of which fortran binary with which input file
> I'm simulating parts of a process with a shell script. The fortran code runs
> for many hours.

Ok, so you want to modify the Fortran code to log the input file. Right?

If so, you could specify the input file twice and then have Fortan pull the
first line of input as the name of the input file.

$ script file.data < file.data

but anything after that redirect is handled separately by the shell and not
easily reachable by the Fortran code.


Regarding cat and file redirection, you're confusing things.

> I have a simple test script my_script                                                             
>                                                                                                   
> #!/bin/bash                                                                                       
> # this is a crap script                                                                           
> myout="$2"'.crap'                                                                                 
> directions=$(cat $2)                                                                              
> echo "$myout"                                                                                     
> echo "$directions"                                                                                
> exit                                                                                              
>                                                                                                   
> It is launched by:                                                                                
> ./my_script < test1.dat

That's what you originally wrote. I said use

  directions=`<${1}`

Which will work if you do "script file.data < file.data".

But you sent a whole bunch of errors here because you were trying to do
something different, mostly stuff I don't understand.

> FYI:
>      shell variable redirect
> 
> fred='foo bar'
> mary=`<${fred}`
> -bash: ${fred}: ambiguous redirect
> 
> mary=`cat ${fred}`
> cat: foo: No such file or directory
> cat: bar: No such file or directory

Where did that come from? You cat files. If foo and bar don't exist, you
can't cat them. If you're trying to pull from multiple files, then the
redirection won't work any more than "myscript < foo bar" would work.

>     get contents of variable
> mary=`echo ${fred}`
> echo $mary
> foo bar
> 
>     make a file
> echo "foo bar" >> fred
> mary=$(<$fred)
> -bash: $fred: ambiguous redirect

Isn't $fred still holding "foo bar"? You were looking for "fred".

>    read file
> cat fred
> foo bar
> 
>     redirect file contents to shell variable
>  mary=`<$fred`
> -bash: $fred: ambiguous redirect

See above.

>    create a reference (as shell var) to file to have redirect "go through"
> john='fred'
> mary=`<${john}`
> echo $mary
> foo bar
> 
> 
>     or just 'cat' the damn thing and irritate R. Schwartz again :-)
> suzy=$(cat ${john})
> echo $suzy
> foo bar

WTF, dude? You have two equivalent code blocks:

  john='fred'
  mary=`<${john}`
  echo $mary
  foo bar

versus

  john='fred'
  suzy=$(cat ${john})
  echo $suzy
  foo bar

That is *exactly* what you're doing in your test script.  The first is safer
and requires no extra work. 

-L


More information about the Ale mailing list