Ben Coughlan
Ben Coughlan

Reputation: 565

working on a file from outside the directory

I'm trying to create a .info file for eact .prt file.

but I need to do ir from a different directory, and it will need to point to a new directory each time.

so far I have

#!/bin/bash
DP_DATE= cat /tmp/dp_date.txt
FOLDER= "/transfer/report_archive/preprod/$DP_DATE/"

for f in $FOLDER
do
for file in *.prt
do 
    [ -f ".info.$file" ] || wc -c < "$file" >> ".info.$file"
done
done

I'm not sure if this can work at all!

any help would be much appreciated

Upvotes: 0

Views: 46

Answers (2)

Barmar
Barmar

Reputation: 781592

You can't have space after = in an assignment. And to assign the output of a command, you have to wrap it with $(...) or backticks.

To deal with the files being in another directory, the simplest way is to cd to the directory before processing it. I do it in a subshell inside the loop, so that before the next directory is processed it will return to the original directory.

#!/bin/bash
DP_DATE=$(cat /tmp/dp_date.txt)
FOLDER="/transfer/report_archive/preprod/$DP_DATE/"

for f in $FOLDER
do ( # Subshell so cd is temporary
    cd "$f"
    for file in *.prt
    do 
        [ -f ".info.$file" ] || wc -c < "$file" >> ".info.$file"
    done
   )
done

Upvotes: 1

l0b0
l0b0

Reputation: 58908

You have to wrap command substitutions in $( and ):

DP_DATE=$(cat /tmp/dp_date.txt)

Spaces after the equals sign will result in an empty variable and trying to execute the second parameter:

FOLDER="/transfer/report_archive/preprod/$DP_DATE/"

Upvotes: 0

Related Questions