0

I have this Script that simply mount the attached External drive and write their mounted path in a text file. Though when I run this script from terminal in tinyCOre it works fine. Following is the Code I am using to execute the script, I am using this code throughout my application for mounting and other purposes but non of them have writing to do.

Code:

  Process p = Runtime.getRuntime().exec(" sh /home/abc/mnt-ddc.sh");
   p.waitFor();

I even tried taking the script as array but no help. Quick help will be appreciated. Thanks.

UPDATE WHen the script is executed from java, nothing happens, no exception thrown, no nothing. I tried all the ways answered below none working.

mnt-ddc.sh

#!/bin/sh
blkid -s LABEL | grep ddc- > ddc.txt        # Get DDC device name and label and write to ddc.txt

perl -pi -e 's/ LABEL="//g' ddc.txt     # Remove text "ddc- from ddc.txt

#perl -pi -e 's/ LABEL="ddc-//g' ddc.txt        # Remove text "ddc- from ddc.txt
perl -pi -e 's/" //g' ddc.txt           # Remove left over text " from ddc.txt

while IFS=: read dev label          # start loop and read device name to $dev and LABEL to $label
do

if mount | grep $dev; then
   echo -e "Already mounted"
else
    if [ ! -d /mnt/$label ]; then
       echo -n "Creating mount point..."
       sudo mkdir /mnt/$label
    fi
        echo -n "Mounting......"$label
    sudo mount $dev /mnt/$label
        echo
    if [ "$?" != "0" ]; then
        echo "Mount failed.  Exiting."
        echo "" > ddc.txt
        exit
    fi
fi

done < ddc.txt
3
  • 3
    You haven't said what actually happens when you run the script from Java, and what the script contains. Commented Dec 5, 2013 at 5:14
  • How about using /bin/sh instead of just sh? Commented Dec 5, 2013 at 6:03
  • @JimGarrison Please see the Script Code Commented Dec 5, 2013 at 7:09

2 Answers 2

4

Did you notice the space in front of "sh"? Why spawn a new shell? I would make the script directly executable (e.g. chmod +x /home/paftdl/NavData/mnt-ddc.sh) and then just call it -

Process p = Runtime.getRuntime().exec("/home/paftdl/NavData/mnt-ddc.sh");
InputStream is = p.getInputStream(); // Let's print what we get.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
  System.out.println(line);
}
Sign up to request clarification or add additional context in comments.

2 Comments

this really helped! However, it is not writing to file.
You need to set your working dir (or update your script to be explicit about the output file path); that is ` File workingDir = new File("/home/paftdl/NavData/");` and then Process p = Runtime.getRuntime().exec("/home/paftdl/NavData/mnt-ddc.sh", null, workingDir);
0

Although it is better to use ProcessBuilder

The problem you are having is that you have a space in front of sh

Process p = Runtime.getRuntime().exec("sh /home/paftdl/NavData/mnt-ddc.sh");
p.waitFor();

Comments

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.