use attr linux command from Java program - java

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).

Related

Passing a space separated value via shell is taking more argument then expected

I'm having difficulties to startup a java program from a shell script (bash) where nested variables are used
export MAIN_CLASS="xxxxx"
MAIN_CLASS_ARGS=("$FirstArg" "$SEC_ARG" )
CMD="java some args here ${MAIN_CLASS} ${MAIN_CLASS_ARGS[#]}"
exec $CMD
And I am passing parameter as
export FirstArg = hello
export SEC_ARG ="hi Jam"
But In my main java class I have getting 3 parameter hello, hi ,Jam. But I am expecting it to be only two. What I am missing here can anyone help me.
I have checked some of the link as
link
But not able to fix it.
When you run exec $CMD, then word splitting is performed on the contents of $CMD. It doesn't matter how the variable was built up; at this point, it's just a string which is split by the shell.
Since you appear to be using a shell with support for arrays, then one option would be to do this instead:
CMD=( java some args here "${MAIN_CLASS}" "${MAIN_CLASS_ARGS[#]}" )
exec "${CMD[#]}"
That is, build up an array of all the arguments, then use a quoted array expansion, which prevents word splitting from taking place.

java reads "*" in args[0] as the .class file

I want some code in my program to run only if the user has input the character '*' at the command-line as a command-line argument. This is the code I've used:-
//myfile.java
import java.io.*;
public class myfile {
public static void main(String[] args) {
if(args[0].equals("*")){
//do stuff
System.out.println(args[0]);//added this line to see what exactly was being passed
}
}
}
When this program is executed at the command-line by entering:-
java myfile *
the output I'm expecting to see on the screen is the asterisk character, instead the output displayed is 'myfile.class'. Where am I going wrong? Why does Java change the asterisk to the .class file?
Also, note that the program worked perfectly the first four times I executed it and then started doing this!
Thanks in advance for your help.
Where am I going wrong?
The star character needs to be quoted or escaped. Run your java program like this:
java myfile "*"
or
java myfile \*
Why does Java change the asterisk to the .class file?
It doesn't. It is your shell that is doing it. It is shell file expansion ... or "globbing" as it is also called.
Run "ls *" or "echo *" and you will see that the same thing happens.
The command terminal already replaces the asterisk and java already gets the value that you see. I'd use any other character, that has no special meaning to the command terminal or otherwise you must escape the asterisk in your command.
Actually escaping arguments on Windows and especially in cmd.exe is non-trivial. This nice article explains it in detail: Everyone quotes command line arguments the wrong way :
the takaway for your case is: surround the asterisk with quotes.
Answer to your question in the comment:
Using the escape character worked! But I still don't get why it worked without the escape character the first few times
I am not sure, but maybe you run into this behavior: It makes a difference if the pattern can be expanded or not. For example, when I pass Test* as argument, then there are 2 cases to consider:
in the current folder there is a file called Test1.txt: then your java program will get Test1.txt as argument
when there are no matching files, your program will get Test* as argument
However, I am not sure, how this would apply to your case, since you only pass *: that should only work in an empty directory.

Run R function with string parameter from Shell, PHP or 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.

Is there any way to find out an argument passed to a jar was quoted?

I'm trying to pass a number of arguments to my Java application, but I would like to parse them by myself using an intelligent parser that doesn't just rely on whitespace to separate the arguments. An example:
/update source=foo func=(bar, foo ,foo,bar)
This all works nicely by converting everything to tokens and then parse those. However, a problem occurs when I add:
path="./foo/bar/foo bar.txt"
(note the double space between foo and bar).
When I use double quotes, the argument is passed as a single string, preserving the double space. The quotation marks are removed though like this:
path=./foo/bar/foo bar.txt
which makes my parser fail. But when I try to use some other character to use as quotes, like ', the parser works fine but then the shell passes the string as two separate strings, separated at the double space, therefore I lose the information that there were two spaces there.
What can I do to pass an argument using double quotes to keep the literal string representation, but also keep the information that the string was quoted, without the user having to type weird constructions like "'string'"? I'm using Java, maybe there is a way to get the entire line of arguments unparsed by the shell? Or just without the quotes being removed?
Btw, I ran this from microsoft command line, haven't tried a unix shell yet, which might even fail on the single quotes from what I read on the interwebs
On the Windows command line (using cmd.exe), you can escape double quotes with \". For example,
java MyApp path=\"./foo/bar/foo bar.txt\"
will result in
args[0] = path="./foo/bar/foo
args[1] = bar.txt"
while
java MyApp path="\"./foo/bar/foo bar.txt\""
will give you
args[0] = path="./foo/bar/foo bar.txt"
Thanks for the help I got, but I already figured it out:
I know the thing that could be quoted doesn't contain brackets, comma's or equals signs, the things that my parser recognizes.
I know that IF something was quoted and it contained spaces, those spaces would still exist within the split argument.
I know that the original string of arguments is split at every region of whitespace, so the final split arguments don't contain spaces, only those in the quoted parts.
Therefore I can assume that if I parse a split argument, that any space in there does not imply a new token has to be generated, therefore it is retained in the final string-token.
I just have to rewrite my tokenizer now to accept an array of arguments instead of the concatenated string I now create from the args array I get passed in my main() method. That way I can differentiate between skipping real whitespace (going into the next element of the array) and quoted whitespace (any other whitespace).

Quotes in command line arguments passed to Java main()

I run a Java program with the following command line (Edit: in NetBeans 6.8 project properties)
toto has:"tutu titi"
args is an array of 2 Strings
toto
has:tutu titi
I want (two arguments indeed, the second) args[1] to be
has:"tutu titi"
How should I do that?
Edit: I have already tried escaping the quotes with backslash from "Arguments" line in Netbeans propject properties, but I get args[1]
has:\tutu titi\
This really depends on your shell. You haven't said what operating system you're using. For example, on Windows this will work:
java Test toto "has:\"tutu titi\""
I believe the same thing will work in bash, too.
But if you're asking what you can do within Java to resolve this: nothing. The shell will have parsed the command line before the process was invoked, and you can't undo that parsing.
I had a similar problem in NetBeans and found the solution:
Edit/Add the property "application.args" in your private.properties to this:
application.args='has:""tutu titi""'
Single quotes to mark your "argument" and two double quotes to define one "double quotes".
Use
toto "has:\"tutu titi\""
If adding from NetBeans (7.1.2) Configuration/Arguments dialog field, a single-quote outer and escaped double quote inner worked for me e.g.:
my argument
This has been recognised by netbeans as a bug that won't be fixed!

Categories