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

Fair Selection Criteria

“Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.” – Albert Einstein

I don’t wanna say much about our failed education or selection system but the following describes the all.

A fair selection
A fair selection

Thinking about Application Performance

The following article is taken from book: Patterns of Enterprise Application Architecture

 

patterns of enterprise application architecture
Patterns of Enterprise Application Architecture

Thinking about Application performance:

One of the most important metrics in success of an application is performance. If an application has a lot of functionality but if it has low performance then it would be a failure. An application would be considered successful only if it has good functionality as well as good performance. In this post my focus is on Enterprise applications. Performance of an enterprise application can be measured in terms of many metrics.  I am writing about some of the performance metrics here.

  1. Response time
  2. Responsiveness
  3. Latency
  4. Throughput
  5. Load
  6. Load Sensitivity
  7. Efficiency
  8. Capacity
  9. Scalability

Response Time

It is the amount of time that a system requires to process a request. This request can be an UI action like pressing a button.

Responsiveness

It is measured in terms of acknowledgement time. In contrast with response time it is the amount of time system takes to acknowledge a user about the status of his request.

You might have seen progress bars in web browsers or in application installers. These kinds of elements are used to acknowledge user about the status of his request.

If a system has low responsiveness then user might get frustrated.

Latency

It is the minimum time to get any kind of response. This time may be due many reasons. It may be due to network or due to processing delays etc.

As system architect we can’t remove latency but as a thumb rule we can reduce our remote calls or IOs to improve system performance.

Throughput

It is defined as the amount of tasks completed by a system in a given span of time. Throughput can have different units in different scenarios like in case of data transfer its unit will be bytes per second. While we are talking about enterprise applications we measure throughput in terms of transactions per second (tps). For your particular system you should pick a common set of transactions.

In this terminology performance is either throughput or response time—whichever matters more to you. It can sometimes be difficult to talk about performance when a technique improves throughput but decreases response time, so it’s best to use the more precise term. From a user’s perspective responsiveness may be more important than response time, so improving responsiveness at a cost of response time or throughput will increase performance.

Load

It is a statement of how much stress a system is under, which might be measured in how many users are currently connected to it. The load is usually a context for some other measurement, such as a response time. Thus, you may say that the response time for some request is 0.5 seconds with 10 users and 2 seconds with 20 users.

Load sensitivity

It is an expression of how the response time varies with the load. Let’s say that system A has a response time of 0.5 seconds for 10 through 20 users and system B has a response time of 0.2 seconds for 10 users that rises to 2 seconds for 20 users. In this case system A has a lower load sensitivity than system B. We might also use the term degradation to say that system B degrades more than system A.

Efficiency

It is performance divided by resources. A system that gets 30 tps on two CPUs is more efficient than a system that gets 40 tps on four identical CPUs.

Capacity

The capacity of a system is an indication of maximum effective throughput or load. This might be an absolute maximum or a point at which the performance dips below an acceptable threshold.

Scalability

It is a measure of how adding resources (usually hardware) affects performance. A scalable system is one that allows you to add hardware and get a commensurate performance improvement, such as doubling how many servers you have to double your throughput. Vertical scalability, or scaling up, means adding more power to a single server, such as more memory. Horizontal scalability, or scaling out, means adding more servers.
The problem here is that design decisions don’t affect all of these performance factors equally. Say we have two software systems running on a server: Swordfish’s capacity is 20 tps while Camel’s capacity is 40 tps. Which has better performance? Which is more scalable? We can’t answer the scalability question from this data, and we can only say that Camel is more efficient on a single server. If we add another server, we notice that swordfish now handles 35 tps and camel handles 50 tps. Camel’s capacity is still better, but Swordfish looks like it may scale out better. If we continue adding servers we’ll discover that Swordfish gets 15 tps per extra server and Camel gets 10. Given this data we can say that Swordfish has better horizontal scalability, even though Camel is more efficient for less than five servers.

When building enterprise systems, it often makes sense to build for hardware scalability rather than capacity or even efficiency. Scalability gives you the option of better performance if you need it. Scalability can also be easier to do. Often designers do complicated things that improve the capacity on a particular hardware platform when it might actually be cheaper to buy more hardware. If Camel has a greater cost than Swordfish, and that greater cost is equivalent to a couple of servers, then Swordfish ends up being cheaper even if you only need 40 tps.