What does this error mean? (SC2129: consider using the file {cmd1; cmd2;} >> instead of separate redirects.)

I am writing a script to create draft posts for my blog. After starting ShellCheck , I see that this error appears. What does this mean and can anyone give an example?

SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.

Also, I'm not sure what I need to do to pass the value of $title to the "Title" field in the YAML column ...

 #!/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 
+6
source share
1 answer

If you press the message given by shellcheck, you will arrive at https://github.com/koalaman/shellcheck/wiki/SC2129

Here you can find the following:

Problem code:

 echo foo >> file date >> file cat stuff >> file 

The correct code is:

 { echo foo date cat stuff } >> file 

Justification:

Instead of adding → something after each line, you can simply group the appropriate commands and redirect the group.

Exceptions

This is basically a stylistic issue and can be freely ignored.

So basically replace:

 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" 

Even if I suggested you use heredoc :

 cat >"$file_path" <<EOL --- title: "$title" layout: post tags: --- EOL 
+7
source

Source: https://habr.com/ru/post/987065/


All Articles