编写外壳-如何执行命令 [英] Writing a shell - how to execute commands

查看:83
本文介绍了编写外壳-如何执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个最终将利用并发性的shell.现在,我有一个运行中的Shell解析器,但是在弄清楚如何执行命令时遇到了麻烦.我看了一下exec(execvp等),它看起来很有希望,但是我有一些疑问.

I'm trying to write a shell that will eventually take advantage of concurrency. Right now I've got a working shell parser but I'm having trouble figuring out how to execute commands. I've looked a bit at exec (execvp etc.) and it looks promising but I have some questions.

exec可以处理文件输入/输出重定向吗?我可以使用exec设置管道吗?

Can exec handle file input/output redirection? Can I set up pipes using exec?

我也想知道子壳.子外壳应该返回什么?最后一条语句的退出状态?子壳可以成为管道的一部分吗?

I'm also wondering about subshells. What should subshells return; the exit status of its last statement? Can subshells be a part of a pipe?

这些问题看起来似乎很愚蠢,但请您耐心等待.

These might seem like really dumb questions but please bear with my inexperience.

推荐答案

exec可以处理文件输入/输出重定向吗?

Can exec handle file input/output redirection?

不,您可以使用open()dup()dup2()(和close())进行此操作.

No, you do that with open() and dup() or dup2() (and close()).

我可以使用exec设置管道吗?

Can I set up pipes using exec?

不,您可以通过pipe()dup()dup2()以及许多close()调用来做到这一点.

No, you do that with pipe(), dup() or dup2() and lots of close() calls.

我也想知道子壳.子外壳应该返回什么,最后一个语句的退出状态?

I'm also wondering about subshells. What should subshells return, the exit status of its last statement?

这是正常的惯例,是的.

That's the normal convention, yes.

子壳可以成为管道的一部分吗?

Can subshells be a part of a pipe?

是的.在普通的shell中,您可以编写如下内容:

Yes. In a normal shell, you can write something like:

(cd /some/where; find . -name '*.png') | sed 's/xyz/prq/' > mapped.namelist


如果您想害怕,可以调查 posix_spawn() 及其支持功能.在 POSIX 2008 网站上搜索"spawn",并准备好害怕.我认为,临时进行映射工作实际上比使用posix_spawn()及其支持者将其编纂更为容易.


If you want to get scared, you could investigate posix_spawn() and its support functions. Search for 'spawn' at the POSIX 2008 site, and be prepared to be scared. I think it is actually easier to do the mapping work ad hoc than to codify it using posix_spawn() and its supporters.

这篇关于编写外壳-如何执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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