命令行参数中包含空格 [英] command line arguements with spaces in them

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

问题描述

我有一个从命令行读入3个文件名的程序


prog filename1 filename2 filename3


然而,它没有''当其中一个文件名中有空格时工作

(由于目录名中有空格)因为该文件名得到

拆分为2.


我试过


prog" / blah / blah 2 / filename1" "文件名2" " filename3"


它仍然在空格上分裂。 (它也包括chars too。


这里有可能的快速解决方案吗?我意识到它可以通过手动检测b $ b来解决。并创建3

文件名。但是,如果有一种方法可以编写命令行,那么

就没有必要了,那就更好/更容易了。

I have a program which reads in 3 filenames from the command line

prog filename1 filename2 filename3

However, it doesn''t work when one of the filenames has spaces in it
(due to a directory name with a space in it) because that filename gets
split into 2.

I tried

prog "/blah/blah 2/filename1" "filename2" "filename3"

and it still splits on the space. (It included the " chars too).

Is there a quick fix that would be possible here? I realise that it
could be fixed by manually detecting the " and creating the 3
filenames. However, if there was a way to write the command line so
that isn''t necessary, that would be better/easier.

推荐答案

在文章< 11 ********************* @ s13g2000cwa.googlegroups中。 com>,

< ra ***** @ netscape.netwrote:
In article <11*********************@s13g2000cwa.googlegroups. com>,
<ra*****@netscape.netwrote:

>我有一个读入3的程序命令行中的文件名

prog filename1 filename2 filename3

然而,当其中一个文件名中有空格时它不起作用
(由于目录名称中有空格)因为该文件名被分成2个。

我试过

prog" / blah / blah 2 / filename1" "文件名2" " filename3"
>I have a program which reads in 3 filenames from the command line

prog filename1 filename2 filename3

However, it doesn''t work when one of the filenames has spaces in it
(due to a directory name with a space in it) because that filename gets
split into 2.

I tried

prog "/blah/blah 2/filename1" "filename2" "filename3"



在将

参数传递给程序之前,shell会解释引号。您所写的内容上面的
将适用于Unix下的大多数shell。或者,你可以用反斜杠来保护空间,如:


prog / blah / blah \ 2 / filename1 filename2 filename


这样的东西可能会或可能不会在其他系统上运行。

你可能想在新闻组专门询问你的问题

到你的系统。


-

Rouben Rostamian

The quotation marks are interpreted by the shell before the
arguments are passed to the program. What you have written
above will work on most shells under Unix. Alternatively,
you may protect the space by a backslash, as in:

prog /blah/blah\ 2/filename1 filename2 filename

Something like that may or may not work on other systems.
You may want to ask your question in a newsgroup dedicated
to your system.

--
Rouben Rostamian


ra ***** @ netscape.net 说:

我有一个读入3个文件名的程序从命令行


prog filename1 filename2 filename3


但是,当其中一个文件名中有空格时,它不起作用

(由于目录名称中有空格)因为该文件名得到

拆分为2.


我试过


prog" / blah / blah 2 / filename1" "文件名2" " filename3"


它仍然在空格上分裂。 (它也包括chars too。


这里有可能的快速解决方案吗?
I have a program which reads in 3 filenames from the command line

prog filename1 filename2 filename3

However, it doesn''t work when one of the filenames has spaces in it
(due to a directory name with a space in it) because that filename gets
split into 2.

I tried

prog "/blah/blah 2/filename1" "filename2" "filename3"

and it still splits on the space. (It included the " chars too).

Is there a quick fix that would be possible here?



是的。重命名文件,使其中没有空格。下一次

任何人都会在你的系统上的文件名中放一个空格,然后拍摄它们。它将为你节省很多麻烦。


-

Richard Heathfield

" ; Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

Yes. Rename the file so that it doesn''t have spaces in it. And the next time
anyone puts a space in a filename on your system, shoot them. It will save
you a lot of hassle.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


ra ***** @ netscape.net 写道:

我有一个读入3的程序命令行中的文件名


prog filename1 filename2 filename3


然而,当其中一个文件名中包含空格时,它不起作用

(由于目录名称中有空格)因为该文件名得到

拆分为2.


我试过


prog" / blah / blah 2 / filename1" "文件名2" " filename3"


它仍然在空格上分裂。 (它也包括chars)。
I have a program which reads in 3 filenames from the command line

prog filename1 filename2 filename3

However, it doesn''t work when one of the filenames has spaces in it
(due to a directory name with a space in it) because that filename gets
split into 2.

I tried

prog "/blah/blah 2/filename1" "filename2" "filename3"

and it still splits on the space. (It included the " chars too).



这很奇怪;你在上面给出的内容适用于大多数平台。


严格来说,提供程序参数的方式没有定义为C标准的
。 C标准不要求命令行有这样的
,或者参数用空格分隔。


< OT>

正如Rouben所说,在Unix平台上,由shell解析

命令行并分离各个参数。 Unix shell

通常支持双引号,单引号和退格

转义空格。


在Windows命令行中,支持双引号,并且在正确的转义行为中将导致




C:\ docs \ prog \ c> testargs" hello world" ; foo

argc = 3

argv [0] = testargs

argv [1] =你好世界

argv [2] = foo


但单引号不会被认为是特别的:


C:\ docs \ prog \ c> testargs''hello world''''foo''

argc = 4

argv [0] = testargs

argv [1] =''你好

argv [2] =世界''

argv [3] ='''foo''


< / OT>

That''s strange; what you have given above works on most platforms.

Strictly, the way in which program arguments are supplied is not defined
by the C standard. The C standard does not require that there is such a
thing as a command line, or that arguments are separated by spaces.

<OT>
As Rouben said, on Unix platforms it is up to the shell to parse the
command line and separate out the individual arguments. Unix shells
typically support double quotes, single quotes and also backspace
escaping of spaces.

On the Windows command line, double quotes are supported and will result
in the correct escaping behaviour:

C:\docs\prog\c>testargs "hello world" "foo"
argc = 3
argv[0] = testargs
argv[1] = hello world
argv[2] = foo

But single quotes will not be recognised as anything special:

C:\docs\prog\c>testargs ''hello world'' ''foo''
argc = 4
argv[0] = testargs
argv[1] = ''hello
argv[2] = world''
argv[3] = ''foo''

</OT>


这里有可能的快速解决方案吗?我意识到它可以通过手动检测b $ b来解决。并创建3

文件名。但是,如果有一种方法可以编写命令行,那么

就没有必要了,那就更好/更容易了。
Is there a quick fix that would be possible here? I realise that it
could be fixed by manually detecting the " and creating the 3
filenames. However, if there was a way to write the command line so
that isn''t necessary, that would be better/easier.



你必须做任何必要的事情才能使你的工作方式符合你平台上的b $ b。检测双引号听起来像是一个可能的选择。


-

Simon。

You''ll have to do whatever is necessary to make it work the way you want
on your platform. Detecting the double quotes sounds like a possible option.

--
Simon.


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

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