C 语言中的 Shell
我想用C语言在Linux操作系统下写一个shell。
该项目中使用的库和函数的名称是什么?
谢谢。
I want to write a shell in the Linux operating system with the C language.
What is the name of the libraries and functions to use in this project?
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能至少需要:
单一 UNIX 规范手册页 是所需任何附加功能的良好资源。
You will probably want, at the very least:
The Single UNIX Specification Man Pages are a good resource for any additional functionality needed.
我不明白你的问题。您正在开始一项新事物,您可以使用任何您想要的名称。项目的概念通常特定于某些 IDE。
要编写 shell,首先必须熟悉 C 编程语言,并了解几个重要的 Linux 系统调用(例如
fork
、execve
、pipe、
chdir
、dup
等)。因此,首先阅读一本关于这些的好教科书。系统调用可通过标准 C 库使用,您无需链接额外的库。也许,研究小型 shell(例如 sash)的源代码会对您有很大帮助。
I don't understand your question. You are starting a new thing, you can use whatever name you want. And the notion of Project is usually specific to some IDEs.
To code a shell, you first must know well the C programming language, and understand well several important Linux system calls (like
fork
,execve
,pipe
,chdir
,dup
etc.). So read a good textbook on these first. The system calls are available thru the standard C library, you don't need to link an extra one.And probably, studying the source code of small shells (like sash) would help you a lot.
实际上,这是一个有趣的问题,对于了解 Unix 的 IO 机制来说,这可能是一个很好的练习。您可能知道,Shell 负责解释输入、调用系统调用并显示输出。该程序本身运行在内核之上。因此,该练习可能会提供有关进程执行系列的大量信息,例如
exec*
。这是 一个教程,用 C 语言为您介绍它,这个 one in Python 将帮助您设计一个快速原型以供理解。
Actually, that's an interesting question and it could be a good exercise for something to know the IO mechanism of Unix. As you might know, Shell is responsible for interpreting Input , invoking the system calls and displaying the output. The program as such runs on top of the kernel. So, the exercise might give a lot of info on process execution family such as
exec*
.Here is one tutorial covering it for you in C and this one in Python would help you design a quick prototype for understanding.
不幸的是,您无法为此进行一些神奇的
emulateShell()
API 调用,您将需要(深入深入)研究诸如exec
、fork
、popen
、signal/sigaction
以及许多其他流程控制内容。此外,您可能需要参与终端处理等,例如
fcntl
和ioctl
。这么大的一个答案框,确实不是一个可以全面涵盖的主题。我建议尝试将其分解为较小的工作,以便使您(和我们)的工作变得更轻松。
也许从一个简单的程序开始,该程序接受用户的命令并将其分解为令牌以供执行。这将是良好的第一步。
Unfortunately, there isn't some magical
emulateShell()
API call you can make for this, you are going to need to look into (in great depth) things likeexec
,fork
,popen
,signal/sigaction
and a host of other process control things.In addition, you'll probably need to get involved with terminal handling and so forth, things like
fcntl
andioctl
.It's not really a subject that can be covered comprehensively is an answer box this size. I would suggest trying to break it down in smaller jobs so as to make your (and our) job easier.
Perhaps start with a simple program that accepts commands from the user and breaks them down into tokens for execution. That would be a good first step.