免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7381 | 回复: 5
打印 上一主题 下一主题

[学习共享] 如何将多个shell脚本和相关文件合并成一个可执行程序? [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2016-05-23 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-05-20 10:53 |只看该作者 |倒序浏览
本帖最后由 boy010 于 2016-05-20 10:54 编辑

最近在琢磨这么个东西,想将多个shell脚本和相关的文件打包成一个可执行程序,在网上查找了半天,终于发现了一位大神做的一个shell脚本,shell_pack.sh,经试验该脚本可以实现没有参数传入情况下的运行(权限所限不能贴链接,就不贴原贴地址了^^,而且因为脚本有100多行,所以我就贴在附件中了,不直接贴在这里了)。但我现在需要在主脚本执行时,还要传入一个参数,但该脚本没办法实现,不知道该如何修改下,请大神指导下,谢谢!当然如果大神有别的方法可以实现,也请不吝赐教!万分感谢!
shell_pack.zip (1.07 KB, 下载次数: 88)

举个简单的例子:
当前目录下有三个文件,print.sh在执行时会用到list.sh和1.txt
  1. [root@localhost test]# ls
  2. 1.txt  list.sh  print.sh  shell_pack.sh
  3. [root@localhost test]# cat print.sh
  4. #!/bin/sh
  5. echo $1  #我需要传入参数的地方
  6. echo "#############################"
  7. cat 1.txt
  8. echo "##################################"
  9. echo "list all file in /home"
  10. ./list.sh
  11. [root@localhost test]# cat list.sh
  12. #!/bin/sh
  13. ls -l /home/
  14. [root@localhost test]# cat 1.txt
  15. this is 1st line
  16. this is 2nd line
  17. [root@localhost test]#
复制代码
shell_pack.sh是用来打包他们的,也就是我贴的附近中的脚本,运行情况如下:
  1. [root@localhost test]# ./print.sh helloworld  #不打包直接运行主脚本,传入参数helloworld
  2. helloworld   # 这里可以打印出我希望传入的参数。
  3. #############################
  4. this is 1st line
  5. this is 2nd line
  6. ##################################
  7. list all file in /home
  8. total 10
  9. drwx------.  2 root     root     16384 Apr 28  2015 lost+found
  10. drwxr-xr-x.  4 root     root      4096 Apr 11 17:01 study
  11. [root@localhost test]# ./shell_pack.sh -p packed_all.sh -s print.sh list.sh 1.txt
  12. start packing ..

  13. list.sh
  14. 1.txt
  15. print.sh

  16. output: packed_all.sh

  17. [root@localhost test]# ./packed_all.sh helloworld  # 希望能实现的目的,打包好的可执行程序也可以接受参数传入。
  18. begin ...
  19. unpacking ...

  20. installing ...
  21. print.sh          # 这里打印出了print.sh, 并不是我所想要的helloworld
  22. #############################
  23. this is 1st line
  24. this is 2nd line
  25. ##################################
  26. list all file in /home
  27. total 10
  28. drwx------.  2 root     root     16384 Apr 28  2015 lost+found
  29. drwxr-xr-x.  4 root     root      4096 Apr 11 17:01 study
  30. install ok, enjoy!
  31. [root@localhost test]#
复制代码




论坛徽章:
1
程序设计版块每日发帖之星
日期:2016-05-23 06:20:00
2 [报告]
发表于 2016-05-20 10:56 |只看该作者
shell_pack.sh的脚本也在这里给贴一下,免得还要麻烦各位去下载:
  1. #!/bin/bash -
  2. #========================================
  3. #
  4. #  FILE: shell_pack.sh
  5. #
  6. # USAGE: ./shell_pack.sh
  7. #
  8. #   DESCRIPTION:
  9. #
  10. #   OPTIONS: ---
  11. #  REQUIREMENTS: ---
  12. #  BUGS: ---
  13. # NOTES: ---
  14. #AUTHOR: lwq (28120), scue@vip.qq.com
  15. #  ORGANIZATION:
  16. #   CREATED: 04/22/2015 02:38:01 PM CST
  17. #  REVISION:  ---
  18. #========================================
  19. #===  FUNCTION  =========================
  20. # NAME:  usage
  21. #  DESCRIPTION:  Display usage information.
  22. #========================================
  23. function usage ()
  24. {
  25. cat <<- EOT
  26. Usage :  $0 -p package -s script file1 file2 file3 ..
  27. Options:
  28. -h|help   Display this message
  29. -p|packageThe output package name
  30. -s|script The script will run when unpack package
  31. Other The all files what you want to pack
  32. EOT
  33. } # ----------  end of function usage  ----------
  34. #--------------------------------------------
  35. #  Handle command line arguments
  36. #--------------------------------------------
  37. while getopts ":hp:s:" opt
  38. do
  39. case $opt in
  40. h|help) usage; exit 0   ;;
  41. p|package ) package_name=$OPTARG ;;
  42. s|script  ) install_script=$OPTARG ;;
  43. \?) echo -e "\n  Option does not exist : $OPTARG\n"
  44. usage; exit 1   ;;
  45. esac # --- end of case ---
  46. done
  47. shift $(($OPTIND-1))
  48. if [[ -z $package_name ]]; then
  49. echo "package_name can't not be empty"
  50. usage
  51. exit
  52. fi
  53. if [[ -z $package_name ]]; then
  54. echo "install_script can't not be empty"
  55. usage
  56. exit
  57. fi
  58. files=$@
  59. generate_wrapper_script(){
  60. local install_script=$1
  61. local wrapper_script=$2
  62. cat <<-'EOT' >$wrapper_script
  63. #!/bin/sh
  64. echo "begin ..."
  65. unpackdir=/tmp/$(basename $0)_unpack
  66. rm -rf $unpackdir 2>/dev/null
  67. mkdir -p $unpackdir
  68. echo "unpacking ..."
  69. sed '1, /^#__SCRIPTEND__/d' $0 | tar zxf - -C $unpackdir
  70. if [ $? -ne 0 ]; then
  71. echo "unpack package failed."
  72. exit 1
  73. fi
  74. echo ""
  75. echo "installing ..."
  76. cd $unpackdir
  77. EOT
  78. cat <<-EOR >>$wrapper_script
  79. chmod +x $install_script
  80. ./$install_script
  81. EOR
  82. cat <<-'EOE' >>$wrapper_script
  83. if [ $? -ne 0 ]; then
  84. echo "install failed."
  85. exit 2
  86. elif [[ -d $unpackdir ]]; then
  87. rm -rf $unpackdir
  88. fi
  89. echo "install ok, enjoy!"
  90. exit 0
  91. #__SCRIPTEND__
  92. EOE
  93. }
  94. tarfile=package_content_$.tgz
  95. wrapfile=wrap_$.sh
  96. echo -e "start packing ..\n"
  97. tar zcvf $tarfile $files $install_script
  98. generate_wrapper_script $install_script $wrapfile
  99. cat $wrapfile $tarfile > $package_name
  100. chmod +x $package_name
  101. echo -e "\noutput: $package_name\n"
  102. rm -f $tarfile
  103. rm -f $wrapfile
