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

我正在研究一个我需要在machineX上运行的shell脚本。 它将在其他两台机器( machineQmachineQ此文件夹MAPPED_LOCATION中检查某个文件夹的格式为YYYYMMDD 。 所以在machineQmachineQpath都是这样的 –

 /bat/testdata/t1_snapshot/20140311 

在上面的文件夹path里面会有一些文件。 下面是我的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 

Noow我应该做的是 – 如果这个path存在/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" 

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

 echo "Everything is Correct" 

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

另一个更新 –

我只在20140411中创build了一个空文件夹20140411 。 然后我在debugging模式下运行脚本 –

 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 

Solutions Collecting From Web of "如何使用shell脚本检查两台机器中的文件夹?"

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

我还在执行ls之前添加了对目录existance [[ -d "$dir3" ]]的检查,并且在当前的bash脚本中转义&&以不被解释。

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

要定义一个bash数组,请在命令后面添加extra ( ) ,然后比较数组的大小。

 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