Shellcheck 경고 SC2129는 무엇입니까? “{ cmd1; 사용을 고려하십시오. cmd2; } >> 개별 리디렉션 대신 파일을 사용하세요.” 평균?

Shellcheck 경고 SC2129는 무엇입니까? “{ cmd1; 사용을 고려하십시오. cmd2; } >> 개별 리디렉션 대신 파일을 사용하세요.” 평균?

shellcheck알 수 없는 경고 가 표시됩니다 .

In /mnt/e/bin/iconic line 540:
            printf "FALSE|" >> "$IconsRaw"           # Select field number 1
            ^-- SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.

나는 여기에서 우리 중 많은 사람들이쉘체크bash 스크립트/쉘 명령을 수정하여 질문이 주제에 관한 것이기를 바랍니다.


bash 스크립트의 관련 섹션을 게시하는 의견에 따르면:

    if [[ "$X" == "?" || "$Y" == "?" ]] ; then
        : # Bad X or Y offset usually "Link to Name.ext~" (backup name)
    else
        let i++
        printf "FALSE|" >> "$IconsRaw"           # Select field number 1
        printf "%s|" "$i" >> "$IconsRaw"         # 2
        printf "%s|" "${File##*/}" >> "$IconsRaw"
        printf "%s|" "$Linkless" >> "$IconsRaw"  # 4
        printf "%s|" "$Date" >> "$IconsRaw"      # 5
        printf "%s|" "$X" >> "$IconsRaw"         # 6
        echo   "$Y" >> "$IconsRaw"               # 7
    fi

해결책

shellcheck승인된 답변과 의견 덕분에 코드에서 오류를 포착할 뿐만 아니라 성능 개선도 제안한다는 사실을 알게 되었습니다 . 이 경우 파일 이름은 및 를 $IconsRaw사용하여 여러 번 열리고 닫혔습니다 .printfecho

보다 효율적인 bash 코드:

    # X,Y screen coordinates invalid on backup files ending with "~"
    ! [[ "$X" == "?" || "$Y" == "?" ]] && { let i++; echo \
        "FALSE|$i|${File##*/}|$Linkless|$Date|$X|$Y" >> "$IconsRaw"; }

답변1

귀하의 스크립트에 >> "$IconsRaw". 해당 메시지는 출력을 한 번만 리디렉션하고 하위 쉘에서 명령을 그룹화할 것을 제안합니다. 아마도 파일을 여러 번 열고 닫는 오버헤드를 피하기 위한 것 같습니다.

따라서 다음 대신에:

    printf "FALSE|" >> "$IconsRaw"           # Select field number 1
    printf "%s|" "$i" >> "$IconsRaw"         # 2
    printf "%s|" "${File##*/}" >> "$IconsRaw"
    printf "%s|" "$Linkless" >> "$IconsRaw"  # 4
    printf "%s|" "$Date" >> "$IconsRaw"      # 5
    printf "%s|" "$X" >> "$IconsRaw"         # 6
    echo   "$Y" >> "$IconsRaw"               # 7

이것:

{
    printf "FALSE|"            # Select field number 1
    printf "%s|" "$i"          # 2
    printf "%s|" "${File##*/}" 
    printf "%s|" "$Linkless"   # 4
    printf "%s|" "$Date"       # 5
    printf "%s|" "$X"          # 6
    printf "%s\n" "$Y"         # 7
} >> "$IconsRaw"

그러나 그것은 또한 불필요한 반복 printf이며 다음을 수행하는 것이 더 효율적입니다.

printf '%s|%s|%s|%s|%s|%s|%s\n' \
      'FALSE' "$i" "${File##*/}" "$Linkless" \
      "$Date" "$X" "$Y" >> "$IconsRaw"

관련 정보