我遇到了一些奇怪的错误。我正在研究一个查询引擎界面,用户应该在其中询问表单的查询 (Sub,Pred,Obj) 其中 Sub、Pred 和 Obj 是资源(如在 RDF 中)。如果用户想在给定 Pred 和 Obj 的情况下得到匹配的 Sub,那么他将查询 (?,Pred,Obj)。所以, ?指示用户希望获得什么作为输出(我们可以有多个?)。 现在在我的网页上,我已经尝试了所有 8 种组合,但只有 1 种组合无法显示任何输出。这种组合是(?,Something,?)。我有一个 bash 脚本,点击一个按钮,它会以正确的查询格式调用我的 C 代码(即我的查询引擎)。它将查询的输出存储在一个文件中。现在脚本中的另一个命令运行另一个 bash 命令,该命令创建一个网页,其中包含以无序列表形式的每一行输出。现在,当我提出有问题的查询(即 (?,Something,?))时,文件包含正确的输出,但 bash 脚本没有创建正确的网页。 em>
//BashScript (create_q_out_list.sh) to create the web-page from data
function createPage
{
filename='temp'
outfile='q_out_list.html'
echo "<html>" > $outfile
echo "<head>" >> $outfile
echo "<title>" >> $outfile
echo "List of Query Results" >> $outfile
echo "</title>" >> $outfile
echo "</head>" >> $outfile
echo "<body>" >> $outfile
echo "<ul>" >> $outfile
IFS=$'\n'
while read line #Loop to go through each relation in the file
do
echo "<li>$line</li>" >> $outfile
echo $line
done < $filename
echo "</ul>" >> $outfile
echo "</body>" >> $outfile
echo "</html>" >> $outfile
}
createPage $@
// JavaScript file that calls the above script and my querying engine to
// generate web-page and the file containing the output of the query respectively
var exec = require("child_process").exec;
function query(type, sub, pred, obj) {
if (type === "Search")
str = './search "<' + sub + "," + pred + "," + obj + '>" > temp';
if (type === "Create")
str = './create "<' + sub + "," + pred + "," + obj + '>" > temp';
if (type === "Delete")
str = './delete "<' + sub + "," + pred + "," + obj + '>" > temp';
exec(str, function(error, stdout, stderr) {
console.log("stdout:" + stdout);
console.log("stderr:" + stderr);
if (error != null) {
console.log("exec error: " + error);
}
});
exec("bash create_q_out_list.sh", function(error, stdout, stderr) {
console.log("stdout:" + stdout);
console.log("stderr:" + stderr);
if (error != null) {
console.log("exec error: " + error);
}
});
}
module.exports.query = query;
在上面的代码中,temp 是包含输出的文件。另外,q_out_list.html 是运行 create_q_out_list.sh 时应该生成的网页。 现在,除了我上面描述的那个之外,这两个脚本对于所有组合都运行良好。但是对于这种组合,查询引擎仍然会给出正确的输出,即临时文件包含正确的输出。但是生成的网页没有临时文件的任何信息。
最佳答案
在继续 bash
调用之前,您的 JavaScript 代码不会等待第一个 exec
完成。
有几种方法可以解决这个问题:
- 改用
execSync
– 这很简单,但会在您的子进程执行时阻塞 Node.js 进程 - 将第二个
exec()
调用放在第一个exec()
的回调中(下面的示例 1) - 使用
async
/await
和一个 promisifiedexec()
– 这更干净和更好(下面的示例 2 – 也有其他现代化) .
例子1
var exec = require("child_process").exec;
function query(type, sub, pred, obj) {
if (type === "Search")
str = './search "<' + sub + "," + pred + "," + obj + '>" > temp';
else if (type === "Create")
str = './create "<' + sub + "," + pred + "," + obj + '>" > temp';
else if (type === "Delete")
str = './delete "<' + sub + "," + pred + "," + obj + '>" > temp';
else throw new Error("Invalid type");
exec(str, function(error, stdout, stderr) {
console.log("stdout:" + stdout);
console.log("stderr:" + stderr);
if (error != null) {
console.log("exec error: " + error);
return;
}
exec("bash create_q_out_list.sh", function(error, stdout, stderr) {
console.log("stdout:" + stdout);
console.log("stderr:" + stderr);
if (error != null) {
console.log("exec error: " + error);
}
});
});
}
例子2
var exec = require('child_process').exec;
var promisify = require('util').promisify;
var execP = promisify(exec);
const typeToBinary = {
Search: './search',
Create: './create',
Delete: './delete',
};
async function query(type, sub, pred, obj) {
const binary = typeToBinary[type];
if (!binary) {
throw new Error(`Invalid type ${type}`);
}
var command = `${binary} "<${sub},${pred},${obj}>" > temp`;
const [stdout1, stderr1] = await execP(str);
const [stdout2, stderr2] = await execP('bash create_q_out_list.sh');
console.log(stdout1, stderr1, stdout2, stderr2);
}
关于javascript - 由于(可能)错误的 bash 脚本导致的一些奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56886976/