I am trying to create a shell script from java code. I am using following method to create the shell script.
String cmd = "echo -e \"echo -e abc\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\" > ";
String [] commands = { "/system/bin/sh", "-c", cmd + "script.sh " };
Runtime.getRuntime().exec(commands);
However, when I open the script.sh file I see following :
echo -e abc\*7\*7\*7\*7\*7\*7\*7\*7
What I want is the following:
echo -e "abc\0177\0177\0177\0177\0177\0177\0177\0177"
What is wrong with I am doing ? Any idea how to fix this ?
Not sure why you need this but you can use single quote in echo and 2 backslashes:
String cmd =
"echo -e 'echo -e abc\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177' > ";
Related
The following command executes fine in bash:
Command:
bash -c "$(echo 'H4sIAArQ/mAAA1WMuw7CIBRAd77ihLJqtKuTg19hHIjetiQU0svl/1sn43weaeKJD4PnlI2R1w1bpOBA3kvF340ssX1Z1LmvUqyhsvWk8jl7nOQmP/2x9ZixSlXWqnLcYvlrw4VwJYxHOiW3AwCHgS2AAAAA' | base64 --decode | zcat)" - -a -b
Output:
Equal to or more than 2 arguments - -a -b
Wanted to know - how can I achieve this using Java's ProcessBuilder?
I tried the following:
ProcessBuilder processBuilder = new ProcessBuilder(args);
where args are:
bash
-c
"$(echo 'H4sIAArQ/mAAA1WMuw7CIBRAd77ihLJqtKuTg19hHIjetiQU0svl/1sn43weaeKJD4PnlI2R1w1bpOBA3kvF340ssX1Z1LmvUqyhsvWk8jl7nOQmP/2x9ZixSlXWqnLcYvlrw4VwJYxHOiW3AwCHgS2AAAAA' | base64 --decode | zcat)"
-
-a
-b
But I keep on getting the following error:
-: if: command not found
Process finished with exit code 127
Can someone please point out the issue here?
Command substitution results, in bash, don't go through all parsing steps. That means that compound commands like if aren't honored, command separators like ; have no syntactic meaning, etc.
If you want to override that and force an additional parsing pass, you need to use eval. Thus:
args = String[]{
"bash",
"-c",
"eval \"$(echo 'H4sIAArQ/mAAA1WMuw7CIBRAd77ihLJqtKuTg19hHIjetiQU0svl/1sn43weaeKJD4PnlI2R1w1bpOBA3kvF340ssX1Z1LmvUqyhsvWk8jl7nOQmP/2x9ZixSlXWqnLcYvlrw4VwJYxHOiW3AwCHgS2AAAAA' | base64 --decode | zcat)\"",
"-",
"-a",
"-b",
}
Why did this work when you ran it in a shell, instead of from a ProcessBuilder? Because that shell you ran it in would perform the command substitution in "$(...)", and put the results of that substitution in the text it passed to the child shell; so the substitution was already done at parsing time.
I'm trying to execute a bcp command like below in linux using java:
bcp dbname..mytable in /home/guest/test -U guest -P guest -S LXXDB1D06 -I /opt/sybase/08/interfaces -c -Y -t \| -r \\n
In Java Class, I do as:
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec("bcp dbname..mytable in /home/guest/test -U guest -P guest -S LXXDB1D06 -I /opt/sybase/08/interfaces -c -Y -t \| -r \\n");
p.waitFor();
} catch(..){
..}
I tried with \|, "\"+"\n" and other few.
But didnt work.
What should be the correct format?
Edit: With ProcessBuilder, it looks like:
[bcp, pfactdbcpusdev01..gb_inactive_upc, in, C:\hs_data_dev_00\itm_mstr\tmp\usaupcinact_tmp_rollup, -U, inactupcUSD1, -P, inac01, -S, ACNLNXQ002D01, -I, C:\Program Files\Nielsen\Sybase\12.5.1/interfaces, -c, -t, |, -r, \n, -Y]
This is a super classical problem and I'm frankly astonished that you didn't find the solution by googling around...
The solution is to use a ProcessBuilder:
final ProcessBuilder pb = new ProcessBuilder("bcp", "dbname..mytable",
"in", "/home/guest/test",
"-U", "guest",
"-P", "guest",
"-S", "LXXDB1DO6",
"-I", "/opt/sybase/08/interfaces",
"-c",
"-Y",
"-t", "|",
"-r", "\\n"
);
final Process p = pb.start();
Also, you should check the result of .waitFor().
Also check the manpage for execve(2), execl(2) and friends.
I am trying to run bash script from java file. The script runs only the echo statement and the remaining code doesn't get executed. However, when the script is run separately, it works fine.
Java Code: Trying to run bash script from java code
Process p3 = Runtime.getRuntime().exec(chmod 777 "/path/runall_1");
String line = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(p3.getInputStream()));
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
System.out.println(output);
p3.waitFor();
Script runall_1: Compile and run C files using script
cflag="-std=gnu99 -O3"
rm -rf core
algorithm="fgme"
echo ${algorithm}
gcc ${cflag} -DAlgorithm=${algorithm} Newharness.c -lpthread -lm
Time="2"
N="32"
F="2"
rm -rf core
T="1"
while [ ${T} -le ${N} ] ; do
./a.out ${T} ${Time} ${F} # Zhang d-ary
if [ -f core ] ; then
echo core generated for ${T} ${Time} ${F}
break
fi
T=`expr ${T} + 1`
done
if [ -f core ] ; then
echo core generated for ${1}
break
fi
Output: fgme
Build Successful...
I need to schedule a windows job to loop thru thousand of files and execute a command with the command options, the file name, and extension.
These files have an extension of *.xls in directory c:\proj. For example, one of the file is myImportantFile0001.xls, I would execute the following in powershell, manually:
PS> java.exe -classpath "C:\PROGRA~2\my1.0.2\lib/*" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:"myImportantFile0001/discovery:discovery" "C:\proj\myImportantFile0001_xls.txt"
couple of problems I've got are:
the command itself has double quotes,
the file name and extensions need to be found and converted
execute a java within powershell
So how can I loop thru the thousand of files to build a string to execute the command using powershell?
does below script helps you? i think it just strings combination.
$Commands = ls C:\Temp\*.xlsx | %{
"java.exe -classpath `"C:\PROGRA~2\my1.0.2\lib/*`" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:`"$($_.BaseName)/discovery:discovery`" `"C:\proj\$($_.BaseName)_xls.txt`""
}
$Commands
exit # remove the line if you want to execute
$Commands | %{
& $_
}
What about
PS> ls C:\Temp\*.xlsx | foreach-object {
"java.exe -classpath `"C:\PROGRA~2\my1.0.2\lib/*`" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:`"$($_.name)/discovery:discovery`" `"C:\proj\$($_.name)_xls.txt`""
}
You can do it like this:
$files = Get-ChildItem "C:\temp" -Filter "*.xls"
foreach ($file in $files)
{
$command = "java.exe -classpath ""C:\PROGRA~2\my1.0.2\lib/*"" com.my.madsci "
$command = $command + "-v --collector-url https://api.fun.my.com/ "
$command = $command + "--oauth-url https://api.my.com/oauth/access "
$command = $command + "--type /fileName:string/discovery:string "
$command = $command + ("--key /fileName:""{0}/discovery:discovery"" " -f $file.BaseName)
$command = $command + ("""C:\proj\{0}_xls.txt""" -f $file.BaseName)
Invoke-Expression $command
}
Additionally, you can also schedule it from powershell itself (Works with PS 3.0 and above)
$trigger = New-JobTrigger -Daily -At "9:00 AM"
$scriptPath = "C:\FilePath\myScript.ps1"
Register-ScheduledJob -Name "MyJob" -FilePath $scriptPath -Trigger $trigger
I'm trying to execute unix commands thru a java program. Some of these commands involve an if-then-fi statement. Can this be done thru java / Runtime class? Seems like it only handles 1 command at a time.
I'm looking to do something like this:
grep 'Error One' SystemErr.log > $HOME/tempFiles/output.txt
grep 'Error Two' SystemErr.log >> $HOME/tempFiles/output.txt
grep 'Error Three' SystemErr.log >> $HOME/tempFiles/output.txt
.
.
if [ -s $HOME/tempFiles/output.txt ]
then
mail -s "Subject here" "a#b.com" < $HOME/tempFiles/output.txt
fi
Basically, I just want to email the file (results) if the grep found anything.
I want to use java instead of a direct shell script so that the errors I search for can be database-driven, easier to change.
I know I could read the file myself in java and search/parse it myself. But grep and other unix commands have a lot of built-in functionality I want to use to make it easier.
Any ideas, or am I totally on the wrong track?
Here is some code, using simpler commands, but basically equivalent:
public static void main( String[] args ) throws Exception {
try {
ProcessBuilder pb = new ProcessBuilder( "/bin/bash", "-c",
"echo one >/tmp/xxx && echo two >>/tmp/xxx && " +
"if [ -s /tmp/xxx ]; then cp /tmp/xxx /tmp/yyy; fi" );
File log = new File( "/tmp/log.txt" );
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
Process process = pb.start();
process.waitFor();
} catch( Exception e ){
// ...
} catch( Error e ){
// ...
}
}
The trick is to put it all into a single shell command so that you can call /bin/bash with the -c command option.
If composing this command is too complicated, write a shell file and source that.