Bash Self-Extracting Script
来源:互联网 发布:手机验钞机软件2016 编辑:程序博客网 时间:2024/06/06 02:09
http://www.linuxjournal.com/node/1005818
In this post I'll show you how to create a self extracting bash script to automate the installation of files on your system. This script requires coreutils (for cat, tail), awk, gzip, tar and bash. There are 3 parts to the Base Self-Extracting Script
- The Payload
- The Decompression Script
- The Builder Script
jeff:~$ mkdir -vp installer/payloadmkdir: created directory `installer'mkdir: created directory `installer/payload'jeff:~$ cd installer/jeff:~/installer$The Payload The payload directory will contain....just that, the payload of your installer. This is the location where you'll put all your tar files, scripts, binaries, etc that you'll want installed onto the new system. For this example I have a tar file containing some text files that I'll want to install into a folder that I create into my home directory. Here is the listing of the tar file.
jeff:~$ tar tvf files.tar-rw-r--r-- jeff/jeff 40 2007-12-06 07:53 ./File1.txt-rw-r--r-- jeff/jeff 92 2007-12-06 07:55 ./File2.txtjeff:~$Now we must create the installation script that will handle the payload. This script contains any actions that you'd wish to be performed on the installation system, make directories, uncompress files, run system commands, etc. For the example I will create a directory and untar the files into it.
#!/bin/bashecho "Running Installer"mkdir $HOME/filestar ./files.tar -C $HOME/filesNow that we have filled our payload directory with all the files we'd like to install and created the installation script to the the files in their correct location, our directory structure should look like this:
jeff:~$ find installer/installer/installer/payloadinstaller/payload/installerinstaller/payload/files.tarjeff:~$The Decompression Script The Decompression Script does most of the work. The compressed archive of your payload directory will actually be appended onto this script. When ran, the script will remove the archive, decompress it and execute the install script we had created in the previous section.
#!/bin/bashecho ""echo "Self Extracting Installer"echo ""export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0`tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIRCDIR=`pwd`cd $TMPDIR./installercd $CDIRrm -rf $TMPDIRexit 0__ARCHIVE_BELOW__Ok, now lets go through this script step by step. After the bit of printout at the begining, the first real line of work creates a temporary directory for us to decompress our payload into initially before we install it.
export TMPDIR=`mktemp -d /tmp/selfextrac.XXXXXX`The
-dflag tells mktemp to create a directory rather than a file. The parameter at the end,
/tmp/selfextract.XXXXXXis a template of the name of the directory we are going to create. The X's are replaced with random characters to generate a random name, just incase we happen to be installing two things at the same time. The next line in the script,
ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0`find the line number where the archive starts in the script. The first parameter given to awk
/^__ARCHIVE_BELOW__/tells it to search for a regular expression "A line starting with the characters '__ARCHIVE_BELOW__'". Each line is read and until the regular expression is satisfied. The next parameter
{print NR + 1; exit 0; }tells awk to print out the number of records (number of lines read) plus 1, then quit. The third parameter
$0is the first argument when running this script, which happens to be the script's name ($0 = ./decompress). We will now seperate the archive from the script and decompress it into the temporary directory we have created.
tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIRtail prints out the end of a file. The parameter
-n+$ARCHIVEtells tail to start at line number we just read in the previous command, and print til the end of the file.
$0again is the name of this script file. This output is then piped to tar where it is
zungzipped, and
xunarchived into
-C $TMPDIRthe temporary directory. The next section, we remember our current directory,
CDIR=`pwd`and step into the temporary directory
cd $TMPDIR. From here we run the script we created in the Payload section. After the script has been executed, we revert back to our previous directory
cd $CDIRand remove the temporary directory
rm -rf $TMPDIRThe last two lines in this script are very important. First the line
exit 0causes the script to stop executing. If we forget this line BASH would try to execute the binary archive attached at the bottom and would cause problems. The very last line
__ARCHIVE_BELOW__tells awk that the binary archive starts on the next line. Make sure that this is the last line, no extra empty lines below this one. Now that we have finished creating the Decompression script our directory should looke like this:
jeff:~$ find installer/installer/installer/buildinstaller/payloadinstaller/payload/installerinstaller/payload/files.tarinstaller/decompressjeff:~$The Builder Script The last section of this installer is the script that builds the self-extracting script. This script compresses the payload and then adds the decompresion script to the archive.
#!/bin/bashcd payloadtar cf ../payload.tar ./*cd ..if [ -e "payload.tar" ]; then gzip payload.tar if [ -e "payload.tar.gz" ]; then cat decompress payload.tar.gz > selfextract.bsx else echo "payload.tar.gz does not exist" exit 1 fielse echo "payload.tar does not exist" exit 1fiecho "selfextract.bsx created"exit 0This script archives the payload directory
tar cf ../payload.tar ./*and compresses it using gzip
gzip payload.tar. Next the script concatenates the decompress script with the compressed payload
cat decompress payload.tar.gz > selfextract.bsx. With all the scripts completed our directory should look like this:
jeff:~$ find installer/installer/installer/buildinstaller/payloadinstaller/payload/installerinstaller/payload/files.tarinstaller/decompressjeff:~$Test it out Now that we have created everything, we can run the scripts and test it all out. Firstly run the build script. Your output should look as follows:
jeff:~/installer$ ./buildselfextract.bsx createdjeff:~/installer$ lsbuild decompress payload payload.tar.gz selfextract.bsxjeff:~/installer$Now we have our bash script with the archive attached (selfextract.bsx). Run this script and you should see the following output:
jeff:~/installer$ ./selfextract.bsxSelf Extracting Installer./files.tar./installerRunning Installer./File1.txt./File2.txtjeff:~/installer$ find /home/jeff/files/home/jeff/files/home/jeff/files/File1.txt/home/jeff/files/File2.txtjeff:~/installer$If you would like the installer to install something else, just place the files in the payload directory and modify the installer script to perform the proper action.
______________________
Software Engineer in Marshalltown, IA BB PIN: 21C3344D www.biffengineering.com
http://hi.baidu.com/vzomik/blog/item/8ee7fa85128d513367096ebe.html
有趣的dotRUN自解压脚本
2010年06月16日 星期三 20:07
不知道有没有人发现,用 Windows 下载 Linux 的 nVidia 显卡驱动时,会打开一个网页,上半部分都是纯文本,
而下半部分是乱码??呵呵,其实呀,这是一个 Shell 脚本而已!不信的话可以用 GVim 打开那个 .run 文件。
现在我们来学习做一个 .run 文件,当然,没有那个显卡驱动那么夸张,只是一个很简单的自解压脚本,准备的文件:
.ZiJieYa-dir
|-- this-is-directory
| |-- and-this-is-dir-too
| | `-- html-document.html
| |-- this-is-7z-file.7z
| `-- this-is-txt-file
|-- this-is-odt-word-file.odt
`-- this-is-py-script.py
把上面的 ZiJieYa-dir 压缩成 archive.tar.gz 文件,然后再来写一个脚本,脚本内容如下:
#!/bin/sh
#Project self-extracting.run by vzomik
mkdir ./self-extracting
tail -n +7 $0 | tar zxvf - -C ./self-extracting && ls ./self-extracting
exit
__ARCHIVE__FILE__BELOW__
很容易明白,不是么?上面的脚本就是把 $0 的第7行及以下的内容用管道传送给 tar 解压的意思,$0 就是自身。
好!再来将压缩包文件 archive.tar.gz 与脚本文件 self-extracting.run 合并:
cat archive.tar.gz >>self-extracting.run
呵呵,这样的话,这个有趣的dotRUN自解压脚本就做成了,试试在 Bash 中运行:
chmod +x self-extracting.run
./self-extracting.run
就可以在当前目录发现 self-extracting 目录,里面乖乖的躺着 archive.tar.gz 解压后的文件!
而下半部分是乱码??呵呵,其实呀,这是一个 Shell 脚本而已!不信的话可以用 GVim 打开那个 .run 文件。
现在我们来学习做一个 .run 文件,当然,没有那个显卡驱动那么夸张,只是一个很简单的自解压脚本,准备的文件:
.ZiJieYa-dir
|-- this-is-directory
| |-- and-this-is-dir-too
| | `-- html-document.html
| |-- this-is-7z-file.7z
| `-- this-is-txt-file
|-- this-is-odt-word-file.odt
`-- this-is-py-script.py
把上面的 ZiJieYa-dir 压缩成 archive.tar.gz 文件,然后再来写一个脚本,脚本内容如下:
#!/bin/sh
#Project self-extracting.run by vzomik
mkdir ./self-extracting
tail -n +7 $0 | tar zxvf - -C ./self-extracting && ls ./self-extracting
exit
__ARCHIVE__FILE__BELOW__
很容易明白,不是么?上面的脚本就是把 $0 的第7行及以下的内容用管道传送给 tar 解压的意思,$0 就是自身。
好!再来将压缩包文件 archive.tar.gz 与脚本文件 self-extracting.run 合并:
cat archive.tar.gz >>self-extracting.run
呵呵,这样的话,这个有趣的dotRUN自解压脚本就做成了,试试在 Bash 中运行:
chmod +x self-extracting.run
./self-extracting.run
就可以在当前目录发现 self-extracting 目录,里面乖乖的躺着 archive.tar.gz 解压后的文件!
- Bash Self-Extracting Script
- To install the Linux (self-extracting) file
- Bash Script
- shell script - self learning
- JDK Self-Extracting Binary Installation for Linux (32-bit)
- shell script - BASH
- Shell Script(bash)简介
- Linux Bash Script
- linux bash script
- bash shell script
- Bash Shell Script Tutorials
- 学习心得之bash script
- create bash-shell-script
- Bash Script 格式化工具
- Linux Bash Script
- 浅入bash script
- bash script example
- Shell Script(bash)教学例
- 欢迎界面
- 安装织梦之插曲(include/**/templets/default/index.htm Not Found!)
- 创建带默认值的参数并执行
- Howto: Ubuntu下的AMSS编译环境配置
- 程序是否健壮和语言弱相关、和你的思路强相关
- Bash Self-Extracting Script
- Linux紧急情况处理方法
- strcpy()、memcpy()、memmove()、memset()的实现
- 我开博客啦
- 【写给程序员】请不要做浮躁的人
- POJ 3981 字符串替换 (string的基础使用)
- 配置vim
- seuthesis-2.0.0与texlive2009
- Getting Started[Asterisk 1.8 Documentation]