我有一个下面的脚本,该脚本迭代所有计算机并远程检入每台计算机:
是否存在特定目录?
并且该目录中是否存在所有2000个文件。
下面是代码:
for machine in "${MACHINES[@]}"; do
dircheck=($(ssh -o "StrictHostKeyChecking no" user@${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))
if [[ $? != 0 ]] ;then
echo "Folder $dir3 doesn't exist on $machine" >&2
exit 1
fi
# this is for checking all the 2000 files
# this check is very slow when it checks all 2000 files
for el in ${FILES[@]}
do
printf -v cmd '%q ' test -e "$dir3/process_${el}.log"
ssh user@${machine} "$cmd" \
|| { echo "File number $el missing on $machine." >&2;
exit 1; }
done
done
现在的问题是检查所有2000个文件需要花费很多时间,所以想看看是否有什么方法可以使我们做同样的事情,但是速度很快?
更新:
因此,总体而言,我的脚本将如下所示:
readonly MACHINES=(machineA machineB machineC)
readonly dir3=/some_path
echo $dir3
FILES=({0..1999})
checkFunc() {
test -d "$dir3" || echo "NODIR"
local filename successCount=0
while IFS= read -r filename; do
test -e "$dir3/process_${filename}.log" && (( ++successCount ))
done
printf '%s\n' "$successCount"
}
for machine in "${MACHINES[@]}"; do
actual=$(
printf '%s\0' "${FILES[@]}" | \
ssh "$machine" "$(declare -p dir3; declare -f checkFunc); checkFunc"
) || { echo "ERROR: Unable to retrieve remote file count" >&2; exit 1; }
case $actual in
(${#FILES[@]}) echo "SUCCESS: Expected, and found, $numberOfActuallyRemoteFiles files" ;;
(NODIR) echo "FAILURE: Directory $dir3 does not exist" ;;
(*) echo "FAILURE: Out of ${#FILES[@]} files, only $actual exist" ;;
esac
done