Echo error code linux

Is there a standard Bash tool that acts like echo but outputs to stderr rather than stdout? I know I can do echo foo 1>&2 but it's kinda ugly and, I suspect, error prone (e.g. more likely t...

Is there a standard Bash tool that acts like echo but outputs to stderr rather than stdout?

I know I can do echo foo 1>&2 but it’s kinda ugly and, I suspect, error prone (e.g. more likely to get edited wrong when things change).

Zombo's user avatar

asked Jun 7, 2010 at 14:36

BCS's user avatar

2

You could do this, which facilitates reading:

>&2 echo "error"

>&2 copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was originally referring to. For more information see the Bash Hackers Illustrated Redirection Tutorial.

John Kugelman's user avatar

John Kugelman

343k67 gold badges517 silver badges565 bronze badges

answered May 8, 2014 at 18:59

Marco Aurelio's user avatar

Marco AurelioMarco Aurelio

20k1 gold badge16 silver badges16 bronze badges

11

You could define a function:

echoerr() { echo "$@" 1>&2; }
echoerr hello world

This would be faster than a script and have no dependencies.

Camilo Martin’s bash specific suggestion uses a «here string» and will print anything you pass to it, including arguments (-n) that echo would normally swallow:

echoerr() { cat <<< "$@" 1>&2; }

Glenn Jackman’s solution also avoids the argument swallowing problem:

echoerr() { printf "%sn" "$*" >&2; }

answered Jun 7, 2010 at 14:52

James's user avatar

JamesJames

6,8382 gold badges16 silver badges18 bronze badges

13

Since 1 is the standard output, you do not have to explicitly name it in front of an output redirection like >. Instead, you can simply type:

echo This message goes to stderr >&2

Since you seem to be worried that 1>&2 will be difficult for you to reliably type, the elimination of the redundant 1 might be a slight encouragement to you!

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Jul 10, 2012 at 21:24

Brandon Rhodes's user avatar

Brandon RhodesBrandon Rhodes

81.1k16 gold badges105 silver badges145 bronze badges

2

Another option

echo foo >>/dev/stderr

Robin Winslow's user avatar

answered Jan 16, 2013 at 3:57

Zombo's user avatar

5

No, that’s the standard way to do it. It shouldn’t cause errors.

answered Jun 7, 2010 at 14:37

Matthew Flaschen's user avatar

Matthew FlaschenMatthew Flaschen

274k50 gold badges513 silver badges537 bronze badges

6

If you don’t mind logging the message also to syslog, the not_so_ugly way is:

logger -s $msg

The -s option means: «Output the message to standard error as well as to the system log.»

answered Aug 4, 2014 at 19:15

Grzegorz Luczywo's user avatar

Grzegorz LuczywoGrzegorz Luczywo

9,7021 gold badge32 silver badges22 bronze badges

2

Another option that I recently stumbled on is this:

    {
        echo "First error line"
        echo "Second error line"
        echo "Third error line"
    } >&2

This uses only Bash built-ins while making multi-line error output less error prone (since you don’t have to remember to add &>2 to every line).

answered Feb 18, 2019 at 0:17

GuyPaddock's user avatar

GuyPaddockGuyPaddock

1,9091 gold badge21 silver badges23 bronze badges

3

Note: I’m answering the post- not the misleading/vague «echo that outputs to stderr» question (already answered by OP).

Use a function to show the intention and source the implementation you want. E.g.

#!/bin/bash

[ -x error_handling ] && . error_handling

filename="foobar.txt"
config_error $filename "invalid value!"

output_xml_error "No such account"

debug_output "Skipping cache"

log_error "Timeout downloading archive"

notify_admin "Out of disk space!"

fatal "failed to open logger!"

And error_handling being:

ADMIN_EMAIL=root@localhost

config_error() { filename="$1"; shift; echo "Config error in $filename: $*" 2>&1; }

output_xml_error() { echo "<error>$*</error>" 2>&1; }

debug_output() { [ "$DEBUG"=="1" ] && echo "DEBUG: $*"; }

log_error() { logger -s "$*"; }

fatal() { which logger >/dev/null && logger -s "FATAL: $*" || echo "FATAL: $*"; exit 100; }

notify_admin() { echo "$*" | mail -s "Error from script" "$ADMIN_EMAIL"; }

