Linux 自解压程序
简单的自解压程序
参考cmake-3.19.6-Linux-x86_64 写了一个自解压的可执行程序
生成可执行程序
cat ./autopack_run.sh test.tar.gz > test_auto_run.sh
执行自解压程序
方法1: sh 直接执行
ssh test_auto_run.sh
方法2:增加可执行权限
chmod +x test_auto_run.sh
./test_auto_run.sh
脚本 autopack_run.sh
#!/bin/sh
MY_APPNAME="autopack_run"
MY_VERSION="1.0.0"
MY_COPYRIGHT="Copyright (c) Allen"
UNPACK_SUBDIR="DATA_DIR"
TAR_GZ_START_LINE=$(awk '/^__STARTS_TAR_GZ_FILE_HERE__/ {print NR + 1; exit 0; }' "$0")
#interactive=TRUE
interactive=FALSE
# Display usage
autopack_runusage()
{
cat <<EOF
Usage: $0 [options]
Options: [defaults in brackets after descriptions]
--help print this message
--version print autopack_run version
--prefix=dir directory in which to install
--include-subdir include the ${UNPACK_SUBDIR} subdirectory
--exclude-subdir exclude the ${UNPACK_SUBDIR} subdirectory
EOF
exit 1
}
autopack_runecho_exit()
{
echo $1
exit 1
}
# Display version
autopack_runversion()
{
echo "$MY_APPNAME Version: $MY_VERSION, $MY_COPYRIGHT"
}
# Helper function to fix windows paths.
autopack_runfix_slashes ()
{
echo "$1" | sed 's/\\/\//g'
}
autopack_runinclude_subdir=""
for a in "$@"; do
if echo $a | grep "^--prefix=" > /dev/null 2> /dev/null; then
autopack_runprefix_dir=`echo $a | sed "s/^--prefix=//"`
autopack_runprefix_dir=`autopack_runfix_slashes "${autopack_runprefix_dir}"`
fi
if echo $a | grep "^--help" > /dev/null 2> /dev/null; then
autopack_runusage
fi
if echo $a | grep "^--version" > /dev/null 2> /dev/null; then
autopack_runversion
exit 2
fi
if echo $a | grep "^--include-subdir" > /dev/null 2> /dev/null; then
autopack_runinclude_subdir=TRUE
fi
if echo $a | grep "^--exclude-subdir" > /dev/null 2> /dev/null; then
autopack_runinclude_subdir=FALSE
fi
done
if [ "x${autopack_runinclude_subdir}x" != "xx" ]
then
interactive=FALSE
fi
autopack_runversion
echo "This is a self-extracting archive."
toplevel="`pwd`"
if [ "x${autopack_runprefix_dir}x" != "xx" ]
then
toplevel="${autopack_runprefix_dir}"
fi
echo "The archive will be extracted to: ${toplevel}"
if [ "x${interactive}x" = "xTRUEx" ]
then
echo ""
echo "If you want to stop extracting, please press <ctrl-C>."
if [ "x${autopack_runinclude_subdir}x" = "xx" ]
then
echo "Do you want to include the subdirectory ${UNPACK_SUBDIR}?"
echo "Saying yes will install in: \"${toplevel}/${UNPACK_SUBDIR}\" "
echo "Saying no will install in: \"${toplevel}\" [Yn]: "
read line leftover
autopack_runinclude_subdir=TRUE
case ${line} in
n* | N*)
autopack_runinclude_subdir=FALSE
esac
fi
fi
if [ "x${autopack_runinclude_subdir}x" = "xTRUEx" ]
then
toplevel="${toplevel}/${UNPACK_SUBDIR}"
mkdir -p "${toplevel}"
fi
echo
echo "Using target directory: ${toplevel}"
echo "Extracting, please wait..."
echo ""
# so at first try to tail some file to see if tail fails if used with "-n"
# if so, don't use "-n"
use_new_tail_syntax="-n"
tail $use_new_tail_syntax +1 "$0" > /dev/null 2> /dev/null || use_new_tail_syntax=""
extractor="tar xvzf -"
tail $use_new_tail_syntax +${TAR_GZ_START_LINE} "$0" | (cd "${toplevel}" && ${extractor}) || autopack_runecho_exit "Problem unpacking the ${UNPACK_SUBDIR}"
echo "Unpacking finished successfully. File List:"
ls -l ${UNPACK_SUBDIR}
exit 0
#-----------------------------------------------------------
# Start of TAR.GZ file
#-----------------------------------------------------------;
__STARTS_TAR_GZ_FILE_HERE__