在 shell 脚本函数中创建 json 文件

问题描述 投票:0回答:1

我想在 shell 函数内创建一个简单的 json 文件。这是我的代码

gen_manifest () {
    FILENAME="abcd"
    cat > manifest.json << EOF
    {
        "general": {
            "name": "fs",
            "version": "1.0.0"
        },
        "install": {
            "type": "rootFs",
            "class": {
                "category": "partition",
                "image": "./${FILENAME}"
            }
        }
    }
    EOF
}

错误

Line 3:
    cat > manifest.json << EOF
    ^-- SC1009 (info): The mentioned syntax error was in this simple command.
                        ^-- SC1073 (error): Couldn't parse this here document. Fix to allow more checks.
 
Line 17:
    EOF
^-- SC1039 (error): Remove indentation before end token (or use <<- and indent with tabs).
 
Line 18:
}
 ^-- SC1072 (error): Here document was not correctly terminated. Fix any mentioned problems and try again.
shell
1个回答
0
投票

您可以在 shell 文档中使用 TAB 字符作为缩进;尽管您需要使用

<<-
来禁用它:

cat <<- EOF
    hello
        EOF

当我使用选项卡时,输出是:

hello
© www.soinside.com 2019 - 2024. All rights reserved.