Run R function with string parameter from Shell, PHP or Java - java

I have an R function with my_string parameter. In my case, parameter my_string is a very long json string, with many quotes.
my_function<-function(my_string) return(my_string)
When I run it from R:
>my_function('string"with"quotes')
[1] "string\"with\"quotes"
It works well. But when I try to run this function from Shell:
R -e "source('./my_function.R'); my_function('string"with"quotes')"
It fails with error, because shell can't deal with quotes.
I need to run this function directly with json string parameter.
If it is impossible to do this from Shell, maybe there are other ways to run R function from PHP or Java with string, that contains quotes?
P.S. I know, that in Shell I can escaping the double quotes with \", and it will work, but json string is very long, therefore I can't do it manually every time.

Related

Why getting argument when * doesnt work in Java?

I am new in Java and I want program to get as argument a different sign. For example:
java program #
And in program I convert string to char like this:
char c = args[0].charAt(0);
or like this:
char [] c = args[0].toCharArray();
But when I put *, it doesn't work. I print and get D or even name of file.
When you use * character in the command line, it becomes expanded - that's why you get a filename/list of filenames.
You may try escaping asterisk:
using backslash \ : java program \*
using double quotes " (which should also be escaped): java program \"*\"
However, in both cases you're likely to get the asterisk along with the escape character.
Other solutions to similar issue are shown here.

Unable to get the desired result if password contains dollar ($) character while running openssl passwd -apr1 command

I am running the following command to generate the hash for a given password on the Linux terminal.
openssl passwd -apr1 'Test123$Pwd'
$apr1$lWLP.Qc0$f/YdAVqsYFglu1EnLvzUS.
I have written a simple shell script that generates a hash for the given password. Here is my shell script: (Should I modify anything here in shell script to include single quotes surrounding $1)
echo -n "$1" | openssl passwd -apr1 $1
Now, the problem is, when I call the same shell script from java program, for some reason if the password contains a dollar($) character, it is not working as expected.
Here is my java code which calls the above shell script:
void test(String password){
String script = "sh test.sh %s";
String command = String.format(script, password);
- - -
execute(command); // execute using ProcessBuilder
- - -
}
The above code works fine if the password doesn't contain a dollar character. But, if the password contains $ char, it ignores the text after the $ symbol and generates a hash for the remaining password only up to $.
For example: If I pass the password as Test$Pwd
The hash is getting generated for only Test instead of Test$Pwd
I tried to set a single quote in java code as below but this also didn't work. When executed using java for some reason it still generates the hash for Test123 instead of Test123$Pwd
Modified java code to include a single quote as below:
"sh test.sh '%s'";
Observed the followed command in logs which has single quotes, and this also didn't work when I ran shell script from java.
openssl passwd -apr1 'Test123$Pwd'
From the Linux terminal when I run the script it works fine.
sh test.sh 'Test123$Pwd' -- this works
sh test.sh Test123$Pwd -- this doesn't work (generates hash only for Test123 even when run directly on the linux terminal)
I am confused about whether I need to modify the shell script or something in java?
Any pointers would be helpful. Thanks!
In shell scripts, double quotes is a way to get around spaces being seen as delimiters. It doesn't exclude variable substitution. So that's what you want here; single quotes would pass the literal $1 as in, the dollar, then the 1, and not 'substitute the first argument of the script here'. However, you pass $1 straight up without quotes for the second.
But, this is putting rather a lot of dependency on the exact shell in use and opens up doors to passwords with certain symbols messing up your program.
Why not forego the shell script entirely and call openssl directly? You can replace the standard in (and thus, send what echo is sending in your script), and the args passed via ProcessBuilder's List<String> / String... options (never use the single string option, really, it's hard to read and is asking java to attempt to turn spaces into separators, bad idea here) - as now you're passing the raw options without bash or whatever shell you have attempting to 'interpret' that password which you don't want it to do.
Or, better yet, why are you invoking a shell script?
apr1 is, if memory serves, the crypt key indicating 'MD5, done 1000 times'. This is a HORRIBLE, in that it is trivially rainbow tabled and broken, way of hashing passwords. It's also something you don't need openssl for at all, you can easily do that from java.
So, here's my advice:
Reconsider apr1. It's insecure as heck, you don't want it. Use bcrypt, scrypt, or pbkdf2. Probably bcrypt.
Stop farming this job out via an exec, do it in java. For example, use jBCrypt.

how to pass commandline argument include "|" to java program

One of the arguments passed to my java program is like this, ab|cd. Initially, when I run the java program, like this,
$ java className ab|cd
it fails, since | is interpreted in the linux shell as a pipe symbol. So only ab is passed into java program. So I made a second attempt:
$ java className "ab|cd"
This time, "ab|cd" is passed in, but the value includes the double quotes. What the program is really intended to have is ab|cd. How can I pass in the correct value without the quotes?
In the command shell, you can escape out characters using the '\' character.
java className ab\|cd
Try (for Linux):
$ java className ab\|cd
For Windows:
java className ab^|cd
Try this,
"\" is used inorder to nullify the effect of the characters which have special meanings.
java className ab\|cd
Maybe try this:
arg="ab|cd"
java className $arg

egrep from Java fails while in shell succeed

I run egrep using Java Runtime.exec()
String command = "egrep \'(Success|Loading\\.\\.\\.|Loaded : READY|Found a running instance)\' "+ instance.getPath() + "/log";
Runtime.getRuntime().exec(command);
The stdout is always null and stderr shows "egrep: Unmatched ( or (". but when I copy the command to shell and run, it returns the correct value.
The solution is pretty simple: (Success|Loading\\.\\.\\.|Loaded is not a valid regex.
You can't protect white space with quotes when using Process.exec(String). Always use the versions of exec() that take an array or, even better, use ProcessBuilder.
That way, you can pass each argument as a single Java String and spaces and other special characters won't create any problems.
The single quotes should not be escaped. You don't escape them on the command line, either, do you?

use attr linux command from Java program

I want to attach meta data to a file in Unix file system.
attr command lets me do that but
the command syntax requires the path of the attached variable to be in double qoutes.
attr -s outpipe0 "/mnt/FUse/FileB" FileA
how can i Use System.Runtime.exec in java to run the above command. When ever i try to run using a string array argument I have to give the above "/mnt/FUse/FileB" which causes problem in java program as it considers the double quotes as end of string in java. I basically want to send a string argument which in itself has double quotes.
Can someone suggest a work around .
Thanks
You can escape the quotes within your literal string in Java, like this:
"\"/mnt/FUse/FileB\""
That will address your question of how to include double quotes in a string, but I doubt it will solve your program. That's because I doubt the attr program actually wants (or accepts) double quotes. Instead, the shell eats them. For example, if the command you type in the shell is the one you mentioned, the double quotes will be consumed by the shell before the arguments are passed to attr. So I have doubts that you need the double quotes at all (but if you do, see above).

Categories