I have seen a .sh file on execution in terminal it extracts some jar files inside the sh file, how to create such sh files? i could not open that sh file in normal gedit to check the content. Is there any way to do that?
-
1Please have a look at http://stackoverflow.com/questions/18410785/bash-script-containing-binary-executable – Janek Oct 17 '16 at 07:32
-
The trick to a self extracting shell file is to simply create a normal archive using standard tools such as `tar` (or zip, cpio etc.) and then wrap the resulting `.tar` file in a couple lines of shell script that will separate out the archive from the script, extract it again and execute any additional setup steps. Decoding that should be trivial, but an editor attempting to do syntax highlighting or matching braces on the binary contents of such a file might be quite slow. Try `/bin/vi` instead of `gedit`. – HBruijn Oct 17 '16 at 08:47
-
Maybe you're thinking of *shar* for shell archives? See `man shar` for details. – CodeGnome Oct 22 '16 at 05:42
2 Answers
Use shar for Shell Archives
Create shell archives using the shar utility. For example:
# Create some fixture data to archive.
cd /tmp
mkdir -p foo/bar/baz
touch foo/bar/baz/quux
# Create the shell archive.
find foo/ -exec shar {} + > quux.sh
# Remove fixture data.
rm -rf foo/
# Extract archive to recreate directories and files.
sh quux.sh
You can validate that this recreated your data with ls -R foo
. In addition, if you inspect quux.sh, you'll see that it's composed of a series of simple shell commands. You can insert those commands directly into another shell script, if desired, or call the archive from another script to unpack it.
Caveats
The BSD version of shar on macOS doesn't handle binary data without a separate encoding step, while the GNU version has various encoding options available to it. If your version of shar doesn't handle binary encoding, you may be better off with a more robust archiving option like tar. Your mileage may vary.

- 285
- 2
- 9
To compress a file or set of files you can use the gzip
command in UNIX.
To zip a single file use:-
$ gzip filename
It will create a file named filename.gz
.
To zip a folder, first create a .tar
file and then zip it.
Let the name of the folder be shell
.
To create a .tar
file use:-
$ tar cvf filename.tar shell/.
It will create a file named filename.tar
Now use
$ gzip filename.tar
It will create a file named filename.tar.gz
.
So in this way you can compress a file.
Note: To uncompress a file you can use gunzip
command.

- 4,225
- 5
- 23
- 28

- 20
- 2