Reasons that handle concerns in OP:

  • nicest syntax possible (meaningful words instead of ugly symbols)
  • harder to make an error (especially if you reuse the script)
  • it’s not a standard Bash tool, but it can be a standard shell library for you or your company/organization

Other reasons:

  • clarity — shows intention to other maintainers
  • speed — functions are faster than shell scripts
  • reusability — a function can call another function
  • configurability — no need to edit original script
  • debugging — easier to find the line responsible for an error (especially if you’re deadling with a ton of redirecting/filtering output)
  • robustness — if a function is missing and you can’t edit the script, you can fall back to using external tool with the same name (e.g. log_error can be aliased to logger on Linux)
  • switching implementations — you can switch to external tools by removing the «x» attribute of the library
  • output agnostic — you no longer have to care if it goes to STDERR or elsewhere
  • personalizing — you can configure behavior with environment variables

answered Mar 18, 2016 at 18:07

Cezary Baginski's user avatar

My suggestion:

echo "my errz" >> /proc/self/fd/2

or

echo "my errz" >> /dev/stderr

echo "my errz" > /proc/self/fd/2 will effectively output to stderr because /proc/self is a link to the current process, and /proc/self/fd holds the process opened file descriptors, and then, 0, 1, and 2 stand for stdin, stdout and stderr respectively.

