-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"}
1
  • 1
    Don't create JSON by hand like that. Install the jq utility. Commented Aug 31, 2018 at 1:29

3 Answers 3

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"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Thx is the most beatiful answer i like :)
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.

1 Comment

Nice one! Thx =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.