简体   繁体   中英

Invalid escape sequence to run Linux command in JAVA

I need to execute this Linux command in Java using BufferedReader:

grep 'auth\.' /var/lib/iscsi/nodes/10.1.1.36/*/default 

What I was trying to execute is :

String inputThis = "";
String executeThis = "grep" + " " + "'auth\\.'" + " "
                        + "/var/lib/iscsi/nodes/10.1.1.36" + "/*/default";

Process process = ServerHelper.callProcessWithInput(executeThis, inputThis);

BufferedReader stdOutput = new BufferedReader(
        new InputStreamReader(process.getInputStream()));

try {
    logger.debug("stdOutput for editing:");

    String s = null;
    while ((s = stdOutput.readLine()) != null) {
        logger.info("####################" + s);
    }

} catch (IOException e) {
    logger.fatal(e);
}

It has no error warning: Invalid escape sequence (valid ones are \\b \\t \\n \\f \\r \\" \\' \\\\ )

But it works incorrect. I run the command in Linux terminal, it works fine and returned the desired results without error message. But when I execute the above code, the result is null. If you check the the error message,it says:

####################grep: /var/lib/iscsi/nodes/10.1.1.36/*/default: No such file or directory

Any idea how to modify the string?

Update: Figured it out! We cannot use "*" in the string, java cannot detect it.Just

executeThis = "ls" + " " + "/var/lib/iscsi/nodes/"+ 10.1.1.36;

to find what * is, and

executeThis = "cat" + " " + "/var/lib/iscsi/nodes/" + 10.1.1.36 + "/" + myString + "/default";

Try this:

exec(String[] cmdarray) Executes the specified command and arguments in a separate process.

process.exec("new String[]{"grep", "'auth\\.'", "/var/lib/iscsi/nodes/10.1.1.36/*/default"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM