I had this files
$img=1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg
Here is my code
# Split on the comma, and create an array
IFS=',' read -ra images <<< "$timg"
# Start the JSON
echo "\"pictures\":["
# loop through the images, and output the JSON
# keep track of the index of output items
counter=1
for image in "${images[@]}"
do
echo -n " {\"source\":\"$image\"}"
# Add a comma unless it is the last element in the array
if [ $counter -lt ${#images[@]} ]
then
echo ","
else
echo ""
fi
(( counter = counter + 1 ))
done
# Close the JSON
echo "]}"
The result is
{"source":"1.jpg"},
{"source":"2.jpg"},
{"source":"3.jpg"},
{"source":"4.jpg"},
{"source":"5.jpg"},
{"source":"6.jpg"},
{"source":"7.jpg"}
I want to give to for comand only read x numbers and stop. Its Posible ? Thx Example i want to run only first 5 The result desire is :
{"source":"1.jpg"},
{"source":"2.jpg"},
{"source":"3.jpg"},
{"source":"4.jpg"},
{"source":"5.jpg"}