Richard Rublev Richard Rublev - 5 months ago 13
Bash Question

How to write shell script,to automate file conversion?

I have 30 files(ascii),which I want convert to binary.Linux command line(FORTRAN 77 CODE) that has been compiled

./rec_binary


Relevant part of the code

character*72 ifname,ofname
c
write(*, fmt="(/'Enter input file name')")
read(5,85) ifname
85 format(a72)
write(*, fmt="(/'Enter output file name')")
read(5,85) ofname


Then code asks for input and output file names

Enter input file name
rec01.txt

Enter output file name
rec.01


How to automate this?I have tried like this

#!/bin/csh -f
set list = 'ls rec*.txt'
foreach file ($list)
rec_binary ${file} >


Or

#!/bin/sh
for f in .txt
do
./rec_binary F
done


But I do not have a clue for next step.Text files are

rec01.txt
rec02.txt

rec30.txt


Output files

rec.01
rec.02

rec.30

Answer

Try this:

#!/bin/bash
for each in `ls rec*.txt`
do
  op_file=$(echo $each | sed 's/\(rec\)\([0-9]*\).txt/\1\.\2/')
  ./rec_binary <<EOF
$each
$op_file
EOF
done

The variable op_file converts your rec01.txt to rec.01.