作为程序员, 能做开源项目肯定是一种荣耀.
但如果自己的东西实在不好意思开源, 或者存有一点点私心的话, 将自己的脚本封装成二进制, 也是对远吗的一种保护.
下面, 介绍一下编译shell脚本的方法.
lius@suse12:tmp> cat lius.sh
#!/bin/bash
echo OK!
编译生成二进制:
所用到的命令: shc(没有命令,需要yum或者rpm安装)
lius@suse12:bin> shc --help
shc: invalid option -- '-'
shc parse: Unknown option
shc Version 3.9.6, Generic Shell Script Compiler
shc GNU GPL Version 3 Md Jahidul Hamid <jahidulhamid@yahoo.com>
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-o outfile] [-rvDUCABh] -f script
-e %s Expiration date in dd/mm/yyyy format [none]
-m %s Message to display upon expiration ["Please contact your provider"]
-f %s File name of the script to compile
-i %s Inline option for the shell interpreter i.e: -e
-x %s eXec command, as a printf format i.e: exec('%s',@ARGV);
-l %s Last shell option i.e: --
-o %s output filename
-r Relax security. Make a redistributable binary
-v Verbose compilation
-D Switch ON debug exec calls [OFF]
-U Make binary untraceable [no]
-C Display license and exit
-A Display abstract and exit
-B Compile for busybox
-h Display help and exit
Environment variables used:
Name Default Usage
CC cc C compiler command
CFLAGS <none> C compiler flags
Please consult the shc man page.
常用选项:
-v: 详细模式, 打印信息详细, 便于查看进程;
-r: 支持多系统执行的一种二进制编译格式;
-f: 跟要编译的shell脚本名称;
编译过程:
lius@suse12:tmp> shc -f lius.sh -v -r // -f 后面跟shell脚本的名称, 可以放到句子中间
shc shll=bash // 后面是执行过程
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc lius.sh.x.c -o lius.sh.x
shc: strip lius.sh.x
shc: chmod ug=rwx,o=rx lius.sh.x
lius@suse12:tmp> ls lius* // 可以看到生成的两个文件
lius.sh lius.sh.x lius.sh.x.c
lius@suse12:tmp> file lius* // 查看他们的性质
lius.sh: Bourne-Again shell script, ASCII text executable // 源码
lius.sh.x: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.0.0, BuildID[sha1]=f2231a9fb3168396b211f41a9d88a577e1cc600c, stripped // 二进制
lius.sh.x.c: C source, ASCII text // C 源码
我们可以删除之前的源码, 将lius.sh.x命名成lius.sh.
注意, 这样的脚本, 必须要有Sha-Bang, 不然的话执行时候会出错.
执行结果:
lius@suse12:tmp> ./lius.sh.x
OK!
转载请注明:liutianfeng.com » shell脚本编译成二进制的方法
发表回复
要发表评论,您必须先登录。