ruby 如何清除system()的输出列表?

ffvjumwh  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(115)

有人告诉我,system()的输出存储在一个临时文件中。
当我运行该命令时,我收到了这个错误,而导致它的是system()

let content_type = 'Content-Type: application/x-www-form-urlencoded'
let accept = 'Accept: application/json'
let url = g:languagetool_server_root . '/v2/check'
Calling shell to execute: "(curl -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" -d "language='en-US'&enabledOnly=false&data='%7B%22annotations%22%3A
%5B%7B%22markup%22%3A%22%3Ch1%3E%22%7D%2C%7B%22text%22%3A%22Hello+world%21%22%7D%2C%7B%22markup%22%3A%22%3C%2Fh1%3E%22%7D%2C%7B%22markup%22%3A%22%3Cp%3E%22%7D%2C%7B%22text%22%3A%22Style+and+grammer+checke
r%22%7D%2C%7B%22markup%22%3A%22%3C%2Fp%3E%22%7D%5D%7D'"'http://localhost:8081/v2/check'")>/var/folders/yh/z3ydby_56qz7mltrddsv3lqw0000gn/T/vzsapcl/2 2>&1"
Error detected while processing function languagetool#CheckGrammar:
line   12:
E282: Cannot read from "/var/folders/yh/z3ydby_56qz7mltrddsv3lqw0000gn/T/vzsapcl/2"

字符串
我刚刚注意到,错误提供的路径被附加到system()。这很奇怪。下面是运行shell命令的代码:

let content_type = 'Content-Type: application/x-www-form-urlencoded'
let accept = 'Accept: application/json'
let url = g:languagetool_server_root . '/v2/check'
let response = system('curl -X POST --header ' . shellescape(content_type) . ' --header ' . shellescape(accept) . ' -d language=' . shellescape(g:languagetool_default) . '&enabledOnly=false&data=' . shellescape(json) . shellescape(url))


到目前为止,我已经尝试了:clearhistdel(),但我被告知histdel()system()输出的临时文件无关。

bjp0bcyl

bjp0bcyl1#

成功了!我重新构造了curl命令,并将头和主体(包括要传递的数据)分离为变量。这是引号的问题。shellescape()用单引号将变量的值括起来。
这是一个很好的例子。

let content_type = 'Content-Type: application/x-www-form-urlencoded'
let accept = 'Accept: application/json'
let data = 'language=' . g:languagetool_default . '&enabledOnly=false&data=' . json
let url = g:languagetool_server_root . '/v2/check'
let response = system('curl -s -X POST --header ' . shellescape(content_type) . ' --header ' . shellescape(accept) . ' -d ' . shellescape(data) . ' ' . shellescape(url))

字符串
我仍然不明白为什么system()加载了一个不存在的临时文件。它肯定不是curl,因为它不将输出存储在客户端的任何位置。
我说得太早了。它在重建的curl命令中再次执行此操作。我猜不是引号的问题导致了临时文件的问题。
我的错。这和这个问题无关。

相关问题