Learning bash notes-input and output, bash hide output

Source: Internet
Author: User

Learning bash notes-input and output, bash hide output
1. I/O redirection

The I/O redirection is as follows:

  • Cmd1 | cmd2: pipeline that receives the standard output of cmd1 as the standard input of cmd2.
  • > File: redirects standard output to file.
  • <File: receives standard input from file
  • > File: redirects the standard output to file. If the file exists, it is appended
  • > | File: Even if noclobber is set, the standard output is still forced to file.

Shell provides a feature called noclobber, which prevents the existing files from being overwritten inadvertently during redirection. By setting the noclobber variable, you can

Open this feature. If you redirect the output to an existing file, shell reports the error message and does not execute the redirection command.

For example:

Root @-virtual-machine :~ # Cat a.txt
123
Root @ virtual-machine :~ # Set-o | grep "noclobber"
Noclobber off
Root @ virtual-machine :~ # Echo "test1"> a.txt
Root @ virtual-machine :~ # Cat a.txt
Test1
Root @ virtual-machine :~ # Set-o noclobber
Root @ virtual-machine :~ # Set-o | grep "noclobber"
Noclobber on
Root @ gmdz-virtual-machine :~ # Echo "hello"> a.txt
-Bash: a.txt: The existing files cannot be overwritten.

Root @ virtual-machine :~ # Echo "hello"> | a.txt
Root @ virtual-machine :~ # Cat a.txt
Hello

  • N> | file: The file is forcibly output from file descriptor n even if noclobber is set.

For example:

Root @-virtual-machine :~ # Cat a.txt
123
Root @ virtual-machine :~ # Set-o | grep "noclobber"
Noclobber off
Root @ virtual-machine :~ # Ls nosuchfile 2> a.txt
Root @ virtual-machine :~ # Cat a.txt
Ls: unable to access nosuchfile: No file or directory
Root @ virtual-machine :~ # Echo "123"> a.txt
Root @ virtual-machine :~ # Cat a.txt
123
Root @ virtual-machine :~ # Set-o noclobber
Root @ virtual-machine :~ # Set-o | grep "noclobber"
Noclobber on
Root @ virtual-machine :~ # Ls nosuchfile 2> a.txt
-Bash: a.txt: The existing files cannot be overwritten.
Root @ virtual-machine :~ # Cat a.txt
123
Root @ virtual-machine :~ # Ls nosuchfile 2> | a.txt
Root @ virtual-machine :~ # Cat a.txt
Ls: unable to access nosuchfile: No file or directory

  • <> File: use file as both input and output.
It is mainly used for device files, that is, hardware devices that correspond to terminals and communication lines. Underlying system programmers can use it to test the device drive. Otherwise, it is not very useful.
  • N <> file: open the file in read/write mode, and redirect n to the file.

To describe the redirection, first describe the exec command:

Shell's built-in command exec will not start a new shell, but will replace the current shell process with the command to be executed, and the environment of the old process will be cleared, and the exec command

Other commands will not be executed. Therefore, if you execute exec ls in a shell, the shell will exit after the current directory is listed.

The shell process has been replaced by a process that only executes the ls command. After the execution ends, the shell process exits. However, pay attention to one exception. When the exec command operates on the file descriptor

The shell will not be replaced, and the subsequent commands will be executed after the operation is complete.

  1. $ Exec 3 <> file
  2. $ Ls> & 3
  3. $ Cat file
  4. File
  5. $ Cat <& 3
  6. $ Exec 3 <> file
  7. $ Cat <& 3
  8. File
After the file descriptor 3 is directed to file and the standard output is directed to 3, the standard output content is written to file. Similarly, when reading from the standard input, the standard input is directed

To 3, the file content is printed. The preceding result shows that exec 3 <> file is only valid for one command.

  • N> file, open the file in writing, and redirect n to the file.

For example:

Root @ gmdz-virtual-machine :~ /Test # exec 3> file
Root @ gmdz-virtual-machine :~ /Test # ls> & 3
Root @ gmdz-virtual-machine :~ /Test # cat file
A
B
Root @ gmdz-virtual-machine :~ /Test # cat <& 3
Cat:-: incorrect file descriptor

  • N <file, open the file in read format, and redirect n to the file.

For example:

Root @ virtual-machine :~ /Test # cat file
Test
Root @ virtual-machine :~ /Test # exec 3 <file
Root @ virtual-machine :~ /Test # ls> & 3
Ls: write error: incorrect file descriptor
Root @ virtual-machine :~ /Test # cat <& 3
Test
Root @ virtual-machine :~ /Test # cat <& 3

Root @ virtual-machine :~ /Test #

Similarly, exec 3 <file is only valid for one command.

  • <Label: here-document

<Label redirection forces the input of a command to use the standard input of shell, and reads only lines containing label. The input is called here-document.

For example:

# Cat> msgfile <EOF
> 1
> 2
> 3
> EOF
Root @ gmdz-virtual-machine :~ /Test # cat msgfile
1
2
3

Redirection

Root @ virtual-machine :~ /Test # cat <EOF
> $ PWD
> EOF
/Root/test
Root @ virtual-machine :~ /Test # cat <"EOF"
> $ PWD
> EOF
$ PWD

The second variation is <-, which deletes the leading TAB from the here-document and label lines (but does not delete spaces ).

For example, the script content is as follows:
#! /Bin/bash
Cat <-EOF
123
456
EOF

After execution, the output is:

123
456
  • N> file: redirects the file descriptor n to the file. If the file exists, it is appended to the end. For example, command 2> file
  • N> &: copy the standard output to the file descriptor n
  • N <&: Copy standard input from file descriptor n
  • N> & m: Make file descriptor n a copy of output file descriptor m.

