0

below you will find a simple code. i am calling function input if the read input is Y however read command in input function never executes.

#!/bin/bash
input() {
    read -p "Press any key to continue..." # read input (this part never executed) 
}

mg() # prompt user for y=true, n=false
{
    [ $FORCE ] && echo>&2 "$* (forced)" && return
    while read<&2 -p "$* (Y/N)? [Y] " YN
    do
        [ -z "$YN" -o "$YN" = Y ] && return
        [ "$YN" = N ] && return 1
    done
    err "failed to read from YN   $*"    
}


if mg "Enter Input?"       
then in="yes" | input # call function input if mg return Y
else in="no"
fi
2
  • Try executing with "bash -x <your_script>" to see what's going on. Commented Jul 24, 2013 at 12:22
  • 2
    Any specific reason for using a pipe | in if? Commented Jul 24, 2013 at 12:29

2 Answers 2

3

The call to input is getting its standard output from the parameter assignment. As soon as the assignment completes, its (nonexistant) standard output is closed, which read interprets as EOF which causes the read to return.

Use a semi-colon instead.

if mg "Enter Input?"
then in="yes"; input
else in="no"
fi

(Removing the pipeline also allows in to be assigned in the current shell, not in the pipeline-induced subshell.)

Sign up to request clarification or add additional context in comments.

Comments

0

Your line

then in="yes" | input

is the issue. You don't want to pipe the assignment of in into input. Separate the commands with a semicolon or a newline:

then in="yes"; input

Comments

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.