Adrian,
The below statement is giving you access to all and each one of the files in the input directory ($1), except directories. Not only the first file as you have been thinking.
--------------------------------------------------------------
for inputFile in `ls -l $1 | grep "^-" | awk '{print $(NF)}'`
do
---------------------------------------------------------------
Your C program redirects the standard input and output through < and > symbols. So inside your C program you don't need to open and close files. This is the reason I say the program doesn't need to know anything about filenames.
---------------------------------------------------------------------------
if yourCProgram < $1/$inputFile > $2/$inputFile
---------------------------------------------------------------------------
If the execution of your C program is okay the file in progress is removed.
----------------------------------------------------------------------------------
then rm $1/$inputFile
----------------------------------------------------------------------------------
Putting everything together
for inputFile in `ls -l $1 | grep "^-" | awk '{print $(NF)}'`
do
if yourCProgram < $1/$inputFile > $2/$inputFile
then rm $1/$inputFile
fi
done
With a lot of help for all of you and some others, I have been working on a script that takes the other approach. It does not seem to work yet, but here it is:
Code:
#!/bin/bash
# I discovered this path by entering this command: which bash
# setup test dirs and files
d1="dir1"
d2="dir2"
cd /tmp
rm -rf $d1
rm -rf $d2
mkdir $d1
touch $d1/f1
touch $d1/f2
mkdir $d2
# define function
function func1
{
fname=$1
echo "in func1, parm fname=$fname"
}
# show current dir state
echo
echo "before looping through files"
echo "------------------------"
echo "$d1 contents"
ls $d1
echo "$d2 contents"
ls $d2
echo
echo "looping through files in $d1"
echo "------------------------"
# loop through files in d1
for f in $(ls -1 $d1)
do
# call function
func1 $f
# move file to d2
mv $d1/$f $d2/
break
done
# confirm results of moved files
echo
echo "after looping through files"
echo "------------------------"
echo "$d1 contents"
ls $d1
echo "$d2 contents"
ls $d2
echo "------------------------"
echo "done"
James Adrian
jim@futurebeacon.com
Last edited: