Self-decompressed tar files using Unix shell

Source: Internet
Author: User

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:

  1. Extract the content of the subsequent compressed package of the file and put it in a temporary file;
  2. Decompress the temporary file;
  3. Delete the temporary file;
  4. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.