Why only partiial shell script got executed when called in Java


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Why only partiial shell script got executed when called in Java
# 1  
Old 10-06-2011
Why only partiial shell script got executed when called in Java

I'm trying to call shell scripts from Java for DB operations. Using a very simple test, for some reason, one line of (ALTER TABLE) in the shell simply won't be executed while the exit value from the call is "0" (valid exit). When I run the script directly in Unix, it gets executed perfectly fine!! So it is not script error but the workings of this Runtime or something else. Please help!!Smilie

My Java program is:

Code:
public class scriptBuilder {
    private static final Logger logger = LoggerFactory.getLogger(scriptBuilder.class);

    public scriptBuilder() {
    }

    public void writeScript(String scriptFileName) throws java.io.IOException {
        Runtime rt = Runtime.getRuntime();
        String script = scriptFileName;
        rt.exec("chmod 777 " + script);
        Process p = null;
        try {
            String shellCmd = "./" + script;
            p = rt.exec(shellCmd);
            int exitVal = p.waitFor();
            logger.info("rt.exec exit value = " + exitVal);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        String shellfilename = "testing.txt";
        if (new File(shellfilename).exists()) {
            logger.info(shellfilename+" File exists");
        }
    }

}

    public static void main(String []args)    { 
            try {
                sb.writeScript("testingScriptBuilder.sh");
            } catch (IOException e) {
                e.printStackTrace();
            }
}

The shell script being called "testingScriptBuilder.sh" is as below:
Code:
#!/bin/bash


DBCON="mysql -usmartie -phal77berd"
DPS_DB="dps4"

function d {
$DBCON << EOF1

ALTER TABLE ${DPS_DB}.sz_avm_g1 ADD COLUMN test_col INT(1);
DROP TABLE IF EXISTS ${DPS_DB}.test_script;
CREATE TABLE ${DPS_DB}.test_script (property_id int(12));

EOF1
    
}
d


No matter how I organize this script - using function or not, rearraging the order of the lines - the later table line:
Code:
ALTER TABLE ${DPS_DB}.sz_avm_g1 ADD COLUMN test_col INT(1);

simply won't be executed!!!!!
When run standalone in the Unix, this line gets executed perfectly fine!!!!!!

PLEASE HELP IF YOU CAN.... THANKS A LOT.Smilie

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags

Last edited by pludi; 10-06-2011 at 04:48 PM..
# 2  
Old 10-06-2011
777 is not the magic sledgehammer to fix all permissions problems. By using it, you've made that script writable by any user on that system. Literally anyone could edit it and maybe put 'DROP DATABASES' into it. This is bad.

What happens when you execute this script from a proper shell? It might be printing error messages you're not seeing.

Also, use code tags for code please. Whitespace is important in some places in shell scripts and not putting your code in code tags does not preserve this.
# 3  
Old 10-06-2011
- Thanks for the suggestion on 777.
- The same script gets executed complete and fine when run from a shell (just not from java)
- What did you mean by "using code tags"? Sorry I'm new to shell scripting...
# 4  
Old 10-06-2011
Quote:
Originally Posted by zmwang
- What did you mean by "using code tags"? Sorry I'm new to shell scripting...
He meant this: Video tutorial on how to use code tags
# 5  
Old 10-06-2011
Thanks for the video for cod tags.
I just found that the ALTER TABLE line works on some other table in the same DB but not that table... Still trying to understand this part....
(still, if run the original script from shell, it get executed fine on this table...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

To check if the JAVA Program is successfully executed in sh shell scripting

Hi , I have written a shell script to call a java program say load_id.sh .This sh script indeed is executed implicitly in other sh script which calls 2 more sh scripts one by one. I need to check if the load_id.sh (which calls java program) is executed successfully only then continue with... (1 Reply)
Discussion started by: preema
1 Replies

3. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

4. Programming

Remote login UNIX box from java passing parameters to the custom script called in the profile

Hello Good Day / Guten Tag.... I have to login the server and the user profile contains some scripts which need the inputs to be taken from the keyboard. So I use the method to conn.authenticateWithKeyboardInteractive(username, new InteractiveCallback() { public String... (1 Reply)
Discussion started by: Sanalkumaran
1 Replies

5. Shell Programming and Scripting

Shell script not getting executed

Hi As per my requirement when I run . ./file.sh am getting the following error -bash:ELF: command not found when i execute as ./file.sh it is getting executed.How to resolve this. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

6. Shell Programming and Scripting

Shell script executed from Informatica ETL tool is spawning 2 processes for one script

Hi, I am having a shell script which has a while loop as shown below. while do sleep 60 done I am executing this script from Informatica ETL tool command task from where we can execute UNIX commands/scripts. When i do that, i am seeing 2 processes getting started for one script... (2 Replies)
Discussion started by: chekusi
2 Replies

7. Shell Programming and Scripting

help with shell script executed by php.

I made a shell script to execute a server in screen mode. # start server screen -d -m -S Test ./application echo "Program Started Successfully" than I'm executing it from php by echo shell_exec('/home/script.sh'); and it is giving me this error. "cannot make directory... (1 Reply)
Discussion started by: dmallia
1 Replies

8. Shell Programming and Scripting

How to return the value from the called shell script to the calling sh script

Hi all, I have two ksh scripts #sample1.sh #!/bin/ksh . ./sample2.sh echo $fileExist #sample2.sh #!/bin/ksh func() { i=1 return $a } func echo $? Here how should I return the value of sample2.sh back to sample1.sh? Thanks in advance. (2 Replies)
Discussion started by: gp_singh
2 Replies

9. AIX

Help - Need simple example of VI executed in shell script

Please help - I have seen others ask this question but I need a simple example of using vi in a shell script. Once I enter VI the shell script does not execute the next commands until I q!. I invoke VI and start the edit process. I want to go to the third line and replace a character with a new... (2 Replies)
Discussion started by: corsart
2 Replies

10. Shell Programming and Scripting

gzip in shell script called by cron

I'm puzzled by this one. I hope you can explain it to me. I have a ksh shell script that gzips a file among other things. This works perfectly fine when the script is manually run through a shell. However, when the same script is run through cron, it does everything correctly, but it will... (2 Replies)
Discussion started by: hbau419
2 Replies
Login or Register to Ask a Question