user1709091
user1709091

Reputation:

Bash script with a loop not executing utility that has paramters passed in?

Anyone able to help me out? I have a shell script I am working on but for the loop below the command after "echo "first file is $firstbd" is not being executed.. the $PROBIN/proutil ?? Not sure why this is...

Basically I have a list of files in a directory (*.list), I grab them and read the first line and pass it as a parameter to the cmdlet then move the .list and the content of the .list to another directory (the .list has a list of files with full path).

for i in $(ls $STAGEDIR/*.list); do
echo "Working with $i"
# grab first .bd file
firstbd=`head -1 $i`
echo "First file is $firstbd"
$PROBIN/proutil $DBENV/$DBNAME -C load $firstbd tenant $TENANT -dumplist $STAGEDIR/$i.list >> $WRKDIR/$i.load.log
#move the list and its content to finished folder
binlist=`cat $i`
for movethis in $binlist; do
    echo "Moving file $movethis to $STAGEDIR/finished"
    mv $movethis $STAGEDIR/finished/
done
echo "Finished working with list $i"
echo "Moving it to $STAGEDIR/finished"
mv $i $STAGEDIR/finished/
done

The error I was getting is..

./tableload.sh: line 107: /usr4/dlc/bin/proutil /usr4/testdbs/xxxx2 -C load /usr4/dumpdir/xxxxx.bd tenant xxxxx -dumplist /usr4/dumpdir/PUB.xxxxx.list >> /usr4/dumpdir/PUB.xxxx.list.load.log: A file or directory in the path name does not exist... however if I run "/usr4/dlc/bin/proutil"

The fix was to remove ">> $WRKDIR/$i.load.log".. the binary utility wouldn't run when trying to output results to file.. strange..

Upvotes: 1

Views: 112

Answers (1)

glenn jackman
glenn jackman

Reputation: 247012

A couple of really bad practices here

  • parse the output of ls
  • not quoting variables
  • iterating the lines of a file with cat and for

As shelter comments, you don't check that you've created all the directories in the path for your log file.

A rewrite:

for i in "$STAGEDIR"/*.list; do
    echo "Working with $i"
    # grab first .bd file
    firstbd=$(head -1 "$i")
    echo "First file is $firstbd"
    # ensure the output directory exists
    logfile="$WRKDIR/$i.load.log"
    mkdir -p "$(dirname "$logfile")"
    "$PROBIN"/proutil "$DBENV/$DBNAME" -C load "$firstbd" tenant "$TENANT" -dumplist "$STAGEDIR/$i.list" >> "$logfile"
    # move the list and its content to finished folder
    while IFS= read -r movethis; do  
        echo "Moving file $movethis to $STAGEDIR/finished"
        mv "$movethis" "$STAGEDIR"/finished/
    done < "$i"
    echo "Finished working with list $i"
    echo "Moving it to $STAGEDIR/finished"
    mv "$i" "$STAGEDIR"/finished/
done

Upvotes: 1

Related Questions