Linux 如何使用shell脚本检查两台计算机中的文件夹?
我正在编写一个shell脚本,需要在Linux 如何使用shell脚本检查两台计算机中的文件夹?,linux,bash,shell,ubuntu,ssh,Linux,Bash,Shell,Ubuntu,Ssh,我正在编写一个shell脚本,需要在machineX上运行。它将在其他两台机器中检查此文件夹中YYYYMMDD格式的特定文件夹MAPPED\u位置,machineP和machineQ。因此,路径在machineP和machineQ中都是这样的 /bat/testdata/t1_snapshot/20140311 在上面的文件夹路径中,会有一些文件。下面是我的shell脚本- #!/bin/bash readonly MACHINES=(machineP machineQ) readonly
machineX
上运行。它将在其他两台机器中检查此文件夹中YYYYMMDD
格式的特定文件夹MAPPED\u位置
,machineP
和machineQ
。因此,路径在machineP
和machineQ
中都是这样的
/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\u 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
中添加任何文件夹。不确定是什么问题
另一个更新-
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
。然后我在调试模式下运行脚本-
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
一种方法是使用find和wc-l的管道来计算目录中的文件并进行比较。或者您想使用rsync--dry run查看两个目录之间的差异,并据此采取行动。@user2809564检查文件是否存在的代码正在工作。(如果我在我的服务器上尝试,但该文件夹不存在,它将退出并出现正确错误)。你确定machineP在机器里吗?在调试中运行脚本。替换
#/bin/bash
by#/bin/bash-x
并将输出添加到post@naab:我刚刚在调试模式下为您更新了问题的详细信息。如果一台服务器上不存在该文件夹,请尝试在一台服务器上创建一个空文件夹,然后运行它。如果文件夹也不存在,我可以看到错误。但是在一台机器上创建了一个文件夹之后,我正在运行它。我总是看到上面的问题。没有引用,这就是我在$FILERS\u位置为机器使用的;执行
。看看你的答案。我只是用了你给我的一切,谢谢。如果文件夹不存在,则dir3check
行给出一个错误,表示没有这样的文件或目录?@AKIWEB如果文件夹不存在,dir3check
将是一个空数组,并在数组大小的else语句中执行check@naab:谢谢。有没有办法不用for循环就可以做到这一点?基本上,我可以使用机器数组索引0和1来进行检查,并获得发生的特定错误?我已经用我的例子更新了这个问题。@user2809564是的,你可以,但它很难看。如果有一天你再添加一个服务器呢?您必须更改10-20行代码,但无法确定它是否能按预期工作,而我需要更改1,并且我确信它能工作。@naab:当然,naab是有意义的,但在我的示例中,它始终是两台服务器,所以我试图使它变得简单。不管怎样,我刚刚尝试了你的例子,我在意外标记附近遇到了这个错误语法错误done'`知道怎么回事吗?