复制代码

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
3 [报告]
发表于 2016-05-20 13:19 |只看该作者
回复 2# boy010

to use argument in your package script in shell_pack.sh line 80

cat <<-EOR >>$wrapper_script
chmod +x $install_script
./$install_script "\$@"
EOR
   

remark it directly if did't want to see the running information, like as below
begin ...          ==> line 64 in shell_pack.sh
unpacking ...   ==> line 68 in shell_pack.sh

installing ...     ==> line 74,75
...

评分

参与人数 2信誉积分 +18 收起 理由
boy010 + 8 Really Appreciate !!!
Herowinter + 10 很给力!

查看全部评分

论坛徽章:
1
程序设计版块每日发帖之星
日期:2016-05-23 06:20:00
4 [报告]
发表于 2016-05-20 14:20 |只看该作者
本帖最后由 boy010 于 2016-05-20 14:33 编辑

回复 3# jason680

非常感谢你的回复,结果正是我想要实现的目的。再次献上对大神的膜拜!Thank you Sir !!!
我刚又尝试下如果将print.sh脚本里添加个生成文件夹和生成一个新文件的命令,用shell_pack.sh先打包,然后再运行结束后发现没有看到生成的目录呢?问题出在什么地方了呢?
  1. [root@localhost test]# cat print.sh
  2. #!/bin/sh

  3. echo $1  #我需要传入参数的地方

  4. echo "#############################"
  5. cat 1.txt
  6. echo "##################################"
  7. echo "list all file in /home"
  8. ./list.sh
  9. mkdir file_backup   # 这里尝试生成个目录
  10. echo "hello China" > file_backup/text.txt  # 尝试在这个目录里生成一个文件
复制代码


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
5 [报告]
发表于 2016-05-20 14:34 |只看该作者
回复 4# boy010

一言难尽...打包后只能全路径

dir=$HOME      # or ~, /tmp, ...
mkdir $dir/file_backup   # 这里尝试生成个目录
echo "hello China" > $dir/file_backup/text.txt  # 尝试在这个目录里生成一个文件
   

论坛徽章:
1
程序设计版块每日发帖之星
日期:2016-05-23 06:20:00
6 [报告]
发表于 2016-05-20 14:47 |只看该作者
回复 5# jason680

哦,原来这样的,我再试试去。。。谢谢!!!

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP