How to loop each JSON files on bash script to know if content of the file in empty or not empty? - Stack Overflow

admin2025-04-22  2

Here's my bash script on looping the JSON files to know if content of the file in empty or not empty. Tried to test this script on my terminal and I'm not getting exactly what I want, my goal is to loop for each JSON files and determine if the JSON file is empty or not empty.

#!/bin/bash

file_json=$(find /inputs/resource/*/*.json | awk -F'/' '{ print $(NF) }' | sort -n)

echo "Checking JSON file content."
for file in "$file_json"; do
    echo "$file"
    if [[ ! -s "file" ]]; then
        echo "JSON file is empty."
        exit
    fi
        echo "JSON file is not empty."
done

Here's the output from my terminal, it only proceeds on "JSON file is empty." even my JSON file has contents inside, and I also notice that it is not looping for each JSON files to check if the JSON file is empty or not empty, where did my bash script go wrong?

Checking JSON file content.
xpm1.json
xpm2.json
xpm3.json
JSON file is empty.

Here's my bash script on looping the JSON files to know if content of the file in empty or not empty. Tried to test this script on my terminal and I'm not getting exactly what I want, my goal is to loop for each JSON files and determine if the JSON file is empty or not empty.

#!/bin/bash

file_json=$(find /inputs/resource/*/*.json | awk -F'/' '{ print $(NF) }' | sort -n)

echo "Checking JSON file content."
for file in "$file_json"; do
    echo "$file"
    if [[ ! -s "file" ]]; then
        echo "JSON file is empty."
        exit
    fi
        echo "JSON file is not empty."
done

Here's the output from my terminal, it only proceeds on "JSON file is empty." even my JSON file has contents inside, and I also notice that it is not looping for each JSON files to check if the JSON file is empty or not empty, where did my bash script go wrong?

Checking JSON file content.
xpm1.json
xpm2.json
xpm3.json
JSON file is empty.
Share Improve this question edited Jan 21 at 19:37 John Kugelman 363k69 gold badges553 silver badges597 bronze badges asked Jan 21 at 14:49 生きがい生きがい 859 bronze badges 5
  • hint: enable debug mode (#!/bin/bash -x), run the script, review the output; of particular interest will be the contents of the file variable for each pass through the loop; alternatively, inside the loop add typeset -p file (to display the contents of the variable) – markp-fuso Commented Jan 21 at 15:10
  • You are missing an else statement – Paolo Commented Jan 21 at 15:12
  • 1 What does an "empty" JSON file look like? Is it a real empty file (0 bytes) or does it contain something like []? (If the true empty case is the 0-byte case, then we don't need to use JSON-aware tools at all). – Charles Duffy Commented Jan 21 at 15:26
  • 1 This is a typo: You're running [[ ! -s "file" ]] but you want to run [[ ! -s "$file" ]] (or [[ ! -s $file ]], which is 100% identical: [[ suppresses globbing and word-splitting so you don't need to use quotes for the same thing). – Charles Duffy Commented Jan 21 at 15:34
  • 3 If you are simply looking for empty (as in "0 bytes") files, just use find directly: find /inputs/resource/*/ -type f -name '*.json' -empty – knittl Commented Jan 21 at 16:11
Add a comment  | 

2 Answers 2

Reset to default 3

There are several issues with your script:

  • your use of find is quite strange because after pathname expansion by the shell /inputs/resource/*/*.json becomes the list of files of interest, so find is useless,
  • an else statement is apparently missing,
  • the exit statement terminates the script, which is probably not what you want,
  • for file in "$file_json"; do iterates only once on the content of variable file_json considered as one single word,
  • in if [[ ! -s "file" ]]; then you should have $file, not just file,
  • you use awk to suppress the directory part but then you cannot test the file size with just the base name.

The consequence of all this is that you iterate only once with only one file value (with 3 lines in it):

xpm1.json
xpm2.json
xpm3.json

This single value is printed, the if [[ ! -s "file" ]]; then test succeeds because there is no such file, so the JSON file is empty. message is printed and the script exits.

But you don't need all this. Let's assume that "empty" means "0 byte"; using just find you can try:

find /inputs/resource -maxdepth 2 -mindepth 2 -type f \
  -name '*.json` -printf '%f\nJSON file is ' \
  \( -size +0c -printf 'not ' -o -true \) -printf 'empty.\n'

If you prefer pure bash:

#!/bin/bash

echo "Checking JSON file content."
for file in /inputs/resource/*/*.json; do
    echo "${file##*/}"
    if [[ ! -s "$file" ]]; then
        echo "JSON file is empty."
    else
        echo "JSON file is not empty."
    fi
done

Try this:

#!/bin/bash

echo "Checking JSON file content."
for file in $(find /inputs/resource/*/*.json | sort -n); do
    if [ -s $file ]; then
        echo "File '$file' is not empty."
    else
        echo "File '$file' is empty."
    fi
done
转载请注明原文地址:http://anycun.com/QandA/1745302703a90600.html