系统shell命令 [英] system shell command

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

问题描述

您好。我正在编写一个从标准输入读取命令的命令shell。

此时我在std :: string中有命令。现在我想在shell中执行

这个命令。从Borland新闻组我了解到,

是stdlib.h中一个叫做system的函数。


int system(const char * command);


首先,问题是符合ANSI标准的系统命令。因为我包括

< cstdlib>并编写std :: system(command.c_str());它看起来像ANSI C ++

函数。但文档说它只在POSIX和Win32中,而不是在

ANSI C和ANSI C ++中。


其次,更重要的是,系统命令似乎没有响应

cd命令。调用std :: system(" cd ..")运行没有错误,但确实没有更改工作目录,因为可以通过调用
$ b $的getcwd来证明b dir.h在调用系统之前和之后。原因是系统

命令在一个新的子进程中运行,并且在那个进程中,b
目录可能会发生变化。毕竟,如果一个人运行系统(退出),一个

希望子进程退出,而不是父进程。


那怎么能我得到系统命令来更改父

进程的目录?我是否必须解析命令,并寻找cd xyz;并更改

目录为xyz使用dir.h的chdir?或者是否有某种方法可以创建

a系统对象作为局部变量,以便它可以知道它的状态(即

它的目录,环境变量等)?如果这是错误的

新闻组,有人可以告诉我正确的新闻组吗?


谢谢。


-

++++++++++++ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ div $

解决方案

" Siemel Naran" <硅********* @ REMOVE.att.net>写道:

#嗨。我正在编写一个从标准输入读取命令的命令shell。

#此时我在std :: string中有命令。现在我想在shell中执行

#this命令。从Borland新闻组我了解到那里

#是stdlib.h中一个叫做system的函数。



#int system(const char *命令);



#第一个问题,是系统命令ANSI兼容。因为我包括

#< cstdlib>并编写std :: system(command.c_str());它看起来像ANSI C ++

#函数。但文档说它只在POSIX和Win32中,而不是在
#ANSI C和ANSI C ++中。


system(...)是ANSI C,但其评估是系统特定的。请查看您的

系统文档。在unices上,系统函数调用shell来

评估命令;在unix上使用它有点像作弊(或者如果它试图使用你的shell来评估

字符串,那么
会导致无休止的递归)。


#其次,更重要的是,系统命令似乎没有响应

#cd命令。调用std :: system(" cd ..")运行没有错误,但确实没有更改工作目录,因为可以通过调用
$ b的getcwd来证明$ b#dir.h调用系统之前和之后。原因是系统


在unices上,cd只更改当前进程的目录; shell

解释cd ... string是一个单独的进程,其

目录中的更改不会影响评估

系统(...)函数的进程目录。 Unix shell直接在shell中识别和解释cd命令




if(strcmp(command," cd")== 0){

更改当前目录(休息(命令));

}否则{

系统(命令);

}


-

Derk Gwen http://derkgwen.250free.com/html/index.html

GERBILS

GERBILS

GERBILS


Siemel Naran写道:

嗨。我正在编写一个从标准输入读取命令的命令shell。
此时我在std :: string中有命令。


此时,您的帖子已成为comp.lang.c中的偏离主题。 std :: string

是C ++的一部分,而不是C.你发布到comp.lang.c ++是好的,但

你的问题是你的问题所在的新闻组不能是热门话题

表示您在发布之前既没有关注新闻组也没有查看常见问题解答

。这是一个罪。

现在我想在shell中执行这个命令。从Borland新闻组我了解到,
是stdlib.h中的一个叫做system的函数。

int system(const char * command);


由于您使用的是C ++,因此您希望使用C ++标头< cstdlib>而不是
C头< stdlib.h>。

第一个问题,系统命令是ANSI兼容的。




它是ISO(& ANSI)C和C ++的一部分。然而,_argument_与使用它的实现操作系统没有任何区别。

。它的用途是,即使是标准的,也不是便携式的。



-

Martin Ambuhl


2004年2月4日星期三08:02:27 GMT,Siemel Naran

< Si ********* @ REMOVE.att.net>写道:

嗨。我正在编写一个从标准输入读取命令的命令shell。
此时我在std :: string中有命令。现在我想在shell中执行
这个命令。从Borland新闻组我了解到
是stdlib.h中的一个函数叫做系统。

int system(const char * command);

第一个问题,是符合ANSI标准的系统命令。因为我包括
< cstdlib>并编写std :: system(command.c_str());它看起来像ANSI C ++
函数。但文档说它只在POSIX和Win32中,而不是在ANSI C和ANSI C ++中。

其次,更重要的是,系统命令似乎没有响应
cd命令。调用std :: system(" cd ..")运行时没有错误,但确实没有更改工作目录,这可以通过调用
dir.h的getcwd来证明之前和之后打电话到系统。原因是系统
命令在一个新的子进程中运行,并且在该进程内,
目录可能会发生变化。毕竟,如果一个人运行系统(退出),一个人希望子进程退出,而不是父进程。

那么如何才能获得系统命令来更改父母目录
进程?我是否必须解析命令,并寻找cd xyz;并将目录更改为xyz使用dir.h的chdir?或者是否有某种方法可以将系统对象创建为局部变量,以便它可以知道它的状态(即它的目录,环境变量等)?如果这是错误的
新闻组,有人可以让我知道正确的新闻组吗?



不,这个MSDOS小组中的第一个 - 也是最重要的 - 问题是

" system()如何在DOS和DOS模拟器中执行命令?

答案是它调用默认命令处理器(shell)和

将命令传递给它。如果你的程序是shell,那么它必须处理你支持的所有内部命令处理器命令,以及加载和启动任何外部可执行文件 - 如果

