Capture stdout of shell script

I am building a "Transform & Replace Selection with JavaScript" action. I have been trying to capture the output of a shell script that does IO. The script in question is a pretty widespread Python CLI interface to remote and local LLMs (GitHub - simonw/llm: Access large language models from the command-line).

async (content) => {
   let cmd = `/path/to/llm  "${content}"`;
   let resp = await runShellScript({script: cmd});
   return(resp);
}

But the output doesn't get captured. Probably because of IO delay. Indeed, the following does work:

async (content) => {
   let cmd = `echo "${content}"`;
   let resp = await runShellScript({script: cmd});
   return(resp);
}

When I run llm --help as the command, the help message get captured (no IO involved) too. But when I run a query, as shown above, no output get captured. (Needless to say, it works fine in the terminal).

Any idea? (Is there a way to specify a timeout, for instance?)

I found a solution: piping the query to the llm command rather than passing it as an argument (both work in the terminal).

async (content) => {
   let cmd = `echo "${content}" | /path/to/llm`;
   let resp = await runShellScript({script: cmd});
returnToBTT(resp);
};

Thanks for BTT by the way (I bought it two times. It is, by far, the best helper tool on OS X that I have ever used. Quicksilver comes second).

1 Like