I would like to execute the Linux command "pwd" through a C language function like execv().
我想通过像execv()这样的C语言函数执行Linux命令“pwd”。
The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.
问题是没有一个名为“pwd”的可执行文件,我无法执行“echo $ PWD”,因为echo也是一个没有可执行文件的内置命令。
3 个解决方案
#1
8
You should execute sh -c echo $PWD
; generally sh -c
will execute shell commands.
你应该执行sh -c echo $ PWD;通常sh -c将执行shell命令。
(In fact, system(foo)
is defined as execl("sh", "sh", "-c", foo, NULL)
and thus works for shell built-ins.)
(事实上,system(foo)被定义为execl(“sh”,“sh”,“ - c”,foo,NULL),因此适用于shell内置函数。)
If you just want the value of PWD
, use getenv
, though.
如果您只想要PWD的值,请使用getenv。
#2
17
If you just want to execute the shell command in your c program, you could use,
如果你只想在你的c程序中执行shell命令,你可以使用,
#include <stdlib.h>
int system(const char *command);
In your case,
在你的情况下,
system("pwd");
The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.
问题是没有一个名为“pwd”的可执行文件,我无法执行“echo $ PWD”,因为echo也是一个没有可执行文件的内置命令。
What do you mean by this? You should be able to find the mentioned packages in /bin/
这是什么意思?你应该能够在/ bin /中找到提到的包
sudo find / -executable -name pwd
sudo find / -executable -name echo
#3
5
You can use the excecl command
您可以使用excecl命令
int execl(const char *path, const char *arg, ...);
Like shown here
如此处所示
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
int main (void) {
return execl ("/bin/pwd", "pwd", NULL);
}
The second argument will be the name of the process as it will appear in the process table.
第二个参数将是进程表中显示的进程名称。
Alternatively, you can use the getcwd() function to get the current working directory:
或者,您可以使用getcwd()函数来获取当前工作目录:
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#define MAX 255
int main (void) {
char wd[MAX];
wd[MAX-1] = '\0';
if(getcwd(wd, MAX-1) == NULL) {
printf ("Can not get current working directory\n");
}
else {
printf("%s\n", wd);
}
return 0;
}
#1
8
You should execute sh -c echo $PWD
; generally sh -c
will execute shell commands.
你应该执行sh -c echo $ PWD;通常sh -c将执行shell命令。
(In fact, system(foo)
is defined as execl("sh", "sh", "-c", foo, NULL)
and thus works for shell built-ins.)
(事实上,system(foo)被定义为execl(“sh”,“sh”,“ - c”,foo,NULL),因此适用于shell内置函数。)
If you just want the value of PWD
, use getenv
, though.
如果您只想要PWD的值,请使用getenv。
#2
17
If you just want to execute the shell command in your c program, you could use,
如果你只想在你的c程序中执行shell命令,你可以使用,
#include <stdlib.h>
int system(const char *command);
In your case,
在你的情况下,
system("pwd");
The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.
问题是没有一个名为“pwd”的可执行文件,我无法执行“echo $ PWD”,因为echo也是一个没有可执行文件的内置命令。
What do you mean by this? You should be able to find the mentioned packages in /bin/
这是什么意思?你应该能够在/ bin /中找到提到的包
sudo find / -executable -name pwd
sudo find / -executable -name echo
#3
5
You can use the excecl command
您可以使用excecl命令
int execl(const char *path, const char *arg, ...);
Like shown here
如此处所示
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
int main (void) {
return execl ("/bin/pwd", "pwd", NULL);
}
The second argument will be the name of the process as it will appear in the process table.
第二个参数将是进程表中显示的进程名称。
Alternatively, you can use the getcwd() function to get the current working directory:
或者,您可以使用getcwd()函数来获取当前工作目录:
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#define MAX 255
int main (void) {
char wd[MAX];
wd[MAX-1] = '\0';
if(getcwd(wd, MAX-1) == NULL) {
printf ("Can not get current working directory\n");
}
else {
printf("%s\n", wd);
}
return 0;
}