我正在编写一个脚本来为我的博客生成草稿帖子。运行ShellCheck后,我一直在看到此错误弹出。这是什么意思,有人可以提供一个例子?
SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
此外,我不确定要将$title
的值传递给邮政YAML中的"Title"
字段...
#!/bin/bash
# Set some variables
var site_path=~/Documents/Blog
drafts_path=~/Documents/Blog/_drafts
title="$title"
# Create the filename
title=$("$title" | "awk {print tolower($0)}")
filename="$title.markdown"
file_path="$drafts_path/$filename"
echo "File path: $file_path"
# Create the file, Add metadata fields
echo "---" > "$file_path"
{
echo "title: "$title""
} >> "$file_path"
echo "layout: post" >> "$file_path"
echo "tags: " >> "$file_path"
echo "---" >> "$file_path"
# Open the file in BBEdit
bbedit "$file_path"
exit 0
如果单击ShellCheck给出的消息,您将到达https://github.com/koalaman/koalaman/shellcheck/wiki/wiki/sc2129
您可以找到以下内容:
有问题的代码:
echo foo >> file date >> file cat stuff >> file
正确的代码:
{ echo foo date cat stuff } >> file
理由:
而不是在每行之后添加>>您可以 只需将相关命令分组并重定向组。
异常
这主要是一个风格问题,可以自由地忽略。
基本上替换:
echo "---" > "$file_path"
{
echo "title: "$title""
} >> "$file_path"
echo "layout: post" >> "$file_path"
echo "tags: " >> "$file_path"
echo "---" >> "$file_path"
with:
{
echo "---"
echo "title: "$title""
echo "layout: post"
echo "tags: "
echo "---"
} > "$file_path"
即使我建议您使用Heredoc:
cat >"$file_path" <<EOL
---
title: "$title"
layout: post
tags:
---
EOL