我正在制作简单的 ANSI C 程序,模拟 Unix shell。因此,我使用 fork() 创建子进程,并在子进程内部调用 exec() 来运行给定的(由用户)程序。
我需要做的是将文件内容重定向到标准输入,以便将其发送给用户调用的程序。
Example: cat < file \\user wants run cat and redirect content of that file to it by typing this to my program prompt
我正在尝试这样做:
...child process...
int fd = open(path_to_file, O_RDONLY);
int read_size = 0;
while ((read_size = read(fd, buffer, BUF_SIZE)) != 0) {
write(STDIN_FILENO, buffer, read_size);
}
close(fd);
execlp("cat", ...);
Run Code Online (Sandbox Code Playgroud)
一切都很顺利,文件内容被写入标准输入,但在读取整个文件后,cat 仍在等待输入(我需要告诉 cat,输入结束),但我不知道如何:-(?
有任何想法吗?多谢!!!
想象一下我的服务器上有一个 script.sh 要求输入您的姓名。您运行该脚本,它会显示:
what is your name?
Run Code Online (Sandbox Code Playgroud)
然后你输入你的名字,它会打印:
hello name!
Run Code Online (Sandbox Code Playgroud)
是否可以使用 php exec 运行这样的脚本?那就是执行脚本并以某种方式回答它可能有的不同问题?
希望我说清楚了。我真正想做的是从我们的开发服务器自动执行“git pull”命令,但它一直要求输入用户名/密码。我知道我可以使用 ssh 证书登录来避免这种情况,但我的问题仍然存在。我可以使用 exec() 并回答脚本中的未来问题吗?
我想从 c 中的 main 函数在后台运行一个子进程。我已经使用 fork 和 execv 函数来执行此操作。但我也想在父进程结束时杀死子后台进程,以防子进程尚未退出。我将使用kill(pChildPid)函数来执行此操作。所以我的问题是
假设子进程先于父进程退出,Linux操作系统可以将与子进程相同的pid分配给其他进程吗?如果是,那么我会无意中杀死该进程?
如何从命令行使用 gpg 加密给定的字符串?我将公钥存储在一个名为 pubkey.pub 的文件中,我想我可以简单地用类似的东西来做到这一点。
gpg --import "path/to/pubkey.pub" --encrypt "my string to encrypt"
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。
背景:我必须使用 PHP exec 命令来加密给定的文本,因为我没有在服务器上安装 PHP 模块本身。
我有一些名为file1.php、file2.php、file3.php的 PHP 文件。现在,我想从另一个名为all.php的 PHP 文件中一一运行这些 PHP 文件。我的意思是 file2.php 将在 file1.php 完成后执行。file3.php将在file2.php完成后执行。怎么做 ?
我可以使用 exec 函数吗?我的托管安全吗?我在共享主机中使用 Cpanel。有什么办法可以做到这一点,但对我的托管内容来说是安全的?
非常感谢 !
我有以下场景:
if { [catch { exec echo calling a BIG script } exec_msg] } {
puts "NOK"
puts "output of the BIG script: $exec_msg"
} else {
puts "OK"
puts "output of the BIG script: $exec_msg"
}
Run Code Online (Sandbox Code Playgroud)
是否有任何解决方案可以实时打印 $exec_msg ,与 BIG 脚本的执行同步?
我想使用 GO 在 Windows 10 上运行异步命令。我正在运行的命令是阻塞的,如果直接从终端运行,它会提供源源不断的状态消息,直到 ctrl-c
我想通过 exec 从 GO 运行该命令,并将 exec 命令的输出实时捕获到终端,即不仅在 GO 应用程序终止时。
我尝试了许多示例但没有成功,我只是得到一个空白终端,即使在退出 GO 应用程序后,我也看不到我执行的命令的输出。
我想subprocess.run在 Golang 中复制以下函数调用。什么是正确的方法呢?
subprocess.run(['kinit', username], input=password.encode())
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经弄清楚如何使用exec.Command来运行外部命令,但让我感到困惑的是将字符串作为STDIN该命令的输入传递。Pythonsubprocess.run有一个方便的input参数来处理这个问题,我如何在 Golang 中获得类似的结果?
我尝试使用 node.js(docker exec 命令)按顺序执行长进程。
我愿意:
const childProcess = require('child_process');
const execWithPromise = async command => {
return new Promise(async resolve => {
const process = childProcess.exec(command);
process.on('exit', err => resolve(err));
process.on('close', err => resolve(err));
});
};
const run = async () => {
await execWithPromise('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
await execWithPromise('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
};
run();
Run Code Online (Sandbox Code Playgroud)
但是承诺立即得到解决,结果为 1。在这两种情况下。该命令在命令行上运行得很好。
为什么会立即返回?
我有一些代码使用 fork、execlp 和 wait 来创建两个进程。目标是能够重复打印提示并让用户输入命令,该命令最多包含 4 个参数/选项。
int main()
{
string command, argument;
istringstream iss(argument);
do
{
//prompt user for command to run
cout << "Enter command: ";
cin >> command >> argument;
int pid, rs, status;
//fork will make 2 processes
pid = fork();
if(pid == -1) { perror("fork"); exit(EXIT_FAILURE); }
if(pid == 0) {
//Child process: exec to command with argument
//C_str to get the character string of a string
rs = execlp(command.c_str(), command.c_str(), argument.c_str(), (char*) NULL);
. …Run Code Online (Sandbox Code Playgroud)