The /proc/self link doesn’t work on MacOS, however, /proc/self/fd/* is available on Termux on Android, but not /dev/stderr. How to detect the OS from a Bash script? can help if you need to make your script more portable by determining which variant to use.

Sled's user avatar

Sled

18.3k27 gold badges119 silver badges161 bronze badges

answered Sep 3, 2017 at 1:48

Sebastian's user avatar

SebastianSebastian

4,1352 gold badges40 silver badges41 bronze badges

3

Don’t use cat as some have mentioned here. cat is a program
while echo and printf are bash (shell) builtins. Launching a program or another script (also mentioned above) means to create a new process with all its costs. Using builtins, writing functions is quite cheap, because there is no need to create (execute) a process (-environment).

The opener asks «is there any standard tool to output (pipe) to stderr», the short answer is : NO … why? … redirecting pipes is an elementary concept in systems like unix (Linux…) and bash (sh) builds up on these concepts.

I agree with the opener that redirecting with notations like this: &2>1 is not very pleasant for modern programmers, but that’s bash. Bash was not intended to write huge and robust programs, it is intended to help the admins to get there work with less keypresses ;-)

And at least, you can place the redirection anywhere in the line:

$ echo This message >&2 goes to stderr 
This message goes to stderr

Marcel Gosselin's user avatar

answered Oct 9, 2014 at 13:18

return42's user avatar

return42return42

5453 silver badges10 bronze badges

4

This is a simple STDERR function, which redirect the pipe input to STDERR.

#!/bin/bash
# *************************************************************
# This function redirect the pipe input to STDERR.
#
# @param stream
# @return string
#
function STDERR () {

cat - 1>&2

}

# remove the directory /bubu
if rm /bubu 2>/dev/null; then
    echo "Bubu is gone."
else
    echo "Has anyone seen Bubu?" | STDERR
fi


# run the bubu.sh and redirect you output
tux@earth:~$ ./bubu.sh >/tmp/bubu.log 2>/tmp/bubu.err

answered Feb 3, 2012 at 8:40

erselbst's user avatar

erselbsterselbst

1631 silver badge6 bronze badges

2

read is a shell builtin command that prints to stderr, and can be used like echo without performing redirection tricks:

read -t 0.1 -p "This will be sent to stderr"

The -t 0.1 is a timeout that disables read’s main functionality, storing one line of stdin into a variable.

answered Jan 24, 2013 at 0:16

Douglas Mayle's user avatar

Douglas MayleDouglas Mayle

20.7k8 gold badges42 silver badges57 bronze badges

1

Combining solution suggested by James Roth and Glenn Jackman

  • add ANSI color code to display the error message in red:
echoerr() { printf "e[31;1m%se[0mn" "$*" >&2; }

# if somehow e is not working on your terminal, use u001b instead
# echoerr() { printf "u001b[31;1m%su001b[0mn" "$*" >&2; }

echoerr "This error message should be RED"

answered Jun 6, 2020 at 17:20

Polymerase's user avatar

PolymerasePolymerase

6,06111 gold badges42 silver badges64 bronze badges

Make a script

#!/bin/sh
echo $* 1>&2

that would be your tool.

Or make a function if you don’t want to have a script in separate file.

BCS's user avatar

BCS

74.1k67 gold badges185 silver badges291 bronze badges

answered Jun 7, 2010 at 14:48

n0rd's user avatar

n0rdn0rd

11.6k5 gold badges34 silver badges55 bronze badges

3

Here is a function for checking the exit status of the last command, showing error and terminate the script.

or_exit() {
    local exit_status=$?
    local message=$*

    if [ "$exit_status" -gt 0 ]
    then
        echo "$(date '+%F %T') [$(basename "$0" .sh)] [ERROR] $message" >&2
        exit "$exit_status"
    fi
}

Usage:

gzip "$data_dir"
    or_exit "Cannot gzip $data_dir"

rm -rf "$junk"
    or_exit Cannot remove $junk folder

The function prints out the script name and the date in order to be useful when the script is called from crontab and logs the errors.

59 23 * * * /my/backup.sh 2>> /my/error.log

answered Oct 13, 2020 at 8:39

Miroslav Popov's user avatar

Miroslav PopovMiroslav Popov

3,2054 gold badges31 silver badges55 bronze badges

Linux Error Codes

Introduction to Linux Error codes

In the Linux operating system, we are working on many components. While working, we are getting lots of errors. The same errors can be acknowledged by the Linux error codes. There are different error codes available as per the execution error. We can capture the error code with the help of the “echo” command also.

To fix the error message in the Linux level jobs/query or application-level job, it is mandatory that we need to understand the nature of the error, the error description, and the error code. Accordingly, we can fix the application or job on the Linux level.

The error code utility i.e., “errno” is written by the Lars Wirzenius.

Syntax of Error Codes

errno [ OPTION ] [ STRING ]
echo $?

  • errno: We can use the “errno” keyword in the syntax or command. It will take the two arguments as access OPTION and the string. As per the provided arguments, it will provide the error information as per the string or the error code.
  • OPTION: We can provide the different flags as options that are compatible with the “errno” command.
  • STRING: We can provide the short string as per the “error” define. It will provide the error information concerning the provided string.
  • echo $?: The echo command will also provide the error code. We need to use the “?” symbol with the echo command.

How Linux Error Codes Works?

In Linux, we are having a total of 134 error codes. Every error message or the failure in the Linux environment, it will have their error code. With the help of error code, we can fetch the error description and help to fix the issue or error message. If we will keep the same error message as it is then it will be a big impact on the server level.

When we are executing any command or job on the Linux level without any error then it will be fine. But if the Linux command will not be executed properly then the Linux compiler will notify with the relevant error message to the login user.

Below are the lists of error code information available,

Sr No Error Code Error Number Error Description
1 EPERM 1 It will print the error message if the operation is not permitted.
2 ENOENT 2 It will print the error message if there are no such files or directory exists.
3 ESRCH 3 It will print the error message if there is no such process exists.
4 EINTR 4 It will print the error message if any interrupted system calls.
5 EIO 5 It will print the error message if it is any input/output error.
6 ENXIO 6 It will print the error message if there is no such device or address exists.
7 E2BIG 7 It will print the error message if the argument list is too long.
8 ENOEXEC 8 It will print the error message if there is an exec format error.
9 EBADF 9 It will print the error message in it is a bad file descriptor.
10 ECHILD 10 It will print the error message if there are no child process exits.

Examples to implement Linux Error codes

Here are the following examples mention below

Examples #1 – Print the List

The “errno” utility is a very simple and common way to check the list of error codes in a Linux environment. It will print all the list of error codes with the error number and error description.

Command :        

errno -l

Explanation :

As per the above command, we can print all the list of error codes available in the Linux environment. It will print the details information of error code like an error code name, error code number, and the description of the error code.

Output :

Linux Error Codes output 1

Example #2 – Information of Individual Error Number

By default, we are getting all the list of error code information. But we can also get the individual error number information as well.

Command:

errno 9

Explanation :

In the “errno” utility, we can get the specific information error information with the help of an error number. We have used the error no “9” and get the information of “error number 9” only.

Output :

Linux Error Codes output 2

Example #3 – Information of Individual Error Name

In the error codes, we can get the individual error number information. Similarly, we are having the functionality to print the individual error name information.

Command :

errno EBADF

Explanation :

As per the above command, we can get the specific information error information with the help of the error name. We have used the error name “EBADF” and get the information of “error name (EBADF)” only.

Output :

Linux Error Codes output 3

Example #4 – Error Code Information from the Input String

In the Linux environment, we are having the functionality to get the error code and error number information from the input string. We need to use the “-s” option with the “errno” utility.

Note: we need to provide the relevant input string as an input to the “errno” command.

Command :

errno -s access

Explanation :

When we don’t know the exact error number or name then we can use the relevant string to identify the error code and description of it. We have used the “access” string and get the related information of access.

Output :

output 4

Example #5 – Get the Error Code Information with “echo” Command

In the Linux ecosystem, we are having the functionality to check the quick error code information via the “echo” command. If it will return the “0” output then the command was executed fine. If it will return the output value except “0” then there are some issues in the command or job.

Command :

cat test_file.txt
echo $?

Explanation :

In screenshot 1 (a), we are getting the echo command output as “0”. Hence, it is an indication of the proper execution of the command. In screenshot 1 (b), we are getting the echo command output as “1”. Because the previous command was not executed successfully.

Output :

output 5.1

Screenshot 1 (a)

output 5.2

Screenshot 1 (b)

Conclusion

We have seen the uncut concept of “Linux Error Codes” with the proper example, explanation, and command with different outputs. Every error code is having its unique error name and the error number. The error codes are very important in terms of fixing the issues on command or job level.

Recommended Articles

This is a guide to Linux Error Codes. Here we discuss How Linux Error Codes Works and Examples along with the commands and outputs. You may also have a look at the following articles to learn more –

  1. Linux Mount Command
  2. Linux Inode
  3. Linux tac
  4. Linux Free Command
Echo to stderr in Bash

A default error messaging variable built-in Bash script is known as stderr. It is also known as Standard Error, a default output device for errors.

Sometimes we must redirect the errors into the output channel. The Linux environment identifies every file object with its description, known as FD.

It is a positive integer value that identifies the open file session. For stderr, the value of the file descriptor is 2.

This article will learn about the stderr and its functionality. Also, we will look at some examples that will make the topic easier.

Echo to stderr in Bash

The command stderr is mainly used to keep a recode of error during the execution of any command. The general syntax of this command is:

Your command here 2>error.log

In the sample syntax shared above, you can find a symbol 2> and a file named error.log. Now the 2> indicates the value of FILE DESCRIPTOR, which represents the identity of stderr.

In our case, the value of the FILE DESCRIPTOR is 2. Now the file name we provided is for the log file generated during the command execution.

You can also echo the stderr message by following the below syntax format.

Your command here >&2 echo "error"

Let’s see an example to make the topic easier. Suppose we have created an error command and want to echo the output error message.

Now our example code will look like this:

echo $( 3 + 2 )) >&2 echo "error"

In the code shared above, we intentionally made an error to understand how it works. We removed one of the brackets here, so this can be an error.

After that, we show that error with the echo. An output like the one below is shown after running the code above.

./example.sh: line 1: syntax error near unexpected token `)'
./example.sh: line 1: `echo $( 3 + 2 )) >&2 echo "error" '

Now we correct the code like below and run the command again.

echo $(( 3 + 2 )) >&2 "No error has been found."

After running the code, you will get an output like the below.

5 No error has been found.

All the codes used in this article are written in Bash. It will only work in the Linux Shell environment.

stdin, stdout, and stderr are streams attached to file descriptors 0, 1, and 2 respectively of a process.

At the prompt of an interactive shell in a terminal or terminal emulator, all those 3 file descriptors would refer to the same open file description which would have been obtained by opening a terminal or pseudo-terminal device file (something like /dev/pts/0) in read+write mode.

If from that interactive shell, you start your script without using any redirection, your script will inherit those file descriptors.

On Linux, /dev/stdin, /dev/stdout, /dev/stderr are symbolic links to /proc/self/fd/0, /proc/self/fd/1, /proc/self/fd/2 respectively, themselves special symlinks to the actual file that is open on those file descriptors.

They are not stdin, stdout, stderr, they are special files that identify what files stdin, stdout, stderr go to (note that it’s different in other systems than Linux that have those special files).

reading something from stdin means reading from file descriptor 0 (which will point somewhere within the file referenced by /dev/stdin).

But in $(</dev/stdin), the shell is not reading from stdin, it opens a new file descriptor for reading on the same file as the one open on stdin (so reading from the start of the file, not where stdin currently points to).

Except in the special case of terminal devices open in read+write mode, stdout and stderr are usually not open for reading. They are meant to be streams that you write to. So reading from the file descriptor 1 will generally not work. On Linux, opening /dev/stdout or /dev/stderr for reading (as in $(</dev/stdout)) would work and would let you read from the file where stdout goes to (and if stdout was a pipe, that would read from the other end of the pipe, and if it was a socket, it would fail as you can’t open a socket).

In our case of the script run without redirection at the prompt of an interactive shell in a terminal, all of /dev/stdin, /dev/stdout and /dev/stderr will be that /dev/pts/x terminal device file.

Reading from those special files returns what is sent by the terminal (what you type on the keyboard). Writing to them will send the text to the terminal (for display).

echo $(</dev/stdin)
echo $(</dev/stderr)

will be the same. To expand $(</dev/stdin), the shell will open that /dev/pts/0 and read what you type until you press ^D on an empty line. They will then pass the expansion (what you typed stripped of the trailing newlines and subject to split+glob) to echo which will then output it on stdout (for display).

However in:

echo $(</dev/stdout)

in bash (and bash only), it’s important to realise that inside $(...), stdout has been redirected. It is now a pipe. In the case of bash, a child shell process is reading the content of the file (here /dev/stdout) and writing it to the pipe, while the parent reads from the other end to make up the expansion.

In this case when that child bash process opens /dev/stdout, it is actually opening the reading end of the pipe. Nothing will ever come from that, it’s a deadlock situation.

If you wanted to read from the file pointed-to by the scripts stdout, you’d work around it with:

 { echo content of file on stdout: "$(</dev/fd/3)"; } 3<&1

That would duplicate the fd 1 onto the fd 3, so /dev/fd/3 would point to the same file as /dev/stdout.

With a script like:

#! /bin/bash -
printf 'content of file on stdin: %sn' "$(</dev/stdin)"
{ printf 'content of file on stdout: %sn' "$(</dev/fd/3)"; } 3<&1
printf 'content of file on stderr: %sn' "$(</dev/stderr)"

When run as:

echo bar > err
echo foo | myscript > out 2>> err

You’d see in out afterwards:

content of file on stdin: foo
content of file on stdout: content of file on stdin: foo
content of file on stderr: bar

If as opposed to reading from /dev/stdin, /dev/stdout, /dev/stderr, you wanted to read from stdin, stdout and stderr (which would make even less sense), you’d do:

#! /bin/sh -
printf 'what I read from stdin: %sn' "$(cat)"
{ printf 'what I read from stdout: %sn' "$(cat <&3)"; } 3<&1
printf 'what I read from stderr: %sn' "$(cat <&2)"

If you started that second script again as:

echo bar > err
echo foo | myscript > out 2>> err

You’d see in out:

what I read from stdin: foo
what I read from stdout:
what I read from stderr:

and in err:

bar
cat: -: Bad file descriptor
cat: -: Bad file descriptor

For stdout and stderr, cat fails because the file descriptors were open for writing only, not reading, the the expansion of $(cat <&3) and $(cat <&2) is empty.

If you called it as:

echo out > out
echo err > err
echo foo | myscript 1<> out 2<> err

(where <> opens in read+write mode without truncation), you’d see in out:

what I read from stdin: foo
what I read from stdout:
what I read from stderr: err

and in err:

err

You’ll notice that nothing was read from stdout, because the previous printf had overwritten the content of out with what I read from stdin: foon and left the stdout position within that file just after. If you had primed out with some larger text, like:

echo 'This is longer than "what I read from stdin": foo' > out

Then you’d get in out:

what I read from stdin: foo
read from stdin": foo
what I read from stdout: read from stdin": foo
what I read from stderr: err

See how the $(cat <&3) has read what was left after the first printf and doing so also moved the stdout position past it so that the next printf outputs what was read after.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Eccs c u error signal
  • Ecc error rate что такое
  • Ecc error rate как исправить
  • Ecc error rate ssd что значит
  • Ecc error rate hdd

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии