这个错误是什么意思? (SC2129:考虑使用 { cmd1; cmd2; } >> 文件而不是单独的重定向。)
What does this error mean? (SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.)
我正在编写一个脚本来为我的博客生成 post 草稿。在 运行 ShellCheck 之后,我一直看到这个错误弹出。这是什么意思,有人可以举个例子吗?
SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
此外,我不确定我需要做什么才能将 $title
的值传递给 post 的 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([=10=])}")
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/shellcheck/wiki/SC2129
在那里您可以找到以下内容:
Problematic code:
echo foo >> file
date >> file
cat stuff >> file
Correct code:
{
echo foo
date
cat stuff
} >> file
Rationale:
Rather than adding >> something after every single line, you can
simply group the relevant commands and redirect the group.
Exceptions
This is mainly a stylistic issue, and can freely be ignored.
所以基本上替换:
echo "---" > "$file_path"
{
echo "title: \"$title\""
} >> "$file_path"
echo "layout: post" >> "$file_path"
echo "tags: " >> "$file_path"
echo "---" >> "$file_path"
与:
{
echo "---"
echo "title: \"$title\""
echo "layout: post"
echo "tags: "
echo "---"
} > "$file_path"
尽管我建议您使用 heredoc:
cat >"$file_path" <<EOL
---
title: "$title"
layout: post
tags:
---
EOL
我正在编写一个脚本来为我的博客生成 post 草稿。在 运行 ShellCheck 之后,我一直看到这个错误弹出。这是什么意思,有人可以举个例子吗?
SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
此外,我不确定我需要做什么才能将 $title
的值传递给 post 的 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([=10=])}")
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/shellcheck/wiki/SC2129
在那里您可以找到以下内容:
Problematic code:
echo foo >> file date >> file cat stuff >> file
Correct code:
{ echo foo date cat stuff } >> file
Rationale:
Rather than adding >> something after every single line, you can simply group the relevant commands and redirect the group.
Exceptions
This is mainly a stylistic issue, and can freely be ignored.
所以基本上替换:
echo "---" > "$file_path"
{
echo "title: \"$title\""
} >> "$file_path"
echo "layout: post" >> "$file_path"
echo "tags: " >> "$file_path"
echo "---" >> "$file_path"
与:
{
echo "---"
echo "title: \"$title\""
echo "layout: post"
echo "tags: "
echo "---"
} > "$file_path"
尽管我建议您使用 heredoc:
cat >"$file_path" <<EOL
---
title: "$title"
layout: post
tags:
---
EOL