【问题标题】:z shell issue, running an alias and getting this " zsh: parse error near `}' "z shell 问题,运行别名并得到这个“zsh: parse error near `}'”
【发布时间】:2018-07-25 05:47:03
【问题描述】:

我正在尝试在z shell (5.5.1) 中创建一个别名,它将node.js 服务器代码写入文件app.js。当我运行别名时,我得到了这个

"zsh: `}' 附近的解析错误"

我试图避开大括号,但没有成功。我已经搜索和搜索,但找不到任何东西来澄清我做错了什么。

alias srvr='echo const express = require("express");
const app = express();

app.use(express.static("public"));
app.use(express.static("vendors"));

 app.get("/", function (req, res) {
   res.sendFile(__dirname + "/index.html");
 });

 app.listen(3000, function () {
   console.log("Example app listening on port 3000!");
 });' > app.js

【问题讨论】:

    标签: node.js ubuntu command-line alias zsh


    【解决方案1】:

    不带引号的分号后的任何内容都不会被视为echo 的参数。无论如何,您不应该为此使用别名。请改用函数。

    srvr () {
        echo 'const express = require("express");
    const app = express();
    
    app.use(express.static("public"));
    app.use(express.static("vendors"));
    
     app.get("/", function (req, res) {
       res.sendFile(__dirname + "/index.html");
     });
    
     app.listen(3000, function () {
       console.log("Example app listening on port 3000!");
     });' > app.js
    }
    

    【讨论】:

    • 长得吓人的单引号字符串仍然难以阅读并且容易出错(很容易在包含单引号的内容中意外编辑)。也许改用here-document?
    猜你喜欢
    • 2022-06-21
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多