1

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
ruubel
  • 153
  • 1
  • 10

1 Answers1

0

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

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • btw does it run in the background for ever? cause my mac is running very intense right now. I ran "top" in terminal and i saw four sh processes going even after i quit the script? – ruubel Sep 04 '17 at 13:03
  • @ruubel it'll run in the background until it exits, either naturally or is killed. In your script you could get a LOT of them since you start them all, wait 3 seconds then start them all again and continually do so If it takes longer than 3 seconds for any of them to finish you'll end up with more and more until the system just can't keep up any more – Eric Renouf Sep 04 '17 at 13:07
  • Ooh i get it! No i changed it to 60 sec. Can i use a kill switch in the script or how should i cancel them out When Im Done? – ruubel Sep 04 '17 at 13:31
  • One possibility is the advice in the answers to [this question](https://stackoverflow.com/questions/360201/how-do-i-kill-background-processes-jobs-when-my-shell-script-exits) – Eric Renouf Sep 04 '17 at 13:34
  • Ok, thank you for all the help. Really appriciate it! – ruubel Sep 04 '17 at 13:36