Shell Script Arguments with Spaces from JAVA

Today i was doing my office work. It was about automating a process by calling shell scripts from java code. I got a very time consuming issue it took my almost 4 hours to resolve it. Actually in start i searched for replacement not for the direct solution.

Problem Statement:

The problem was that whenever you pass some shell script arguments with spaces from java code which have spaces in it the value isn’t passed correctly. It breaks the value at first space. For example:

If you have passed “second argument” form java code then in shell script the statement “export SECOND_PARAM=$2” will set the value of SECOND_PARAM to “second” only.

Shell Script Arguments with Spaces POC:

The following java code snippet is written for this POC.

public static void main(String[] args) {
String userDir = System.getProperty("user.dir");
System.out.println("User dir : " + System.getProperty("user.dir"));
Integer intVal = -1;
String[] command = { "./resources/dumpScript.sh", "firstArgument", "second Argument" };

try {

BufferedReader stdInput = null;
BufferedReader stdError = null;

Process p = Runtime.getRuntime().exec(command);
stdInput = new BufferedReader(new InputStreamReader(p
.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p
.getErrorStream()));

String line;
System.out.println("===============stdInput================");
line = stdInput.readLine();
while (line != null) {
System.out.println(line);
line = stdInput.readLine();
}
System.out.println("=======================================");

System.out.println("===============stdError================");
line = stdError.readLine();
while (line != null) {
System.out.println(line);
line = stdError.readLine();
}
System.out.println("=======================================");

intVal = p.waitFor();

try {
if (stdInput != null)
stdInput.close();
} catch (Exception ignored) {
}
try {
if (stdError != null)
stdError.close();
} catch (Exception ignored) {
}

} catch (Exception e) {
System.out.println(e.getMessage());
}
System.setProperty("user.dir", userDir);
System.out.println("Current dir : " + System.getProperty("user.dir"));
}

and the following is the shell script written for this POC.

echo "start of the Shell script."

export FIRST_PARAM=$1
export SECOND_PARAM=$2

echo "First arg= " $FIRST_PARAM
echo "Second arg= " $SECOND_PARAM

echo "end of the Shell script."

When i run the code first i got the following output:


User dir : /home/haris/workspace/Shell Script Arguments with Spaces POC
java.io.IOException: Permission denied
Current dir : /home/haris/workspace/Shell Script Arguments with Spaces POC

This was because my shell script file did not have execute permissions on linux. To give permissions to your shell scripts do the following:

Set File permissions in Linux:

cd "path of directory where your shell script is"
chmod 777 dumpScript.sh

After successful execution of my shell script i got the following output:

User dir : /home/haris/workspace/Shell Script Arguments with Spaces POC
===============stdInput================
start of the Shell script.
First arg= firstArgument
Second arg= second
end of the Shell script.
=======================================
===============stdError================
=======================================
Current dir : /home/haris/workspace/Shell Script Arguments with Spaces POC

Here you can see “Second arg= second” You can see in java code snippet that i passed  “second Argument” as argument to this variable but only “second” could be set in the variable.

Solution for shell scripts arguments with spaces:

Change your shell script. Simply add double quotes around “$2”. The new shell script is copied below:

echo "start of the Shell script."

export FIRST_PARAM=$1
export SECOND_PARAM="$2"

echo "First arg= " $FIRST_PARAM
echo "Second arg= " $SECOND_PARAM

echo "end of the Shell script."

After running the program again you can see the correct output. Following snippet shows the correct output i got after this modification.

User dir : /home/haris/workspace/Shell Script Arguments with Spaces POC
===============stdInput================
start of the Shell script.
First arg=  firstArgument
Second arg=  second Argument
end of the Shell script.
=======================================
===============stdError================
=======================================
Current dir : /home/haris/workspace/Shell Script Arguments with Spaces POC

Source Code:

Download code from here: Shell Script Arguments with Spaces POC

References:

http://stackoverflow.com/questions/905891/passing-argument-containing-space-in-shell-script

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook