[英]How to display “help” command in linux terminal using C?
I need a little help on simulating a terminal command within a C program. 在模拟C程序中的终端命令时,我需要一些帮助。 More specifically the "help" command.
更具体地说,“帮助”命令。
Just to clarify what's going on here. 只是为了澄清这里发生的事情。 I'm working on an assignment to build a C program under the Linux environment that will prompt the user to enter a command (user will type an actual Linux command within the C program), the program will read the user input, and output the result of the command as if you were typing the command in the terminal.
我正在研究在Linux环境下构建C程序的任务,该程序将提示用户输入命令(用户将在C程序中键入实际的Linux命令),程序将读取用户输入,并输出命令的结果,就像您在终端中键入命令一样。
I'm new to working in both Linux and C, but I think I can build the main program. 我对使用Linux和C都是新手,但是我认为我可以构建主程序。 I just want to learn how to run Linux commands within a C program.
我只想学习如何在C程序中运行Linux命令。 I researched and have successfully done so with a few commands, such as "clear" and "ls", but I have not been able to get "help" or even "exit" to work.
我已经使用一些命令(例如“ clear”和“ ls”)进行了研究并成功完成了此操作,但是我无法获得“帮助”甚至“退出”工作。
Here's how I got "ls" to work: 这是我使“ ls”工作的方式:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("ls");
return 0;
}
I complied and ran that program and it did exactly what's expected, run the command as if I was typing in the terminal. 我编译并运行了该程序,它的运行完全符合预期,就像在终端中键入命令一样运行命令。 Now I tried the same thing for "help", but it didn't work in saying the command was "not found."
现在,我为“帮助”尝试了相同的操作,但是说命令“未找到”并没有用。 So I searched it up and found that the shell looks in particular directories to execute certain stuff.
因此,我对其进行了搜索,发现该外壳在特定目录中查找以执行某些内容。 "Ls" in particular was in /usr/bin.
特别是“ Ls”位于/ usr / bin中。 The problem is, I can't seem to find the directory where "help" is stored.
问题是,我似乎找不到存储“帮助”的目录。 Am I in the right direction in looking for a particular directory or is it not possible to run "help" within a C program?
我在寻找特定目录时是否朝着正确的方向前进,还是无法在C程序中运行“帮助”?
Any help is greatly appreciated! 任何帮助是极大的赞赏!
help
is a bash builtin, not a real program you can run. help
是内置的bash,不是可以运行的真实程序。
system(3)
uses your system's shell interpreter ( /bin/sh
) to run commands, which happens to have no help
builtin on your system. system(3)
使用系统的Shell解释器( /bin/sh
)运行命令,而这些命令恰好在系统上没有内置help
。
Use 采用
system("bash -c help");
instead of just "help". 而不只是“帮助”。 It seems that your default shell isn't bash but something else.
看来您的默认外壳不是bash,而是其他东西。 "bash -c" simply specifies a command specifically to bash (-c option means "command").
“ bash -c”仅指定专门用于bash的命令(-c选项表示“命令”)。 You can check this via command line by looking at where /bin/sh is pointing.
您可以通过命令行查看/ bin / sh指向的位置来进行检查。
ls -l /bin/sh
should give something to the tune of 应该给一些东西
lrwxrwxrwx 1 root root 4 Sep 9 19:30 /bin/sh -> bash*
or something else 或者是其他东西
As system(3)
doc says, it uses /bin/sh
for executing your command line. 正如
system(3)
doc所说,它使用/bin/sh
执行命令行。 /bin/sh
is not the same as /bin/bash
anyway. /bin/sh
始终与/bin/bash
。 Even if you have only bash(1)
in your system, when you run it as sh
, it behaves as if you where using plain sh
(runs in compatibility mode), so no help
command available. 即使您的系统中只有
bash(1)
,当您以sh
身份运行它时,它的行为也就好像您在使用普通sh
(以兼容模式运行)一样,因此没有可用的help
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.