linux打补丁脚本--文件替换方式

#!/bin/sh
if [ $# -ne 2 ] ; then
    echo use ./install_patch patch_dir dst_dir
    exit
fi

patch_dir=$1
INSTALL_DIR=$2

if [ ! -d $patch_dir ] ; then
    echo $patch_dir not found
    exit
fi

if [ ! -d $INSTALL_DIR ] ; then
    echo $INSTALL_DIR not found
    exit
fi

SRC_PATH="$INSTALL_DIR/src.$$"
>$SRC_PATH

DST_TMP="$INSTALL_DIR/tmp.$$"
>$DST_TMP

DST_PATH_TMP="$INSTALL_DIR/dstpathtmp.$$"
>$DST_PATH_TMP

DST_PATH="$INSTALL_DIR/dstpath.$$"
>$DST_PATH

DST_DIR="$INSTALL_DIR/dstdir.$$"
>$DST_DIR

FILE_NAME="$INSTALL_DIR/filename.$$"
>$FILE_NAME

PATCH_DIR=`pwd`
PATCH_DIR=$PATCH_DIR/$patch_dir

# find file name include @ or +
find "$PATCH_DIR" -name "*[\@\+]*" >"$SRC_PATH"
echo "The files under patch:"
echo "------------------------------"
cat "$SRC_PATH"
echo "------------------------------"

# include + file count
first=`grep -c "+" "$SRC_PATH"`
# without + file count
second=`grep -c -v "+" "$SRC_PATH"`
# total file count
COUNT=`expr $first + $second `
echo "There are $COUNT patchfiles !"
echo ""

LINE=1
while [ "$LINE" -le "$COUNT" ]
do
    POINTER_SRC_PATH=`sed -n "$LINE"p "$SRC_PATH"`
    V_SRC_NAME=`basename "$POINTER_SRC_PATH"`
    V_DST_TMP="$INSTALL_DIR/$V_SRC_NAME"
    echo "$V_DST_TMP" >>"$DST_TMP"
    LINE=`expr "$LINE" + 1`
done

# / instead of +
sed 's/\+/\//g' "$DST_TMP" >>"$DST_PATH_TMP"
# . instead of @
sed 's/@/\./g' "$DST_PATH_TMP" >>"$DST_PATH"

LINE=1
while [ "$LINE" -le "$COUNT" ]
do
    V_DST_PATH=`sed -n "$LINE"p "$DST_PATH"`
    dirname "$V_DST_PATH" >>"$DST_DIR"
    basename "$V_DST_PATH" >>"$FILE_NAME"
    LINE=`expr "$LINE" + 1`
done

LINE=1
while [ "$LINE" -le "$COUNT" ]
do
  POINTER_DST_DIR=`sed -n "$LINE"p "$DST_DIR"`
  POINTER_NAME=`sed -n "$LINE"p "$FILE_NAME"`
  POINTER_SRC_PATH=`sed -n "$LINE"p "$SRC_PATH"`

  if [ ! -d $POINTER_DST_DIR ]
  then
   echo "Warnning, $POINTER_DST_DIR does not exit! mkdir it first!"
 mkdir -p $POINTER_DST_DIR
 echo "Replacing $POINTER_DST_DIR/$POINTER_NAME"
 cp -f "$POINTER_SRC_PATH" "$POINTER_DST_DIR/$POINTER_NAME"
  else
   echo "Replacing $POINTER_DST_DIR/$POINTER_NAME"
    cp -f "$POINTER_SRC_PATH" "$POINTER_DST_DIR/$POINTER_NAME"
  fi

  LINE=`expr "$LINE" + 1`
done

rm -f "$SRC_PATH"
rm -f "$DST_TMP"
rm -f "$DST_PATH_TMP"
rm -f "$DST_PATH"
rm -f "$DST_DIR"
rm -f "$FILE_NAME"
echo "Patch has finished!"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值