i have bash script(1.sh) which calls (2.sh) within a while loop. Execution stops after just one iteration. if i remove call to 2.sh it executes perfectly

my code 1.sh

while read -a A ; do
        echo "${GREEN}  Making production build for ${A[0]} ${NC}"
        # run build for each component
        ./2.sh ${A[0]} ${A[1]}

        if [[ $? -eq 1 ]]; then
            # create logs of error builds
            ERRORBUILDS+=${A[0]}" ,"

    done < $1   
  • Probably 2.sh reads from its stdin (that happens for instance if you call ssh without -n). You may want to read the content of $1 on a different file descriptor and close it within the loop. See examples at Why is using a shell loop to process text considered bad practice? Feb 3, 2017 at 11:43
  • @StéphaneChazelas sir, a correction in the code(build.sh to 2.sh). Could you please explicitly mention what changes i have to make.
    – Hemant
    Feb 3, 2017 at 11:48
  • It could be just a matter of redirecting 2.sh's stdin from /dev/null (add < /dev/null). The question would we why it reads from its stdin and what it expects to find there. Feb 3, 2017 at 12:00
  • @StéphaneChazelas Thank you for the solution. How to upvote your answer as the right answer
    – Hemant
    Feb 3, 2017 at 12:17

1 Answer 1

./2.sh < /dev/null ${A[0]} ${A[1]}
  • 2
  • Quoted variables would be safer, too, unless you really know why you don't want to quote them. Consider ./2.sh < /dev/null "${A[0]}" "${A[1]}" instead. Feb 3, 2017 at 16:11
  • This solved my similar problem, but now I want to know why. Is there a wiping of input?
    – Chris Reid
    Jan 4, 2020 at 1:05
  • I guess the subshell somehow inherits and consumes stdin. I still find that very odd, too, and just spent hours on bugs caused by that.
    – xeruf
    Jul 5, 2023 at 13:16
  • basically, if the script calls any command that might use stdin you get conflicts, such as du or grep, because it is often silently swallowed... even if it runs in a subshell like $()
    – xeruf
    Jul 5, 2023 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .