I'm stuck on a bash problem. The script should read each line in data.txt and assign it to an array before checking if each array value is empty. If the array value is empty, the text "unknown" should replace the empty value. Line 2 would otherwise have the text $Author.
data.txt
07/22/2022, 00:00:00
NASA Reveals Webb Telescope's first images of unseen universe
"We are elated to celebrate this extraordinary day with the world," said Greg Robinson, Webb program director at NASA Headquarters.
webb, telescopes, images, reveals, galaxies, star, unseen, nasa, universe, nebula, webbs, gas, view, space
bashscript.sh
# assign each line in data.txt to array
readarray data < data.txt
# for each value in array
for i in "${data[@]}"; do
# check if value is null
if [ -z "$data[i]" ]; then
# if value is null, assign text "unknown" to value
data[i]="unknown"
fi
done
Author=${data[1]}
echo "author: $Author"
ouput:
author:
expected output:
author: unknown
Thanks for reading/helping....
"${!data[@]}"to get indexes instead of values.ithat can be used as array indexes