简体   繁体   中英

How to Pass Quotes “” in a shell argument to java with argument containing white spaces

I am currently trying to execute following command

./launch.sh -version 212 -file "/root/Desktop/local test/list.csv"

The launch.sh assigns the arguments to a variable MY_VARIABLE

When the value is assigned then the quotes around the file path is removed and echo $MY_VARIABLE gives following result:

./launch.sh -version 212 -file /root/Desktop/local test/list.csv

Now launch.sh calls for Java and gives MY_VARIABLE as argument

But Java splits the argument for the file and try to process /root/Desktop/local and end up with an error while it should be taking /root/Desktop/local test/list.csv .

How can I ignore the white spaces in the Java argument or keep the quotes while assigning the value to the variable to make sure that I give the correct arguments.

您可以尝试添加两个转义的引号(\\“),如下所示:

"\"/root/Desktop/local test/list.csv\""

There are somany ways you can implement this. add escape sequence as Steafan said.or you can store this strings into a text file and read those values to a string from file

How can I ignore the white spaces in the Java argument

  public class Arguments {
      public static void main(String[] args){ 

            String together = "";
            for(int i=0; i < args.length; i++){
                System.out.println( args[i] );
                together = together.concat(args[i]);
            }
            System.out.println(together);

            for(int i=0; i < args.length; i++){
                System.out.println( args[i] );
            }
        }  
}

modifying that code you can concatenate any number of arguments you want. After that you can use together as a single variable

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