ITADN

Shell code clean up jq script

#1148Openjbampton 创建于 2025-12-06
shell
J
jbamptoncommented
![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg) The current implementation first checks if the JSON is valid with `jq empty` and then formats it with `jq .`. This results in processing the same file twice with `jq`, which is inefficient. You can combine these steps into one by directly attempting to format the file. The `jq .` command will fail for invalid JSON, so you can use its exit code to determine if formatting was successful. Additionally, when `jq` fails after a stream redirection (`>`), it can leave behind an empty temporary file. It's good practice to explicitly clean this up on failure. ```suggestion echo "Attempting to beautify $file with jq..." if jq . "$file" > "${file}.pretty" 2>/dev/null; then mv "${file}.pretty" "$file" echo "Beautification of $file complete." else rm -f "${file}.pretty" echo "$file contains invalid JSON. Skipping beautification." fi ``` _Originally posted by @gemini-code-assist[bot] in https://github.com/SalamLang/Salam/pull/1147#discussion_r2595249079_
0 条评论