Unix SSH : Find files with different path on two servers
I have server A & Server B . I want to file find command on both server but with different path .
Currently i created below code to do so :
dir1=( $DATA_DIR/sdfgv $DATA_DIR/1wefgg $DATA_DIR/3fdsevg );
dir2=( $DATA_DIR/asdf $DATA_DIR/sdfewfT $DATA_DIR/efergvfw );
timestamp=$(date +%Y%m%d%H%M%S);
report_name=Audit_Report_${timestamp}.txt
uname=xyz
server=( serv1 serv2);
for j in ${server[@]};
do {
if [ "$j" == "serv1 " ]
then
for i in ${dir1[@]};
do {
Size=`ssh -q $uname@$j "find $i -type f -mtime +1 -name '*.gz' -printf '%s + ' | dc -e0 -f- -ep"`;
echo " $j $i $Size "
}
done
else
for i in ${dir2[@]};
do {
Size=`ssh -q $uname@$j "find $i -type f -mtime +1 -name '*.gz' -exec du -k {} ; | awk '{ total += $ 1} END{print total/1024;}'"`;
echo " $j $i $Size "
}
done
fi
}
done
This code works pretty good but i want something that can be generic without if else on server name .
I dont want to use if else for server name .
Both server have different directory path to search for
Please come up with some suggestions .
Thank You !
Bash functions would help. Perhaps one for determining which directory to use for which server, and then one for executing the find command. Something like this (pseudo-code, untested):
getDirs() {
if [ $1 == 'server1' ]; then
echo $dir1
fi
if [ $1 == 'server2' ]; then
echo $dir2
fi
}
findFiles() {
local srv=$1
shift
local dir=$1
shift
local findArgs=$@
echo $(ssh -q $uname@$srv "find $dir $findArgs")
}
for srv in ${server[@]};
do
dirs=$(getDirs $srv)
for d in $dirs
do
findFiles $srv $d -type f -mtime +1 -name '*.gz'
done
done
链接地址: http://www.djcxy.com/p/78072.html