0

So I have a client and a server Java program. The client uses Java processbuilder to execute the script but my problem is that the user inputs information that needs to be passed to the bash script. So, essentially, I need to know how to send three different strings to three different variables that are being read by the bash script. This script is copying a file so I would rather not make a txt file with java and have the script read the file. I would also like a way for this to be able to run on OS X and Windows so improvements are welcome. I am using Java 7 on Ubuntu currently.

Here is a snippet of what I am trying to do: .java

    Scanner bob = new Scanner(System.in);
    String workingDirectory = new String(System.getProperty("user.dir"));
    File tempDir = new File(workingDirectory); 
    String script = new String(workingDirectory + "/copyjava.sh");

    System.out.print("Designate the location of the file: ");
    String loc = bob.next();
    System.out.print("Type the name of the file w/ extension: ");
    String name = bob.next();
    System.out.print("What is the location of THIS file? "); //I know there is a way to do this automagically but I can't remember how...
    String wkspace = bob.next();
    ProcessBuilder pb = new ProcessBuilder( script, loc, name, wkspace);
    pb.start();
    File myFile = new File (name);

Script:

read loc

read name

read wkspace

cd $LOC

cp $name $wkspace
jordanm
  • 33,009
  • 7
  • 61
  • 76

3 Answers3

1

There is a problem with your shell script. The read command reads from stdin, but you are passing the input as arguments. You are also changing the case of the loc variable. Variables in the shell are case sensitive. Change your script to the following:

#!/bin/sh
loc=$1
name=$2
wkspace=$3
cd "$loc" || { printf 'failed to cd to %s\n' "$loc" ; exit 1; }
cp "$name" "$wkspace" || { printf 'failed to copy %s\n' "$name" ; exit 1; }

On a side note, you shouldn't need to call an external script written in a different language just to copy a file. You should implement this in java. Implementing this in java will also give your code the platform independence you desire.

jordanm
  • 33,009
  • 7
  • 61
  • 76
  • I just recently started scripting so this is all new to me. How would you implement this in Java? I couldn't find anything on copying files from directories outside the working directory. – Mrcheese123 Jul 25 '12 at 23:51
  • A SO search shows this: http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java – jordanm Jul 25 '12 at 23:56
0

You are passing your args on the command line but reading from stdin in your script. How about changing your script to:

cd $1

cp $2 $3
Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
0

I don't see any client/server interaction but let's focus on what's really important: Your are passing the parameters to the script but your script is trying to read them from the standard input.

To fix your problem modify your script as follows:

#!/bin/sh
LOC=$1
name=$2
wkspace=$3

cd $LOC

cp $name $wkspace

Take a look at the documentation for more details.

But are not doing anything that would really need a system-specific script file. The best way to copy a file is using the own mechanism that Java provides and then you don't need to worry of the underlying operating system.

If you keep on using the script then you'll need another one for Windows systems and then decide which script you should run based on the value of the os.name system property.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37