我想执行任何 bash 命令。我发现Command::new但我无法执行“复杂”命令,例如ls ; sleep 1; ls. 此外,即使我将其放入 bash 脚本中并执行它,我也只会在脚本末尾得到结果(正如流程文档中所解释的那样)。我希望在命令打印后立即得到结果(并且也能够读取输入),就像我们在 bash 中一样。
execlp与我们一起运行命令
execlp("ps", "ps", NULL);
Run Code Online (Sandbox Code Playgroud)
这里可以看到冗余,因为我们通过了ps两次。此行为在所有exec变体中都是一致的。
为什么 exec 需要这样的冗余?为什么不这样写,以便我们可以简单地
execlp("ps", NULL);
Run Code Online (Sandbox Code Playgroud) 假设我们在全局命名空间中定义了一些可用的类。在示例中:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Vector:
def __init__(self, alpha, r):
self.x = r * cos(alpha)
self.y = r * sin(alpha)
# and many others...
Run Code Online (Sandbox Code Playgroud)
这个怎么做:
class_name = 'Point'
x = 14.361
y = -8.100
code_str = 'class_object = ' + class_name + '(' + str(x) + ', ' + str(y) + ')'
exec code_str # That evaluates to: "class_object = Point(14.361, -8.100)"
print class_object.x, class_object.y
Run Code Online (Sandbox Code Playgroud)
不使用危险的执行程序?
附言。如果有人询问,我打算从某些 txt 或 json 文件加载数据。
我在 python 脚本中运行 python 脚本时遇到问题,我根本不明白:
假设我们在同一目录中有 2 个文件:'init.py' 和 'text.py'
初始化.py:
X = 5
print("init.py was run")
Run Code Online (Sandbox Code Playgroud)
测试.py:
exec(open("./init.py").read())
print("X = %s" %X)
Run Code Online (Sandbox Code Playgroud)
如果我现在运行 test.py,我会得到
init.py 已运行
X = 5
但是,如果我将 test.py 更改为:
def func_call( filename):
exec(open(filename).read())
print("X = %s" %X)
func_call("./init.py")
Run Code Online (Sandbox Code Playgroud)
我得到:
init.py 已运行
回溯(最近一次调用最后一次):
文件“test.py”,第 5 行,位于
Run Code Online (Sandbox Code Playgroud)func_call("./init.py")文件“test.py”,第 3 行,在 func_call 中
Run Code Online (Sandbox Code Playgroud)print("X = %s" %X)名称错误:名称“X”未定义
有人可以向我解释为什么这会导致不同的结果吗?有解决方法吗?我的目标是通过运行 python 脚本并访问该 python 脚本中设置的变量来初始化大部分变量。
我想看看本地是否存在一个文件,如下所示:
if (exec(`-f ~/.config/myApp/bookmarks.json`)) {
console.log('exists')
} else {
console.log('does not')
}
Run Code Online (Sandbox Code Playgroud)
但是,无论文件是否存在,我都会在控制台中得到存在
我想让我的主进程等待执行程序的一些输出。
我想强制程序按顺序执行这些步骤,等待每个步骤然后再进入下一步。我觉得这应该很容易,但我不知道该怎么做。
对于其他语言,默认情况下会等待,例如在 Perl 中,以下 5 行将完全满足我的要求。
print("START")
print(`sleep 3; echo 1`)
print(`sleep 2; echo 2`)
print(`sleep 1; echo 3`)
print("END")
Run Code Online (Sandbox Code Playgroud)
如何在 Node JavaScript 中做到这一点?
到目前为止,这是我的代码
const util = require('util');
const exec = require('child_process').exec;
function sleepy1() {
exec("sleep 3; echo 1", function( stderr, stdout ) {
console.log( stdout.trim() )
})
}
function sleepy2() {
exec("sleep 2; echo 2", function( stderr, stdout ) {
console.log( stdout.trim() )
})
}
function sleepy3() {
exec("sleep 1; echo 3", function( stderr, stdout ) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 SOCKS5 代理克隆 github 存储库。在~/.ssh/config我有:
Host github.com *.github.com
ProxyCommand /usr/bin/nc -X 5 -x 127.0.0.1:7070 %h %p
Run Code Online (Sandbox Code Playgroud)
git clone失败并出现错误bash: No such file or directory:
$ git clone git@github.com:aureliojargas/sedsed.git
Cloning into 'sedsed'...
bash: No such file or directory
kex_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)
我手动尝试了该ssh命令,但也失败了:
$ ssh -v git@github.com
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration …Run Code Online (Sandbox Code Playgroud) 几天我就提出了一个问题.我的解决方案符合接受的答案中的建议.但是,我的一个朋友提出了以下解决方案:
请注意,代码已更新几次(查看编辑修订版)以反映下面答案中的建议.如果您打算给出新的答案,请考虑这个新代码,而不是那些有很多问题的旧代码.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int fd[2], i, aux, std0, std1;
do {
std0 = dup(0); // backup stdin
std1 = dup(1); // backup stdout
// let's pretend I'm reading commands here in a shell prompt
READ_COMMAND_FROM_PROMPT();
for(i=1; i<argc; i++) {
// do we have a previous command?
if(i > 1) {
dup2(aux, 0);
close(aux);
}
// do we have a next command?
if(i < argc-1) { …Run Code Online (Sandbox Code Playgroud) 我使用PHP查询Oracle数据库,但在某个时刻,可能由于PHP代码非常复杂,我得到了内存泄漏.我正在努力解决这个问题,但我问你:如果我将所有复杂的PHP代码转换为C代码,并在需要时从PHP调用"exec",我是否在性能和内存优化方面获得了一些成果?或者这是一个坏主意?为什么?
基于unix的系统中命令exec(const char*c)和system(const char*c)之间的区别是什么?
两者都可以从C程序调用来执行系统调用.这两个有没有区别?