-2

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"}
Jess
  • 21
  • 4

3 Answers3

3

This is almost trivial using jq:

$ img=1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg
$ echo "$img" | jq -R --argjson n 5 'split(",")[:$n] | {pictures: map({source: .})}'
{
  "pictures": [
    {
      "source": "1.jpg"
    },
    {
      "source": "2.jpg"
    },
    {
      "source": "3.jpg"
    },
    {
      "source": "4.jpg"
    },
    {
      "source": "5.jpg"
    }
  ]
}
chepner
  • 389,128
  • 51
  • 403
  • 529
2

You can make a slice of the array, and loop over that.

images=("${images[0]:0:5}")

See How to slice an array in Bash

Barmar
  • 596,455
  • 48
  • 393
  • 495
1

Consider the following:

#!/bin/bash

timg='image1.png,image2.png,image3.png,image4.png,image5.png,image6.png'

# Split on the comma, and create an array
IFS=',' read -ra images <<< "$timg"

# Start the JSON
echo '{ "pictures": ['

for ((i=0; i<5 && i<${#images[@]}; i++)); do
  printf '  { "source": "%s" }\n' "${images[i]}"
done | paste -sd ","

echo ']}'

Output:

{ "pictures": [
  { "source": "image1.png" },  { "source": "image2.png" },  { "source": "image3.png" },  { "source": "image4.png" },  { "source": "image5.png" }
]}

Notes:

  • Since this shell script uses arrays, it's essential that it starts with #!/bin/bash, otherwise it could be run by a POSIX shell.
  • Consider using single quotes so you don't have to escape double quotes as often (e.g. on the echos that involved JSON)
  • See bash(1) for more info on this more C-like form of a for-loop.
  • ${#images[@]} evaluates to the number of elements in the images array.
  • The entire for-condition will ensure that the loop terminates when we're either at the end of the images array or have emitted 5 objects. Consider making a 5 a variable to help make the code a little more self documenting.
  • printf is a handy way to emit a string that includes double quotes, but needs values to be injected.
  • paste -sd "," will effectively join the lines on its standard input by the provided delimiter. This helps simplify the logic for adding commas.
ctt
  • 1,337
  • 7
  • 18