I need to call a Unix script in my Java code, wait for it to execute, and get the output. The script will do a 'find', and user will provide the directory he wants the 'find' to look in
String[] cmd = new String[] { "/bin/sh", "-c",
runnerScript.getAbsolutePath() + " " + Param1 };
Process p = Runtime.getRuntime().exec(cmd);
Problem is : How can I prevent one to type ' && rm -rf *'
or ' / -exec rm..'
?
I have an idea like check if the directory exist on the server, or use a method like isLetterOrDigit()
, but I want to know if a safer way exists.
I know for Sql Query I can use PreparedStatement to work around this problem. Is there something similar when calling a script?