官术网_书友最值得收藏!

Redirection

When a file descriptor is assigned to something other than a terminal, this is called I/O redirection. The shell performs the redirection of output to a file by closing the standard output file descriptor 1 (the Terminal) and then assigning that descriptor to the file. When redirecting standard input, the shell closes file descriptor 0 (the Terminal) and assigns that descriptor to a file. The Bash shells handle errors by assigning a file to file descriptor 2.The following command will take input from the sample.txt file:

    $ wc  <  sample.txt  

The preceding command will take content from the sample.text file. The wc command will print the number of lines, words, and characters in the sample.txt file. This command will redirect output to be saved in the log.txt file:

    $ echo "Hello world" > log.txt
  

This command will append the Hello World text to the log.txt file:

    $ echo "Welcome to Shell Scripting" >> log.txt

The single > will overwrite or replace the existing text in the log file, and double >> will append the text to the log file. Let's see a few more examples:

    $ tr '[A-Z]' '[a-z]' < sample.txt

The preceding tr command will read text from the sample.txt file. The tr command will convert all uppercase letters to lowercase letters and will print the converted text on screen:

    $ ls > log.txt
    $ cat log.txt

The output of the command is as follows:

    dir_1
    sample.txt
    extra.file

In this example command, ls sends directory content to file log.txt. Whenever we want to store the result of the command in the file, we can use the preceding example.

    $ date >> log.txt
    $ cat log.txt

The output is as follows:

    dir_1
    dir_2
    file_1
    file_2
    file_3
    Sun Sept 17 12:57:22 PDT 2004

In the preceding example, we redirect and append the result of the date command to the log.txt file.

    $ gcc hello.c 2> error_file

The gcc is a C language compiler program. If an error is encountered during compilation, then it will be redirected to error_file. The > character is used for a success result and 2> is used for error-result redirection. We can use error_file for debugging purposes:

    $ find . -name "*.sh" > success_file 2> /dev/null

In the preceding example, we redirect output or success results to success_file, and errors to /dev/null. /dev/null is used to destroy the data, which we do not want to be shown on screen.

    $ find . -name "*.sh" &> log.txt

The preceding command will redirect both output and errors to log.txt.

    $ find . -name "*.sh"  > log.tx 2>&1

The preceding command will redirect the result to log.txt and send errors to where the output goes, such as log.txt.

    $ echo "File needs an argument" 1>&2

The preceding command will send standard output to the standard error. This will merge the output with the standard errors. A summary of all I/O redirection commands is as follows:

Run the following command:

    $ touch filea fileb filec fileab filebc filead filebd filead
    $ touch file{1,2,3}
  

Try out the following command:

    $ ls  s*
    $ ls  file
    $ ls  file[abc]
    $ ls  file[abc][cd]
    $ ls  file[^bc]
    $ touch file file1 file2 file3 ... file20
    $ ls ?????
    file1
    file2
    file3
    $ ls file*
    file file1 file10 file2 file3
    $ ls file[0-9]
    file1 file2 file3
    $ ls file[0-9]*
    file1 file10 file2 file3
    $ ls file[!1-2]
    file3
  
主站蜘蛛池模板: 乾安县| 宁化县| 讷河市| 大理市| 蓝山县| 西充县| 南澳县| 吉木萨尔县| 手游| 合川市| 武平县| 怀宁县| 大渡口区| 平利县| 鄂尔多斯市| 建始县| 确山县| 靖西县| 涪陵区| 昂仁县| 龙胜| 泰顺县| 于都县| 宜黄县| 新泰市| 泗洪县| 黑水县| 新兴县| 虞城县| 屏南县| 鸡东县| 临漳县| 阿图什市| 鞍山市| 鄯善县| 修文县| 和顺县| 巴里| 贡觉县| 叙永县| 临武县|