I would like a bash shell script (for my Debian 12 system) which accepts multiple paths as arguments, and then checks if a file can be successfully created in each one. Then the result should be put into json format.
The command to run should be:
./checkavailable.sh /the/first/path /the/second/path
(where there could be any number of input paths given, but at least one)
and I would like the output to be in the following format:
[
{ "is_available": 0, "path": "/the/first/path/" },
{ "is_available": 1, "path": "/the/second/path/" }
]
Currently, I managed to achieve this with the following script:
#!/bin/bash
# Check if first path is available
if touch $1/checkalivefile ; then
avail_path1=1
rm $1/checkalivefile
else
avail_path1=0
fi
# Check if second path is available
if touch $2/checkalivefile ; then
avail_path2=1
$2/checkalivefile
else
avail_path2=0
fi
echo "["
printf " { "is_available": "$avail_path1", "path": "$1" } "
printf ",n"
printf " { "is_available": "$avail_path2", "path": "$2" } "
echo
echo "]"
but it’s obviously a bit primitive and is hardcoded to work with x2 arguments only, and doesn’t scale to additional arguments.
I could try to add a loop now. However, I came across this script, which seems to do something similar (to get disk usage in this case) but using awk instead of a loop:
#!/bin/bash
echo "["
du -s -B1 "$@" | awk '{if (NR!=1) {printf ",n"};printf " { "dir_size_bytes": "$1", "path": ""$2"" }";}'
echo
echo "]"
Can someone show how this approach using "awk" could be used in my case? I am very new to bash scripts, so hope to learn something.
2
Answers
Note that
touch
can take multiple arguments, sotouch "$@"
would already perform that command on all paths provided to your script. Depending on whatis_available
actually stands for in your desired output, you could then just query if the files are actually present, are writeable, etc.Here’s an approach that still iterates over your script’s arguments using a
for
loop, appliestouch
individually, and checks its (negated) exit code$?
. These are then fed into a simple jq script that takes care of returning a valid JSON encoding, regardless of any strange characters in the paths provided.Note: Swallow stderr by using
touch … 2>/dev/null
to silence the error messages printed on failure.We have two operations … 1) run a sample
touch
and 2) pretty print some output. Running thetouch
from withinawk
is unnecessary overhead, and calling anawk
script to just pretty print what we can easily do inbash
is overkill (imo). So, I would opt to do the whole thing inbash
.General approach:
touch
a file then:rm
the file'['
is_available
line (preface with ending for previous line)']'
ending for previous line
takes care of printing the ending,
for all but the last lineOne
bash
idea:Setup:
Running the script: