Created
November 4, 2023 08:07
-
-
Save wangzhankun/057faa71f3266f9b3ac61b67d8c421ff to your computer and use it in GitHub Desktop.
copy-with-so.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 用法:./copy-with-so.sh 源文件 目标目录 sysroot | |
# 例如:./copy-with-so.sh /bin/ls /tmp | |
my_help(){ | |
echo "用法:./copy-with-so.sh 源文件 目标目录 sysroot" | |
} | |
if [ "$#" -ne 3 ]; then | |
my_help | |
return 1 | |
fi | |
# 如果源文件不是可执行文件 | |
if [ ! -x $1 ]; then | |
echo "$1 不是可执行文件" | |
return 1 | |
fi | |
# 如果目标目录不存在 | |
if [ ! -d $2 ]; then | |
echo "$2 不存在" | |
return 1 | |
fi | |
# 源文件路径和目标路径 | |
source_file=$1 | |
target_dir=$2 | |
sysroot_dir=$3 | |
echo "拷贝 $source_file 到 $target_dir" | |
cp $source_file $target_dir | |
# 通过 ldd 命令获取源文件依赖的动态库列表,并提取文件路径 | |
libraries=$(ldd $source_file | awk '{if ($3 == "=>") print $1; else if ($2 == "=>") print $3;}') | |
# 遍历每个库文件,进行拷贝操作 | |
for library in $libraries; do | |
# 判断文件是否存在 | |
if [ -f $library ]; then | |
# 创建目标目录(保留目录结构) | |
dst_path=${sysroot_dir}${library%/*} | |
mkdir -p "${dst_path}" | |
# 拷贝动态库到目标目录 | |
cp $library "${dst_path}" | |
echo "拷贝 $library 到 ${dst_path}" | |
else | |
echo "$library 不存在" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment