Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script with jar file at the end

Tags:

java

shell

jar

I download an archive file. In the archive there will be a file that has a .sh. extension. When I opened that file with VI I found the below code in the beginning of the file:

#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
    java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$@"
exit 1

I can run the jar by doing java -jar file or `./file'.

Can someone explain me what is going on? How can you create such file?

like image 358
Hunsu Avatar asked Jun 21 '15 18:06

Hunsu


People also ask

What is EOF in shell script?

In computing, end-of-file (EOF) is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream.

What is $? 0 in shell script?

After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.


1 Answers

Try by yourself the following commands. Start creating a normal jar file with any content, or use someone you have. I will name it "myjar.jar"

Next, create a file "hello.sh" with content:

#!/bin/bash

exec echo hello

now, add this file at start of a new jar file:

cat hello.sh myjar.jar > mytrick.jar
chmod 700 mytrick.jar

And finally, the interesting part, type:

./mytrick.jar
jar -tf mytrick.jar
unzip mytrick.jar

in other words, usually jar/unzip skips any content until their own header. Moreover, a shell script "ends" in a line who call "exec" (because shell interpreter is replace at this point by the command in the exec line).

However, this trick is based in a behaviour of jar/unzip probably out of standards. Note, by example, that this statement fails (has no effects):

jar -xf mytrick.jar
like image 145
pasaba por aqui Avatar answered Sep 22 '22 17:09

pasaba por aqui