BentCoder
BentCoder

Reputation: 12740

Suppressing errors messages that might occur when using cat command with EOF or EOD in bash script

My aim is to suppress all the error messages that might occur when using cat with EOF or EOD.

When I run the bash script, I'm getting error below because I deliberately added / sign at the end of the file name.

ERROR

my-hello.sh: line 5: hello.txt/: Is a directory
BAD

SCRIPT

#!/bin/bash

function create()
{
    cat << EOF > hello.txt/
    Hello
EOF
}

createHook
if [ $? -eq 0 ]; then
   printf "GOOD"
else
   printf "BAD"
fi

How can I suppress error message being printed on the terminal?

I tried 2>/dev/null so on but EOF doesn't like it because of known white spacing reasons.

NOTE: The content of the file is actually bigger but I've just put Hello in it simplify. I'm open for changes if that solves my problem.

Upvotes: 0

Views: 679

Answers (1)

P.P
P.P

Reputation: 121407

You need to use redirection to /dev/null like this:

function create()
{

    cat 2>/dev/null << EOF > hello.txt/
    Hello
EOF
}

You can also redirect all the errors from the create() function:

function create()
{
    cat << EOF > hello.txt/
    Hello
EOF
}

createHook 2>/dev/null

Upvotes: 1

Related Questions