Use of exec 2>/dev/null

I have a script for which I am getting useless error messages. It is composed mostly of case statements. I placed exec 2>/dev/null at the top of the script and it works totally but the problem is that it also redirects the read command input to the /dev/null. So if a user responds to "type your name," this gets redirected to /dev/null. Is there a way to make the read command still show the input when a user types and the /dev/null to still work for all error messages? I've tried different ways but I have had no luck. The user needs to be able to see what they are typing.

I haven't known read to behave like that. Which OS and shell are you using? Can you post the read statement?

$ read A
blah
$ echo $A
blah
$ exec 2> /dev/null
read B
blahblah
echo $B
blahblah
exec 2>&1
$

I can imagine the behaviour you describe using stty, but not using exec.

Anyhow, if read is the problem, perhaps redirecting output to standard output (with -u1), if that's allowed, is an option.

Although read doesn't read from standard output!

This is what part of the script looks like. When folks are typing, now they can't see the date or the directory name

while [ $y=1 ]; do
exec 2>/dev/null
    case ${answer} in
    "[Xx][Aa][Nn][Gg][Qq]"|1)
              mode="entry"
        echo -e "Specify an Option\n1. name\t2. directory\t3. sum"
        read option
        case ${option} in
            "[Aa][Dd][Dd]"|1)
                echo "enter directory name in lowercase"
                read -e directory
                echo "type the start date"                r\
                read -e startdate

---------- Post updated at 11:55 AM ---------- Previous update was at 10:48 AM ----------

Not sure how to use u1 switch to redirect? Can you please demonstrate with a command?

---------- Post updated at 12:00 PM ---------- Previous update was at 11:55 AM ----------

I tried

read $directory print -u1 "fd1"

but this did not do anything. Probably I don't know how to use this command switch -u1

So it seems you're using Bash.

You could remove the -e option, or use exec before and after the read (or case statement) to temporarily stop redirection.

(that -u1 thing doesn't work)

1 Like