bash-使用子 shell 遍历文件参数

bash-looping through file arguments using subshell

我在遍历下标中的脚本时遇到了一些问题。问题似乎是 i 运行 it,它只迭代我的 $@ 列表的第一个索引。我认为它会停止并等待更改,然后再继续下一个文件。我的下标的目的是使用我的 "single file script" 同时遍历多个文件。如果我更改我的文件,它会继续到下一个索引,但如果我愿意,它不会。我希望我说清楚了。 谢谢!

脚本 1:

#!/bin/bash

timestamp() {
  date +"%T"
}

FILE=
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

脚本 2:

    #!/bin/bash

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

while : 
do

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

sleep 3

done

一般来说,shell 脚本会等待每个命令完成,然后再继续下一个命令。这就是你的 script2 中发生的事情

sh filkontroll.sh "$i"

并在继续之前等待它完成。

如果你想 运行 同时输入多个,你应该把命令放在后台,把 & 放在行的末尾,比如

sh filkontroll.sh "$i" &

或者考虑像 GNU parallels 这样的工具来进行并行化