Shell exception Handling

Source: Internet
Author: User
Tags sin

Write the shell for a long time, often be unusually troubled, can unexpectedly adhere to a number of years useless, recalled the previous service company, Amitabha, sin sin. Talk less, hope this article can help you and I completely end the shell script is the primary stage of the Linux command collection.

First, STDOUT, STDERR

If you want to get the exception output of a shell script, you need to first understand the standard output stdout, standard error stderr of the shell command.

When we write shell scripts, we operate very frequently with standard input stdin, standard output stdout, and standard error stderr that execute commands. When we execute a script file or execute a shell command, it is difficult to distinguish between the standard output and the standard error from the terminal output. So we redirect this information to a specific place so that we can analyze the execution of script files and shell commands, which uses the file descriptor. A file descriptor is an integer associated with an open file or data stream, and 0, 1, and 2 are three file descriptors reserved by the system, corresponding to standard input, standard output, and standard error. The Linux Shell uses ">" >> to reposition the file descriptor. For example, code:

# !/bin/bashls liqiu >/tmp/#很明显这是一个错误的命令echo $# captures the output of the previous command (if 0 normal else error) 
    ls-l >/tmp/logecho $?

Output Result:

@~ $ ~/study/test. SH ls No such file or directory Ten

1 description ' ls liqiu >/tmp/error ' execution error,0 description ls-l >/tmp/log execution succeeded. Then hope that the demerit is/tmp/error error log,/tmp/log Save the correct results, can see the file discovery is not so.

@~ $ more/tmp/Log Total0drwx------3Liqiu Staff102  4  -   theapplicationsdrwx------+4Liqiu Staff136  One  2  -: .Desktopdrwxr-xr- x   4Liqiu Staff136 Ten  -  the: Aboutsvn@~ $ more/tmp/Error @~ $

The reason: the default parameter of the relocation operator ">" is the standard output stdout, which is 1; Therefore, ">" is equivalent to "1>"; The above code is equivalent to:

# !/bin/bash 1>/tmp/errorecho $# captures output from previous command (if 0 normal else error)1>/tmp/Log  echo $?
second, catch the exception

So to catch an exception, you need to modify the code:

# !/bin/bash 2>&1 # REDIRECT standard error stderr to standard output stdout using "2>&1";echo $#  Capture the output of the previous command (if 0 normal else error)ls-l >/tmp/logecho $?

After execution, you find that the error message appears in the file/tmp/log.

@~ $ cat/tmp/error lsNo  such file or directory@~ $

Shell exception Handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.