1

I am writing a Java program from which I am executing a shell script. The shell script is calling another shell script. I am trying to print output returned by child script to parent shell script. Below is my code.

public class App 
{
    public static void main( String[] args ) throws IOException, InterruptedException 
    {
        String command = new String("/home/Test.sh");   
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec(command); 
        process.waitFor(1, TimeUnit.MINUTES);
        BufferedReader reader = new BufferedReader(new InputStreamReader(        
                process.getInputStream()));                                          
        String s;                                                                
        while ((s = reader.readLine()) != null) {                                
            System.out.println("Script output: " + s);                             
        }   

Shell Script: Test.sh

#!/bin/bash
  result=$( bash myscript.sh )
  echo "$result"

myscript.sh

 #!/bin/bash
    echo "50"

I am getting empty output. I initial thought it might be because the Java process is not waiting for the shell script to finish. So added waitFor() but still no use. Can some one kindly help.

2
  • 1
    check the error stream because it works on my side with your code Commented Aug 4, 2016 at 14:08
  • try this; result=$( bash /home/myscript.sh ) , full path of myscript.sh Commented Aug 4, 2016 at 14:20

1 Answer 1

1

try this; this is not waiting problem.

#!/bin/bash
  result=$( bash /home/myscript.sh ) # full path of myscript
  echo "$result"

Also handle bash error as below;

public static void main(String[] args) throws IOException {
        String command = new String("/tmp/1/Test.sh");   
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(        
                process.getInputStream()));

        String s;                                                                
        while ((s = reader.readLine()) != null) {                                
            System.out.println("Script output: " + s);   
        }

        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(process.getErrorStream()));

        System.out.println("Here is the standard error\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }

    }

adduser tests;

1- myscript.sh:

#!/bin/bash
adduser test
echo "50"

java console output;

Script output: 50
Here is the standard error of the command (if any):

adduser: Only root may add a user or group to the system.

2- myscript.sh:

#!/bin/bash
sudo adduser test
echo "50"

java console output;

Script output: 50
Here is the standard error of the command (if any):

sudo: no tty present and no askpass program specified

3-
myscript.sh:

#!/bin/bash
echo -e "YOURPASSWORD=\n" | sudo -S adduser -shell /bin/bash --home /home/test test
echo -e "YOURPASSWORD\n" | sudo deluser --remove-home test
echo "OK"

java console output;

Script output: Adding user `test' ...
Script output: Adding new group `test' (1002) ...
Script output: Adding new user `test' (1001) with group `test' ...
Script output: Creating home directory `/home/test' ...
Script output: Copying files from `/etc/skel' ...
Script output: Try again? [y/N] Changing the user information for test
Script output: Enter the new value, or press ENTER for the default
Script output:  Full Name []:   Room Number []:     Work Phone []:  Home Phone []:  Other []: Is the information correct? [Y/n] Looking for files to backup/remove ...
Script output: Removing files ...
Script output: Removing user `test' ...
Script output: Warning: group `test' has no more members.
Script output: Done.
Script output: OK
Here is the standard error of the command (if any):

[sudo] password for ..: Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
passwd: password unchanged
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 563.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 564.
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.. Adding the full path helped. I like to understand the bash error handling. What kind of errors will be captured? Say I am using "adduser" command in script, "only root users will be able to add new users" error will be thrown if not an root user. Will that be captured here?
Yes, that will be captured. Also I try to some test as above; Maybe this can be help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.