分类目录归档:shell

高效的简洁的inotify+rsync同步代码

#!/bin/bash
inotifywait -mrq /mnt/www/xxx --format "%w%f %e" -e modify,attrib,close_write,moved_to,moved_from,move,create,delete,delete_self,unmount | while read line
do
  # echo $line
  e=`echo ${line##* }`
  f=`echo ${line% *}`
  if [[ "$f" =~ "/mnt/www/xxx/patchs/" ]] || [[ "$f" =~ "/mnt/www/xxx/release/releases/" ]]; then
    # 排除两个不需要同步的目录
    continue
  fi
  if [[ ! -e "$f" ]]; then
    continue
  fi
  if [[ "$e" =~ "MOVED_TO" ]] || [[ "$e" =~ "CLOSE_WRITE" ]] || [[ "$e" =~ "ATTRIB" ]] || [[ "$e" =~ "CREATE,ISDIR" ]]; then
    rsync -a --delete-after $f f22@host1:$f
    rsync -a --delete-after $f f22@host2:$f
  elif [[ "$e" =~ "DELETE" ]] || [[ "$e" =~ "MOVED_FROM" ]]; then
    rsync --delete -a $f f22@host1:$f
    rsync --delete -a $f f22@host2:$f
  fi
done

bash 查找数组中是否存在某个元素

realfilesarr=(123 456 789)

function contains() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}

if [ $(contains "${realfilesarr[@]}" "123") == "n" ]; then
    echo "不存在";
fi

if [ $contains "${realfilesarr[@]}" "123") == "y" ]; then
    echo "存在";
fi