脚本--二进制程序克隆脚本

该博客介绍了一个可快速将二进制程序及其依赖库文件复制到其他目录的脚本。说明了脚本使用场景、用法,给出脚本名、二进制程序绝对路径、目标目录等参数含义,还提供了测试环境和使用示例,最后提及了源码情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用场景:
    当我们要将一个二进制程序复制到另一系统的根或者其他位置时,我们要做的是将其二进制程序和其依赖的库文件一并复制到目标对应目录下,但是如果某一二进制程序所依赖的库文件较多,那么手动复制就不是一个明智的选择。这个脚本可以帮助你快速的复制一个二进制程序至其他目录。

用法: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值