应用场景:
当我们要将一个二进制程序复制到另一系统的根或者其他位置时,我们要做的是将其二进制程序和其依赖的库文件一并复制到目标对应目录下,但是如果某一二进制程序所依赖的库文件较多,那么手动复制就不是一个明智的选择。这个脚本可以帮助你快速的复制一个二进制程序至其他目录。
用法:Script_Name Binary_Path Root_Path
说明:
aibto:该脚本的脚本名,你也可以自定义名字;
原意:aibto = auto install binary-program to other-position
Binary_Path:二进制程序绝对路径
Root_Path:目标(根)目录
测试环境:CentOS6 Kernel: 3.10.10
例子:aibto /bin/bash /mnt/sysroot
在这个例子中,aibto脚本会在/mnt/sysroot目录下创建bin,lib64等目录,并将/bin/bash程序本身以及所依赖的库文件一并复制到对应目录下。
下面是源码:
注:为了优快云页面上排版美观,在复制源文件代码至此后我对某些行重新缩进过。因此直接复制源码可能会有排版凌乱问题
#!/bin/bash
#
# Descrption:aibto = auto install binary-program to other-disk
# Usage: aibto Binary_Program_Path Root_Path
# This tool can help you copy binary program from your pc to other position.
# You don't need to create every directory such as bin,sbin,lib and so on.
# But you must create a father directory before run this script.
# This script can auto create some directory in father directory when it need.
#
# functions
instbin(){
bin_dir=$1
bin_d=${bin_dir%/*}
bin_f=${bin_dir##*/}
if [ -d $2${bin_d} ]; then
if [ -f $2$1 ]; then
echo "=Skip: $1"
echo "Binary program: $1 == existed" >>${LOG_FILE}
else
cp $1 $2$1
echo "+Copy: $1"
echo "Binary program: $1 -> $2$1" >>${LOG_FILE}
fi
else
mkdir -pv $2${bin_d}
cp $1 $2$1
echo "+Copy: $1"
echo "Binary program: $1 -> $2$1" >>${LOG_FILE}
fi
}
#
instso(){
so_dir=$1
so_dir=${so_dir#*/}
so_dir="/${so_dir% *}"
so_d=${so_dir%/*}
so_f=${so_dir##*/}
if [ -d $2${so_d} ]; then
if [ -f $2${so_dir} ]; then
echo "=Skip: ${so_dir}"
echo "so: ${so_dir} == existed" >>${LOG_FILE}
else
cp ${so_dir} $2${so_d}
echo "+Copy: ${so_dir}"
echo "so: ${so_dir} -> $2${so_dir}" >>${LOG_FILE}
fi
else
mkdir -pv $2${so_d}
cp ${so_dir} $2${so_d}
echo "+Copy: ${so_dir}"
echo "so: ${so_dir} -> $2${so_dir}" >>${LOG_FILE}
fi
}
#
# Checking
[ $# -ne 2 ] && echo "Error! Usage: aibto PATH ROOT" && exit 1
[ ! -f $1 ] && echo "$1 not found" && exit 2
[ ! -d $2 ] && echo "$2 must be a exist directory" && exit 3
#
#
# main
[ -d /tmp ] || mkdir -pv /tmp
LOG_FILE=/tmp/aibto.log
TEMP_FILE=/tmp/aibto.XXX
#
# Copy binary program
echo "-----------------------------" >>${LOG_FILE}
echo " $(date +"%Y-%m-%d %H:%M:%S")" >>${LOG_FILE}
echo $1 | grep "/$" &>/dev/null && echo "$1 is a directory" && exit 2
dest_dir=$2
echo $2 | grep "/$" &>/dev/null && dest_dir=${dest_dir%/*}
instbin "$1" "${dest_dir}"
#
# Copy so
file=$(mktemp ${TEMP_FILE})
ldd $1 >$file
sed -i '1d' $file
while read line; do
instso "$line" "${dest_dir}"
done <$file
echo "------Finished------"
echo "Binary program: $1"
echo "Total: $(wc -l $file | cut -d' ' -f1) so files"
echo "Log file: ${LOG_FILE}"
rm -f $file