首页 技术 正文
技术 2022年11月19日
0 收藏 424 点赞 4,587 浏览 2518 个字

有时候给别人分享一个工具的时候,同时需要提供的文件比较多;

如果分享一个压缩包还得教会对方如何解压、执行哪个脚本,感觉需要传输的内容多了就不方便;

把几个Shell脚本和文件打包成一个“单独的可执行文件”,对方接收到这个文件,只需要执行一下这个文件,就可以实现解压、执行对应脚本了,相对比较方便;

#!/bin/bash -
#===============================================================================
#
# FILE: shell_pack.sh
#
# USAGE: ./shell_pack.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: lwq (), scue@vip.qq.com
# ORGANIZATION:
# CREATED: // :: PM CST
# REVISION: ---
#===============================================================================#=== FUNCTION ================================================================
# NAME: usage
# DESCRIPTION: Display usage information.
#===============================================================================
function usage ()
{
cat <<- EOT Usage : $ -p package -s script file1 file2 file3 .. Options:
-h|help Display this message
-p|package The output package name
-s|script The script will run when unpack package
Other The all files what you want to packEOT
} # ---------- end of function usage ----------#-----------------------------------------------------------------------
# Handle command line arguments
#-----------------------------------------------------------------------while getopts ":hp:s:" opt
do
case $opt in h|help ) usage; exit ;;
p|package ) package_name=$OPTARG ;;
s|script ) install_script=$OPTARG ;;
\? ) echo -e "\n Option does not exist : $OPTARG\n"
usage; exit ;; esac # --- end of case ---
done
shift $(($OPTIND-))if [[ -z $package_name ]]; then
echo "package_name can't not be empty"
usage
exit
fiif [[ -z $package_name ]]; then
echo "install_script can't not be empty"
usage
exit
fifiles=$@generate_wrapper_script(){
local install_script=$
local wrapper_script=$
cat <<-'EOT' >$wrapper_script
#!/bin/sh
echo "begin ..."
unpackdir=/tmp/$(basename $)_unpack
rm -rf $unpackdir >/dev/null
mkdir -p $unpackdir
echo "unpacking ..."
sed '1, /^#__SCRIPTEND__/d' $ | tar zxf - -C $unpackdir
if [ $? -ne ]; then
echo "unpack package failed."
exit
fi
echo ""
echo "installing ..."
cd $unpackdir
EOT
cat <<-EOR >>$wrapper_script
chmod +x $install_script
./$install_script
EOR
cat <<-'EOE' >>$wrapper_script
if [ $? -ne ]; then
echo "install failed."
exit
elif [[ -d $unpackdir ]]; then
rm -rf $unpackdir
fi
echo "install ok, enjoy!"
exit
#__SCRIPTEND__
EOE
}tarfile=package_content_$$.tgz
wrapfile=wrap_$$.shecho -e "start packing ..\n"
tar zcvf $tarfile $files $install_script
generate_wrapper_script $install_script $wrapfile
cat $wrapfile $tarfile > $package_name
chmod +x $package_nameecho -e "\noutput: $package_name\n"rm -f $tarfile
rm -f $wrapfile

文件内容保存到 shell_pack.sh,使用方法举例:

  ./shell_pack.sh -p logcat_install -s logcat_install.sh logcat_all.sh logcat_wrapper.sh vmstat2

将产生可执行文件“logcat_install”,执行logcat_install时,会解压自身文件内的tar.gz文件,并执行关键的脚本 logcat_install.sh

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852