简体   繁体   English

从 Deno 运行 bash 脚本

[英]Running bash script from Deno

Let's say I've got this super useful and advanced bash script:假设我有这个超级有用和高级的 bash 脚本:

#!/usr/bin/env bash

echo What is your name?
read name
echo What is your age?
read age

When I try to run it from Deno with a simple script like this:当我尝试使用这样的简单脚本从 Deno 运行它时:

const process = Deno.run({
  cmd: [`./bash.sh`],
  stdin: "piped",
  stdout: "piped",
});

const decoder = new TextDecoder();

const output = await process.output()
const parsed = decoder.decode(output);

console.log(parsed);

It returns nothing, but if I simplify the Deno script to the first line of the bash script it returns the output just fine它什么也不返回,但是如果我将 Deno 脚本简化为 bash 脚本的第一行,它会返回 output 就好了

const process = Deno.run({
  cmd: [`echo`, `What is your name?`],
  stdin: "piped",
  stdout: "piped",
});

const decoder = new TextDecoder();

const output = await process.output()
const parsed = decoder.decode(output);

console.log(parsed);

Why is this?为什么是这样? I'd assume since the start of the bash file and the single line command both start with echo it would return the same result twice我假设自从 bash 文件和单行命令都以 echo 开头以来,它将返回相同的结果两次

Version 1.5 of denoadded the prompt function which allows you to completely remove the need for shelling out to a different program and handling inter-process communication via stdin/stdout. deno 1.5 版添加了prompt function,它允许您完全消除对不同程序和通过 stdin/stdout 处理进程间通信的需要。

let name: string | null = null;
let age: string | null = null;

while (name === null) {
  name = prompt("What is your name?");
}

while (age === null) {
  age = prompt("What is your age?");
}

console.log(`you are ${name}, ${age}yo`);

Your code is telling Deno to set up the subprocess to expect piped stdin -- but never providing it any content on stdin, Consequently, it hangs in the very first read .您的代码告诉 Deno 设置子进程以期望管道标准输入 - 但从未在标准输入上提供任何内容,因此,它在第一次read时挂起。

If we take that out (letting stdin be passed through from the parent process), and do in fact answer the two prompts on the parent process's stdin, everything works perfectly:如果我们把它拿出来(让标准输入从父进程传递),并且实际上回答了父进程标准输入上的两个提示,一切都会完美运行:

deno run --allow-run run-bash.js <<'EOF'
A Nony Mouse
3
EOF

...with run-bash.js containing: ...使用run-bash.js包含:

const process = Deno.run({
  cmd: [`./bash.sh`],
  stdout: "piped",
});

const decoder = new TextDecoder();

const output = await process.output()
const parsed = decoder.decode(output);

console.log(parsed);

...and your bash.sh unchanged. ...和您的bash.sh不变。 output thus captures the two prompts ( What is your name? and What is your age? ), and forwards them to the javascript interpreter's stdout as-requested. output因此捕获两个提示( What is your name?What is your age? ),并按要求将它们转发到 javascript 解释器的标准输出。

You have to call bash to call your script ( of course with --allow-run option ) like:您必须调用bash来调用您的脚本(当然使用--allow-run选项),例如:

const process = Deno.run({
  cmd: ["bash","bash.sh"],
  stdin: "piped",
  stdout: "piped",
});

const decoder = new TextDecoder();

const output = await process.output()
const parsed = decoder.decode(output);

console.log(parsed);

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

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