使用subshel​​l通过文件参数进行bash循环

时间:2017-09-04 12:32:51

标签: bash shell loops subshell

我在下标中迭代我的脚本时遇到了一些麻烦。问题似乎是我运行它,它只迭代我的$ @列表的第一个索引。我认为它会停止并在继续下一个文件之前等待更改。我的下标的要点是使用我的“单个文件脚本”同时检查多个文件。如果我更改了我的文件,它将继续下一个索引,但如果我让它继续,则不会。我希望我清楚自己。 谢谢!

脚本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

脚本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 个答案:

答案 0 :(得分:0)

通常,shell脚本将等待每个命令完成,然后再转到下一个命令。这就是你的剧本2中发生的事情

sh filkontroll.sh "$i"

并在继续之前等待完成。

如果你想同时运行多个,你应该把&放在行的末尾,就像把

放在后面一样。
sh filkontroll.sh "$i" &

或考虑使用GNU parallels进行并行化的工具