简单的自解压程序

18 篇文章 0 订阅

Linux 自解压程序
简单的自解压程序
生成可执行程序
执行自解压程序
脚本 autopack_run.sh
简单的自解压程序
参考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__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值