Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash read and stderr redirection

Tags:

redirect

bash

So I've got a script that runs test cases on another script. I'm trying to redirect stderr while running the test cases. The part that is giving me problems is the read command:

within script1:

read -p "Delete $file? (y/n) " input

within testscript:

$script $opts $file 2>/dev/null

The read calls from script1 get redirected as well.

like image 343
kjprice Avatar asked May 12 '11 20:05

kjprice


People also ask

What is the meaning of 2 >& 1?

&1 is used to reference the value of the file descriptor 1 (stdout). Now to the point 2>&1 means “Redirect the stderr to the same place we are redirecting the stdout” Now you can do this.

Can stderr be redirected?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


2 Answers

Redirect the prompt to stdout.

read -p "Delete $file? (y/n) " input 2>&1
like image 67
John Kugelman Avatar answered Sep 28 '22 06:09

John Kugelman


You can go simple:

echo "Delete $file? (y/n)"
read input
like image 29
dimba Avatar answered Sep 28 '22 06:09

dimba