bash-looping through file arguments using subshell

71 views Asked by At

I'm having some trouble with iterating through my script within my subscript. The problem seems to be that one i run it, it only iterates over the first index of my $@ list. I'm thinking it stops and waits for a change before it continues to the next file. The point of my subscript is to go over multiple files at the same time using my "single file script". If i change my file, it continues to the next index but not if i let it be. I hope i made my self clear. Thank you!

Script 1:

#!/bin/bash

timestamp() {
  date +"%T"
}

FILE=$1
timeer=$(stat -f "%Sm" $FILE)
myDate=$(date +%b" "%d" "%H:%M:%S" "%Y)
    if [ -f $FILE ]; then
        timestamp
        echo "Filen{$FILE} ble opprettet"
    fi
while :
do
    Atimeer=$(stat -f "%Sm" $FILE)

        if [[ "$Atimeer" != "$timeer" && -f $FILE ]]; then
            timestamp
            echo "Filen ble endret!!!" 
            exit 130
    fi

   if [ ! -f "$FILE" ]; then
     echo "Filen {$FILE} ble slettet.."
     exit 130
    fi
done

script 2:

    #!/bin/bash

if [ $# -lt 1 ]; then
    echo "Not enough arguments, Call this script with"
    echo "Write: ${0} <file.name>"
    exit 1
fi

while : 
do

    for i in "${@}"
    do
    echo $i
    sh filkontroll.sh "$i"
    done

sleep 3

done
1

There are 1 answers

5
Eric Renouf On BEST ANSWER

In general a shell script will wait for each command to finish before moving on to the next one. That's what's happening in your script2 it hits

sh filkontroll.sh "$i"

and waits for that to finish before moving on.

If you want to run multiple in at the same time you should put the command in the background by putting & at the end of the line like

sh filkontroll.sh "$i" &

or consider a tool to do parallelization like GNU parallels