How to pass value to read variable with pipe (stdin) in bash

FotisK

I read a lot about passing piping stdin to bash read function, but nothing seems to work for my bash version!!

GNU bash, version 3.2.51(1)-release (x86_64-suse-linux-gnu)

I have a bash script that in some point asks the user "yes/no" with variable CONTINUEQUESTION:

echo "Do you want to continue? (yes/no):"
read CONTINUEQUESTION
tmp=$(tr '[:upper:]' '[:lower:]' <<<$CONTINUEQUESTION)
if [[ "$tmp" != 'y'  &&  "$tmp" != 'yes' ]]; then
                echo "Aborting because of input '$CONTINUEQUESTION'"
                exit
fi

I would like to pipe a "yes or no" to this question without user input! Yes i know i could use expect, but i don't prefer it in this case.

So i tried several things:

CONTINUEQUESTION='yes'
echo $CONTINUEQUESTION | ./myscript.sh

Aborting because of input ''

./myscript.sh <<< "$CONTINUEQUESTION"

Aborting because of input ''

...and many other, nothing worked!?

O.k. now I did a bit revers thinking and find out that the below line causes the problem with the pipe...because when i remarked it out all the below answers are working just fine, but not when this line is executed:

running=`ssh root@${HOSTNAME} 'su - root -c "/bin/tools list | grep \"system running\"" 2>&1'`

But, i need this line before the read! What do i need to reverse the 2>&1????

My script look like this and is working without this try to over come the user intervantion:

LIST_FILE_NAME=$1
STILL_RUNNING=0

running=`ssh root@${HOSTNAME} 'su - root -c "cat '$LIST_FILE_NAME' | grep \"system running\"" 2>&1'`
if [[ $running =~ .*running.* ]]; then
        STILL_RUNNING=1
        echo "NODE $NODE running stop before continuing."
fi
if [ $STILL_RUNNING -eq 1 ]; then
        echo "Aborting system was still running!"
        exit 1
fi

echo "Do you want to continue? (yes/no):"
read CONTINUEQUESTION

tmp=$(tr '[:upper:]' '[:lower:]' <<<$CONTINUEQUESTION)
if [[ "$tmp" != 'y'  &&  "$tmp" != 'yes' ]]; then
                echo "Aborting because of input '$CONTINUEQUESTION'"
                exit
fi

echo "o.k."

4 points:

  1. list.log can have a line with "system running" or "system notrunning"
  2. if list.log has a line with "system notrunning" than the bash script continue towards the question
  3. at the question i never got it right to inject the 'y' or 'yes' so the bash aborts because of input ''
  4. i execute this like: ./myscript.sh list list.log (normal way)

This bash runs well if the user interacts at the question!

Thanks for you time!!!

konsolebox

Consider this variation as well:

#!/bin/bash
read -p "Do you want to continue? (yes/no): " CONTINUEQUESTION
if [[ $CONTINUEQUESTION != [Yy] && $CONTINUEQUESTION != [Yy][Ee][Ss] ]]; then
    echo "Aborting because of input '$CONTINUEQUESTION'."
    exit
fi

Tested with:

bash script.sh <<< yes

If it doesn't work, show the output of:

bash -x script.sh <<< yes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read stdin in chunks in Bash pipe

From Dev

How to correctly read a value from stdin into a variable

From Dev

bash securely pipe environment variable to stdin

From Dev

Pass bash loop variable through pipe to AWK

From Dev

Perl inside Bash: How to read from pipe and pass arguments to perl at the same time?

From Dev

How to read from file *and* stdin in bash

From Dev

how to pass multi-line output from pipe to a variable in bash? without using temp file

From Dev

Bash: replacing a substring in pipe stdin

From Dev

Bash Read CSV value from Variable in Bash

From Dev

Pass arguments with pipe in bash

From Dev

Read from stdin and pipe to next command

From Dev

Pass Variable Name and Value in Bash Script

From Dev

Pass the variable and value from python to bash

From Dev

Read/write value with pipe

From Dev

Bash: How to read password from stdin without echoing over SSH

From Dev

How do you pass two variables over a pipe in bash?

From Java

How to pass a function value to a variable?

From Dev

How do I reproduce `stdin=sys.stdin` with `stdin=PIPE`?

From Dev

Bash read command and stdin redirection

From Dev

bash return value in pipe to bash

From Dev

Variable input read from stdin

From Dev

Accessing the output of a Bash pipe with 'read'

From Dev

Bash inline pipe with read behavior

From Dev

How to pass the javascript variable value to php variable?

From Dev

How to pass filename through variable to be read it by awk

From Dev

Java how to read stdin?

From Dev

How can I pipe a bash variable into a GREP regex in the following command?

From Dev

How to truncate bash variable assignment without closing pipe

From Dev

How to pipe `terraform state show` outputs to a bash variable?

Related Related

HotTag

Archive