In Windows, there are some files that can be decompressed by yourself, that is, the so-called self-decompressed files. You don't need to know which compression technology is used to compress the file, you can simply decompress the file by running the self-extracting file.
However, in Unix/Linux systems, there is no way to achieve this goal. Today, I suddenly wanted to use a Unix shell to implement self-decompression. After trying it, it was not very difficult and it was easy to achieve this goal without advanced technologies.
The main idea is the same as the idea of self-extracting files in windows, that is, adding a file header to the existing compressed file. The file header can be directly executed. The steps are as follows:
- Extract the content of the subsequent compressed package of the file and put it in a temporary file;
- Decompress the temporary file;
- Delete the temporary file;
- Exit.
Therefore, you only need to write a script program and add this part to the header of each file to be decompressed.
The following is the shell script (extend. Sh) for adding the unpackage header to the TAR file ):
#! /Bin/sh
[$ #! = "1"] & Echo "Usage: Extend. Sh tarfile" & Exit 0
Filename = "$ 1.sh"
Echo "#! /Bin/sh "> $ filename
Echo "lines = \ 'wc-L \ $0 | awk '{print \ $1}' \ '"> $ filename
Echo "lines = \ 'expr \ $ lines-6 \ '"> $ filename
Echo "tail-n \ $ lines \ $0> _temp_.tar"> $ filename
Echo "Tar xvf _temp_.tar"> $ filename
Echo "RM _temp_.tar"> $ filename
Echo "Exit 0"> $ filename
Chmod + x $ filename
Cat $1 >>$ filename
Use extend. Sh tarfile to generate a sh file in the current directory. This is the self-extracting tarfile file.
If you do not understand the above shell scripts, You can first look at the Unix shell programming entry section in the "test programming" section.