在 nodejs 中生成参数中带有空格的 unix 命令
Spawn in nodejs for a unix command with spaces in parameters
我想在 Debian 系统上使用 nodejs Spawn 执行以下命令:
/usr/bin/apt-get upgrade -s | tail -1 | cut -f1 -d' '
我想使用 spawn 而不是 exec 因为将来只使用 root 命令而且我不想允许完全 shell 访问(我将使用正确的命令更新 visudo 文件)
这是我的代码
const apt = spawn('/usr/bin/apt-get', ['upgrade', '-s']);
const tail = spawn('tail', ['-1']);
const cut = spawn('cut', ['-f1', '-d" "']);
apt.stdout.on('data', (data) => {
tail.stdin.write(data);
});
tail.stdout.on('data', (data) => {
cut.stdin.write(data);
});
cut.stdout.on('data', (data) => {
console.log(data.toString());
});
apt.stderr.on('data', (data) => {
console.log("apt stderr: ${data}");
});
tail.stderr.on('data', (data) => {
console.log("tail stderr: ${data}");
});
cut.stderr.on('data', (data) => {
console.log("cut stderr: ${data}");
});
apt.on('close', (code) => {
if (code !== 0) {
console.log("apt process exited with code ${code}");
}
});
tail.on('close', (code) => {
if (code !== 0) {
console.log("tail process exited with code ${code}");
}
});
cut.on('close', (code) => {
if (code !== 0) {
console.log("cut process exited with code ${code}");
}
});
res.status(200).json('');
执行后出现错误,因为无法识别“-d”“”参数。我尝试用双 \ 转义 space 或在两者中拆分参数但仍然错误
应该是:
const cut = spawn('cut', ['-f1', '-d ']);
没有双引号 或 反斜杠转义 -- 这些是用于 的 shell,而不是 cut
,这里没有shell。
这使得处理未知文件名(对于您未来的用例)特别容易:当您的字符串作为参数传递给不滥用它们的软件时,我的 运行 eval
-等价物稍后编写代码),您无需引用、转义、清理或以其他方式修改它们即可将它们作为数据传递。
(也就是说——当您告诉 shell cut -f1 -d" "
时,它调用以启动 cut
进程的实际系统调用在 C 语法中看起来像 execve("/usr/bin/cut", {"cut", "-f1", "-d ", NULL}, environ)
; 引号是句法的,当 shell 使用它们来决定 -d
之后的 space 应该是同一文字参数的一部分时,它会使用这些引号)。 =17=]
我想在 Debian 系统上使用 nodejs Spawn 执行以下命令:
/usr/bin/apt-get upgrade -s | tail -1 | cut -f1 -d' '
我想使用 spawn 而不是 exec 因为将来只使用 root 命令而且我不想允许完全 shell 访问(我将使用正确的命令更新 visudo 文件)
这是我的代码
const apt = spawn('/usr/bin/apt-get', ['upgrade', '-s']);
const tail = spawn('tail', ['-1']);
const cut = spawn('cut', ['-f1', '-d" "']);
apt.stdout.on('data', (data) => {
tail.stdin.write(data);
});
tail.stdout.on('data', (data) => {
cut.stdin.write(data);
});
cut.stdout.on('data', (data) => {
console.log(data.toString());
});
apt.stderr.on('data', (data) => {
console.log("apt stderr: ${data}");
});
tail.stderr.on('data', (data) => {
console.log("tail stderr: ${data}");
});
cut.stderr.on('data', (data) => {
console.log("cut stderr: ${data}");
});
apt.on('close', (code) => {
if (code !== 0) {
console.log("apt process exited with code ${code}");
}
});
tail.on('close', (code) => {
if (code !== 0) {
console.log("tail process exited with code ${code}");
}
});
cut.on('close', (code) => {
if (code !== 0) {
console.log("cut process exited with code ${code}");
}
});
res.status(200).json('');
执行后出现错误,因为无法识别“-d”“”参数。我尝试用双 \ 转义 space 或在两者中拆分参数但仍然错误
应该是:
const cut = spawn('cut', ['-f1', '-d ']);
没有双引号 或 反斜杠转义 -- 这些是用于 的 shell,而不是 cut
,这里没有shell。
这使得处理未知文件名(对于您未来的用例)特别容易:当您的字符串作为参数传递给不滥用它们的软件时,我的 运行 eval
-等价物稍后编写代码),您无需引用、转义、清理或以其他方式修改它们即可将它们作为数据传递。
(也就是说——当您告诉 shell cut -f1 -d" "
时,它调用以启动 cut
进程的实际系统调用在 C 语法中看起来像 execve("/usr/bin/cut", {"cut", "-f1", "-d ", NULL}, environ)
; 引号是句法的,当 shell 使用它们来决定 -d
之后的 space 应该是同一文字参数的一部分时,它会使用这些引号)。 =17=]