你想要使用CD,然后你的程序必须代表它并调用

必要的中断函数来完成底层任务。如果

%COMSPEC%指向你的程序,如果它是shell,那么system()将重新调用你的程序。

TED (td****@gearbox.maem.umr.edu)

垃圾邮件过滤器:发送到此地址的邮件*必须*包含TED

身体或他们将被自动拒绝。


Hi. I''m writing a command shell that reads commands from standard input.
At this point I have the command in a std::string. Now I want to execute
this command in the shell. From the Borland newsgroups I learned that there
is a function in stdlib.h called system.

int system(const char *command);

First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an ANSI C++
function. But the documentation says it''s in POSIX and Win32 only, not in
ANSI C and ANSI C++.

Second, and more important, the system command does not appear to respond to
the cd command. The call std::system("cd ..") runs without error, but does
not change the working directory, as can be evidenced by calling getcwd of
dir.h before and after the call to system. The reason is that the system
command runs within a new child process, and within that the process that
directory does probably change. After all, if one runs system("exit"), one
wants the child process to exit, not the parent process.

So how can I get the system command to change the directory of the parent
process? Do I have to parse the command, and look for "cd xyz" and change
the directory to "xyz" using chdir of dir.h? Or is there some way to create
a system object as a local variable, so that it can know it''s state (ie.
it''s directory, environment variables, etc)? If this is the wrong
newsgroup, can someone please let me know the right newsgroup?

Thanks.

--
+++++++++++
Siemel Naran

解决方案

"Siemel Naran" <Si*********@REMOVE.att.net> wrote:
# Hi. I''m writing a command shell that reads commands from standard input.
# At this point I have the command in a std::string. Now I want to execute
# this command in the shell. From the Borland newsgroups I learned that there
# is a function in stdlib.h called system.
#
# int system(const char *command);
#
# First question, is the system command ANSI compliant. Because I include
# <cstdlib> and write std::system(command.c_str()); it looks like an ANSI C++
# function. But the documentation says it''s in POSIX and Win32 only, not in
# ANSI C and ANSI C++.

system(...) is ANSI C, but its evaluation is system specific. So check your
system documentation. On unices, the system function calls a shell to
evaluate the command; using that on a unix is sort of like cheating (or
leads in to endless recursion if it tries to use your shell to evaluate the
string).

# Second, and more important, the system command does not appear to respond to
# the cd command. The call std::system("cd ..") runs without error, but does
# not change the working directory, as can be evidenced by calling getcwd of
# dir.h before and after the call to system. The reason is that the system

On unices, the cd only changes the directory of the current process; the shell
that interprets the "cd ..." string is a separate process and changes in its
directory do not affect the directory of the process evaluating the
system(...) function. Unix shells recognise and interpret the cd command
directly in the shell itself.

if (strcmp(command,"cd")==0) {
change current directory(rest(command));
}else {
system(command);
}

--
Derk Gwen http://derkgwen.250free.com/html/index.html
GERBILS
GERBILS
GERBILS


Siemel Naran wrote:

Hi. I''m writing a command shell that reads commands from standard input.
At this point I have the command in a std::string.
At this point, your post has become off-topic in comp.lang.c. std::string
is part of C++ and not C. It is good that you posted to comp.lang.c++, but
your crossposting to a newsgroup where your question cannot be topical
suggests that you have neither followed the newsgroups nor checked the FAQs
before posting. This is a sin.
Now I want to execute
this command in the shell. From the Borland newsgroups I learned that there
is a function in stdlib.h called system.

int system(const char *command);
Since you are using C++, you want to use the C++ header <cstdlib> and not
the C header <stdlib.h>.
First question, is the system command ANSI compliant.



It is part of ISO (&ANSI) C and C++. The _argument_ has no meaning apart
from the implementation, OS, in which you use it, however. Its use is,
even though standard, not portable.


--
Martin Ambuhl


On Wed, 04 Feb 2004 08:02:27 GMT, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote:

Hi. I''m writing a command shell that reads commands from standard input.
At this point I have the command in a std::string. Now I want to execute
this command in the shell. From the Borland newsgroups I learned that there
is a function in stdlib.h called system.

int system(const char *command);

First question, is the system command ANSI compliant. Because I include
<cstdlib> and write std::system(command.c_str()); it looks like an ANSI C++
function. But the documentation says it''s in POSIX and Win32 only, not in
ANSI C and ANSI C++.

Second, and more important, the system command does not appear to respond to
the cd command. The call std::system("cd ..") runs without error, but does
not change the working directory, as can be evidenced by calling getcwd of
dir.h before and after the call to system. The reason is that the system
command runs within a new child process, and within that the process that
directory does probably change. After all, if one runs system("exit"), one
wants the child process to exit, not the parent process.

So how can I get the system command to change the directory of the parent
process? Do I have to parse the command, and look for "cd xyz" and change
the directory to "xyz" using chdir of dir.h? Or is there some way to create
a system object as a local variable, so that it can know it''s state (ie.
it''s directory, environment variables, etc)? If this is the wrong
newsgroup, can someone please let me know the right newsgroup?


No, the first - and most important - question in this MSDOS group is
"how does system() execute commands in DOS and DOS emulators?". The
answer is that it invokes the default command processor (shell) and
passes it the command. If your program is to be the shell, then it
has to process all the internal command processor commands that you
support as well as loading and launching any external executables - if
you wnat to use CD, then your program must intrepret it and invoke the
necessary interrupt functons to accomplish the underlying tasks. If
%COMSPEC% points to your program, which it would have to if it were
the shell, then system() reinvokes your program recursively.
T.E.D. (td****@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.


这篇关于系统shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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