For example:

Root @ virtual-machine :~ /Test # cat file
Test
Root @ virtual-machine :~ /Test # ls nosuchfile
Ls: unable to access nosuchfile: No file or directory
Root @ virtual-machine :~ /Test # ls nosuchfile> file 2> & 1
Root @ virtual-machine :~ /Test # cat file
Ls: unable to access nosuchfile: No file or directory

  • N <& m: Make file descriptor n a copy of input file descriptor m
  • &> File: Specify standard output and error output to file.

For example:

Root @ virtual-machine :~ /Test # ll a nosuchfile &> file
Root @ virtual-machine :~ /Test # cat file
Ls: unable to access nosuchfile: No file or directory
-Rw-r -- 1 root 0 2014-08-01 09:09

  • <&-: Disable Standard input.

For example, the script is as follows:

Exec <&-
Cat

Execution result:

#./A. sh
Cat:-: incorrect file descriptor
Cat: Disable Standard Input: incorrect file descriptor

  • > &-: Disable Standard output

For example, the script is as follows:

Exec> &-
Echo "test"

Execution result:

#./A. sh
./A. sh: 2nd rows: echo: write error: incorrect file descriptor

  • N> &-: Disable output from file descriptor n

For example:

Root @ gmdz-virtual-machine :~ # Ls
A. sh file test

Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Ls> & 3

Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Exec 3> &-
Root @ virtual-machine :~ # Ls> & 3
-Bash: 3: incorrect file descriptor

  • N <&-: Disable input from file descriptor n

For example:

Root @ gmdz-virtual-machine :~ # Exec 3 <> file
Root @ gmdz-virtual-machine :~ # Cat <& 3
A. sh
File
Test
Root @ virtual-machine :~ # Exec 3 <> file
Root @ virtual-machine :~ # Exec 3 <&-
Root @ virtual-machine :~ # Cat <& 3
-Bash: 3: incorrect file descriptor


2. String I/O2.1.echo

Echo simply prints the parameters to the standard output. The following table lists the options for echo to receive dashes.

-E: Explanation of escape characters with diagonal lines

-E: Disable the interpretation of escape characters on the backslash of the subsystem. This is the default mode.

-N: the last line feed is omitted.

For example:

Root @ virtual-machine :~ # Echo-e "a \ ta"
A
Root @ virtual-machine :~ # Echo-E "a \ ta"
A \ ta
Root @ virtual-machine :~ # Echo-n "a \ ta"
A \ taroot @ virtual-machine :~ #


2.2.read

Read allows you to read values to shell variables. The basic syntax is:

Read var1 var2...

This statement receives a line from the standard input and forms words separately. Words are separated by any character in the value of the environment variable IFS. The word is assigned to the variable.

Var1, var2...

For example, the content of a. sh is:

Read v1 v2
Echo $ v1
Echo $ v2

Execution result:

#./A. sh
123 456
123
456

If there are more words than variables, the additional words are assigned to the final variables. If you omit the variables, the entire input is assigned to the REPLY variable.

For example, execute the above script again:

#./A. sh
1 2 3 4
1
2 3 4

Modify a. sh:

Read
Echo $ REPLY

The execution result is:

#./A. sh
1 2 3 4
1 2 3 4


The following describes how to read variables from a file.

For example, the file content is as follows:

Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2

Method 1:

Use the following a. sh script to read the variables:

While read v1 v2; do
Echo $ v1 $ v2
Done

Execution result:

#./A. sh <file
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2


Method 2:

Modify a. sh:

While read v1 v2; do
Echo $ v1 $ v2
Done <file

Execution result:

#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2


Method 3:

Modify a. sh:

Func (){
While read v1 v2; do
Echo $ v1 $ v2
Done
}
Func <file

Execution result:

#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2


Method 4:

Modify a. sh:

{
While read v1 v2; do
Echo $ v1 $ v2
Done
} <File

Execution result:

#./A. sh
Arg1-1 arg1-2
Arg2-1 arg2-2
Arg3-1 arg3-2


Read has three common options:-a,-p, and-r.

-A allows you to read values into an array. Each continuous entry is assigned to a given array starting with 0. For example, the content of a. sh is as follows:
Read-a people
Echo $ {people [0] }$ {people [1] }$ {people [2]}

Execution result:

#./A. sh
1 2 3
1 2 3

-P option prints the string before reading the input. For example:

[Root @ yanPC ~] # Read-p "dir? "Dirname
Dir? /Home
[Root @ yanPC ~] # Echo $ dirname
/Home

The-r option does not ignore the "\" character when reading the input.

If "\" is inside the input string:

[Root @ yanPC ~] # Read dirname
123/456
[Root @ yanPC ~] # Echo $ dirname
123456
[Root @ yanPC ~] # Read-r dirname
123/456
[Root @ yanPC ~] # Echo $ dirname
123/456

If "\" is at the end of the input string:

[Root @ yanPC ~] # Read dirname
123 \
> 456
[Root @ yanPC ~] # Echo $ dirname
123456
[Root @ yanPC ~] # Read-r dirname
123 \
[Root @ yanPC ~] # Echo $ dirname
123 \


The power cord of the laptop is V input/output. Can I change it to V input/output?

Yes. No problem.

Optical fiber input and output problems in the notebook

You need an amplifier that supports the input of the optical fiber audio source signal, which is decoded and amplified by the amplifier and reproduced by the speaker. The optical fiber transmission is mainly used to reduce interference and attenuation during transmission.

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.