Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the first line is skipped

Why is the first line skipped if I remove echo "prevent remove first line"?

(echo "prevent remove first line" && find ~/somedir -mindepth 1 -maxdepth 1 -type d) | /usr/local/bin/jq -nR \
'{
    "items": [
        inputs |                                     
        inputs as $title |                  
        {
            "title": $title,
        }
    ]
}'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your example should be:

find ~/somedir -mindepth 1 -maxdepth 1 -type d | /usr/local/bin/jq -nR \
'{
    "items": [      
        inputs as $title |                  
        {
            "title": $title,
        }
    ]
}'

From the jq man page:

 inputs
       Outputs all remaining inputs, one by one.

       This is primarily useful for reductions over a program´s inputs.

This implicitly means that every time called, inputs reads the next line from stdin. stdin is realized as a pipe, once a line from stdin has been read, it is gone.

When you echo a line before the actual input, then this extra line will be subject to this extra read. Just use inputs twice to see the problem coming back:

(echo "test" && find ~/somedir -mindepth 1 -maxdepth 1 -type d) | /usr/local/bin/jq -nR \
'{
    "items": [
        inputs | inputs |
        inputs as $title |                  
        {
            "title": $title,
        }
    ]
}'

PS: As pmf pointed out, the whole solution can be simplified to:

find ~/somedir -mindepth 1 -maxdepth 1 -type d \
    jq -nR '{items: [{title: inputs}]}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading