命令行参数的问题,其中包含空格 [英] Issue with Command Line arguments which got spaces in it

查看:113
本文介绍了命令行参数的问题,其中包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,经过大量的调试,我找到了理由的原因。
我有一个Java程序,我通过bash脚本在Linux环境中执行。

I was struck in a weird problem , for which i found out the cause of reason , after lot of debugging . I have a Java program which i am executing in Linux environment through a bash script .

这是我的简单bash脚本,它接受一个String。

This is my simple bash script , which accepts a String .

#!/bin/bash
java -cp  com.QuoteTester $1

问题是命令行参数可以是Spaces还是没有空格。
例如它可以是

The issue is that the command line argument can be with Spaces or Without spaces . For example it can be either

Apple Inc. 2013 Jul 05 395.00 Call   

OR

Apple

根据我的代码,就是这样

As per my code it is this way

public static void main(String[] args) 
{
String symbol = args[0];

if (symbol.trim().contains(" ")) // Option
{

}

else  // Stock 
{

}

所以问题是,当我试图以这种方式执行时

So the issue is that , when i am trying to execute it this way

./ quotetester Apple Inc. 2013年7月05日395.00致电

它唯一总是转到 else条件即股票。

its only always going to the else condition that is Stock .

无论如何我可以解决这个问题吗?

Is there anyway i can resolve this ??

推荐答案

当你传递带空格的命令行参数时,它们被视为空格分隔的参数,并且是分裂在太空上。所以,你实际上没有一个参数,而是多个参数。

When you pass command line arguments with spaces, they are taken as space separated arguments, and are splitted on space. So, you don't actually have a single argument, but multiple arguments.

如果你想用空格传递参数,请使用引号:

If you want to pass arguments with spaces, use quotes:

java classname "Apple Inc. 2013 Jul 05 395.00 Call"

这篇关于命令行参数的问题,其中包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