gpt4 book ai didi

linux - 如何使用 shell 脚本检查 2 台机器中的文件夹?

转载 作者:太空狗 更新时间:2023-10-29 12:32:22 26 4
gpt4 key购买 nike

我正在编写一个 shell 脚本,我需要在 machineX 上运行它。它将在其他两台机器的 MAPPED_LOCATION 文件夹中检查格式为 YYYYMMDD 的特定文件夹 - machinePmachineQ。所以 machinePmachineQ 中的路径都是这样的-

/bat/testdata/t1_snapshot/20140311

而在上面的文件夹路径里面,里面会有一些文件。下面是我的 shell 脚本 -

#!/bin/bash

readonly MACHINES=(machineP machineQ)
readonly MAPPED_LOCATION=/bat/testdata/t1_snapshot
readonly FILE_TIMESTAMP=20140311

# old code which I was using to get the latest folder inside each machine (P and Q)
dir1=$(ssh -o "StrictHostKeyChecking no" david@${MACHINES[0]} ls -dt1 "$MAPPED_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1)
dir2=$(ssh -o "StrictHostKeyChecking no" david@${MACHINES[1]} ls -dt1 "$MAPPED_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1)

dir3=$MAPPED_LOCATION/$FILE_TIMESTAMP # /bat/testdata/t1_snapshot/20140311

echo $dir1
echo $dir2
echo $dir3

if dir3 path exists in both the machines (P and Q) and number of files is greater than zero in each machine
then

# then do something here
echo "Hello World"

else

# log an error - folder is missing or number of files is zero in which servers or both servers

fi

现在我应该做的是 - 如果此路径在两台机器中都存在 /bat/testdata/t1_snapshot/20140311 并且两台机器中的文件数都大于零,然后做点什么。否则,如果任何服务器中的文件夹丢失或任何其他服务器中的文件数量为零,我将以非零状态退出 shell 脚本并显示一条包含实际错误的消息。

如何在 shell 脚本中执行此操作?

更新:-

for machine in $MACHINES; do
dircheck=($(ssh -o "StrictHostKeyChecking no" david@${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))

#On the ssh command, we exit 1 if the folder doesn't exist. We check the return code with `$?`
if [[ $? != 0 ]] ;then
echo "Folder doesn't exist on $machine";
exit 1
fi

# check number of files retrieved
if [[ "${dircheck[@]}" = 0 ]] ;then
echo "0 Files on server $machine";
exit 1
fi
#all good for $machine here
done

echo "Everything is Correct"

如果我在 machineP 中添加一个新的空文件夹 20140411 然后执行上面的脚本,它总是打印出 -

echo "Everything is Correct"

事实上,我没有在machineQ 中添加任何文件夹。不确定是什么问题?

另一个更新-

我只在 machineP 中创建了一个空文件夹 20140411。然后我在 Debug模式下运行脚本 -

david@machineX:~$ ./test_file_check_1.sh
+ FILERS_LOCATION=(machineP machineQ)
+ readonly FILERS_LOCATION
+ readonly MEMORY_MAPPED_LOCATION=/bexbat/data/be_t1_snapshot
+ MEMORY_MAPPED_LOCATION=/bexbat/data/be_t1_snapshot
+ readonly FILE_TIMESTAMP=20140411
+ FILE_TIMESTAMP=20140411
+ dir3=/bexbat/data/be_t1_snapshot/20140411
+ echo /bexbat/data/be_t1_snapshot/20140411
/bexbat/data/be_t1_snapshot/20140411
+ for machine in '$FILERS_LOCATION'
+ dircheck=($(ssh -o "StrictHostKeyChecking no" david@${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))
++ ssh -o 'StrictHostKeyChecking no' david@machineP '[[' '!' -d /bexbat/data/be_t1_snapshot/20140411 ']]' '&&' exit 1 ';' ls -t1 /bexbat/data/be_t1_snapshot/20140411
+ [[ 0 != 0 ]]
+ [[ '' = 0 ]]
+ echo 'Everything is Correct'
Everything is Correct

最佳答案

你想要做的是,ls 远程目录(删除 -d 标志到 ls(仅列出文件夹),以及 head - n1 命令,因为它只打印第一个文件)并在数组变量中检索数据。

我还在执行 ls 之前添加了一个目录存在性检查 [[ -d "$dir3"]] 并且转义了 && 不在当前的解释bash 脚本。

[[ -d "$dir3" ]] \&\& ls -t1 "$dir3"

要定义 bash 数组,请在命令周围添加额外的 ( ),然后比较数组大小。

dir3="$MAPPED_LOCATION/$FILE_TIMESTAMP" # /bat/testdata/t1_snapshot/20140311

for machine in ${MACHINES[*]}; do
dir3check=($(ssh -o "StrictHostKeyChecking no" david@${machine} [[ -d "$dir3" ]] \&\& ls -t1 "$dir3"))

if [[ "${#dir3check[@]}" -gt 0 ]] ;then
# then do something here
echo "Hello World"
else
# log an error - folder is missing or number of files is zero in server $machine
fi
done

更新:

for machine in ${MACHINES[*]}; do
dircheck=($(ssh -o "StrictHostKeyChecking no" david@${machine} [[ ! -d "$dir3" ]] \&\& exit 1 \; ls -t1 "$dir3"))

#On the ssh command, we exit 1 if the folder doesn't exist. We check the return code with `$?`
if [[ $? != 0 ]] ;then
echo "Folder doesn't exist on $machine";
exit 1
fi

# check number of files retrieved
if [[ "${#dircheck[@]}" = 0 ]] ;then
echo "0 Files on server $machine";
exit 1
fi
#all good for $machine here
done
#all good for all machines here

关于linux - 如何使用 shell 脚本检查 2 台机器中的文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23026879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com