简体   繁体   English

在外壳中调用C程序

[英]Invocation of C program in shell

In the K&R book there is a question as following: K&R书中,存在以下问题:

Write a program that converts uppercase to lower and vice versa, depending on the name it is invoked with? 编写一个程序,该程序根据调用的名称将大写转换为小写,反之亦然。

In the solution given, it says that when the program is invoked with the name lower , the program converts lower to upper . 在给出的解决方案中,它说,当使用名称lower调用该程序时,该程序将lower转换为upper In the solution program argv[0] is compared with "lower" but on Linux argv[0] will be "./lower" and not "lower" . 在解决方案程序中, argv[0]"lower"进行比较,但在Linux上argv[0]将为"./lower"而不是"lower"

Could someone clarify that please? 有人可以澄清一下吗?

Consider this test code: 考虑以下测试代码:

#include <stdio.h>
int main(int argc, char** argv) {
  printf("ARGV[0] = %s\n", argv[0]);
}

If I compile it as "test_argv", and run it, I get whatever I typed to run the program: 如果将其编译为“ test_argv”并运行它,我将得到键入运行该程序的所有信息:

% ./test_argv
ARGV[0] = ./test_argv
% test_argv
ARGV[0] = test_argv
% /tmp/test_argv
ARGV[0] = /tmp/test_argv

So you want to test the string, for example, against a regular expression, or for a substring. 因此,您想例如针对正则表达式或子字符串测试字符串。

How does Shell executes your program? Shell如何执行您的程序? (Very simple explanation below) (下面非常简单的解释)

shell()
{
  loader("Path name/Filename of your program", argv[0], argv[1]...for your program);
}

That means all those arguments to the loader, is made by the shell and then the loader is invoked and by default argv[0] is made the same as the Path name/File name of program. 这意味着所有这些加载程序的参数都由外壳程序生成,然后调用加载程序,并且默认情况下将argv [0]与程序的路径名/文件名相同。

Now when do say ./a.out ..it means absolute path name of your program is ./a.out.(The file a.out in the current directory) 现在当说./a.out ..表示程序的绝对路径名是./a.out。(当前目录中的文件a.out)

Can we omit ./? 我们可以省略./吗? The answer is yes, if the environmental variable PATH is having . 如果环境变量PATH具有,答案是肯定的。 as one of its component say the first component. 作为其组成部分之一,请说第一个组成部分。

You may see the content of PATH variable by typing echo $PATH in the shell prompt.(A : separated list of paths will be seen). 您可以通过在shell提示符下键入echo $ PATH来查看PATH变量的内容。(A:将看到分隔的路径列表)。

if . 如果。 is not there you may add that by typing export PATH=.:$PATH in the shell prompt. 不存在,您可以通过在shell提示符下键入export PATH =。:$ PATH来添加它。

After this you may run your code from the current directory by typing a.out and need not have to type ./a.out. 之后,您可以通过键入a.out在当前目录中运行代码,而不必键入./a.out。

Now I think this is clear that why argv[0] becomes the name of the file that you are executing? 现在,我认为这很清楚,为什么argv [0]成为您正在执行的文件的名称?

Now can we have a different name for argv[0] and not the filename ? 现在我们可以为argv [0]使用不同的名称,而不给文件名吗?

The answer is yes and then you need to call the loader yourself, that is write a small piece of code that behaves like shell. 答案是肯定的,然后您需要自己调用加载程序,即编写一小段类似于shell的代码。

You may browse the internet / man page for the execve family of functions, which is actually the loader referred here. 您可以浏览互联网/手册页中的execve系列功能,实际上是此处引用的加载程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM