The 15th chapter of the Linux command line and Shell Scripting Encyclopedia presents data

Source: Internet
Author: User
Tags stdin tmp file

15.1 Understanding inputs and outputs

Now you know two ways to show script output

1) display on the display screen

2) redirect the output file to a file

15.1.1 Standard File descriptor

Each object is treated as a file by the Linux system. This includes the input and the process.

Linux uses file descriptors to identify each file object.

It is a non-negative integer that uniquely identifies the open file in the session.

Each process can have up to nine file descriptors at a time

Top 3 file descriptors (0, 1, 2) reserved by bash shell

1.STDIN standard input (0)

The stdin file represents the standard input for the shell.

For the terminal interface, the standard input is the keyboard.

The shell obtains input from the keyboard corresponding to the stdin file descriptor, processing each character as the user enters

When using the input redirection symbol (<), Linux replaces the standard input file descriptor with the specified file redirection. It reads the file and extracts the data as if it were typed on the keyboard.

2.STDOUT Standard output (1)

The stdout file descriptor represents the standard output of the shell.

For the terminal interface, the standard output is the terminal display. All output from the shell is directed to the standard output.

Output redirection (>) can also be used to change outputs. The output redirection symbol allows you to redirect output that would otherwise be displayed on the monitor to the specified file.

>> represents appending to a file

Note: With output redirection, the shell does not redirect error messages to the output redirection file. The error message will still appear in the display.

3.STDERR standard error (2)

stderr the file descriptor to handle the error message.

Error messages that are generated when programs and scripts run in the shell or shell are sent to this location.

By default Strout and stderr point to the same place (monitor). However, stderr does not change with stdout redirection.

15.1.2 REDIRECT Error

1. REDIRECT Only error

Place the file descriptor value (2) before the redirect symbol (>), which must be next to each other, with no spaces.

For example, to view a file that does not exist:

$ls –al 2> log.txt

This method only redirects the error message, and the normal output is not redirected.

2. REDIRECT Errors and data

You need to use two redirect symbols, you need to precede the symbol with the file descriptor that corresponds to the data being redirected, and then specify the output file to hold the data.

For example:

$ls-al test1 test2 test3 badfile 2> ErrLog.txt 1> DataLog.txt

Indicates that the error message is redirected to ErrLog.txt, and the normal output is redirected to DataLog.txt.

This error message and normal output are separated in two files.

$ls-al test1 test2 test3 badfile &> AllLog.txt

This means that stdout and stderr are redirected to the same file AllLog.txt.

The bash shell automatically gives the error message a higher priority so that it can focus on the error messages.

15.2 Redirecting output in scripts

There are two ways of doing this:

1) Temporary REDIRECT line output

2) All commands in the permanent redirect script

15.2.1 Temporary redirection

You can redirect a row to stderr separately.

Like what:

echo "This is the error msg" >&2

echo "This is normal msg"

Normal operation will not see anything, but it would be interesting if the runtime redirected the stderr.

$./test 2> Error.txt

You can see that the first line is output to Error.txt. The normal output is still on the screen.

15.2.2 Permanent Redirection

If there is a lot of data that needs to be redirected, it can be cumbersome.

New method: Use the EXEC command to tell the shell to redirect a specific file descriptor during script execution

Directly on the example:

1 #!/bin/bash

2 echo "This is the error msg step1" >&2

3 echo "This is normal msg Step1"

4 # There is no redirect above, so it is still on the screen output. To redirect to the desired file only below

5 exec 1>test2log.txt

6 exec 2>test2error.txt

7 echo "This is the error msg Step2" >&2

8 echo "This is normal msg Step2"

So once redirected, it's hard to change back.

15.3 redirect Input in script

The EXEC command allows you to redirect stdin to a file on a Linux system.

Example: viewing data in a test2

1 #!/bin/bash

2 exec 0< test2 # input redirected to test2

3 echo "Test2:"

4 count=1

5 while Read line

6 do

7 echo "$line"

8 count=$[$count + 1]

9 Done

15.4 Creating your own redirects

Previously said a process can be up to 9 open file descriptors. The other 6 (3 ~ 8) file descriptors can be used as input or output redirection.

You can assign any of these file descriptors to a file.

15.4.1 Creating output file descriptors

Assign a file descriptor to the output with the EXEC command.

As with the standard file descriptor, once another file descriptor is assigned to a file, the redirect will remain in effect until you reassign it.

Example:

1 #!/bin/bash

2 exec 3>test4log.txt # exec 3>>test4log.txt This is to append the output to the existing file

3 echo "This is Normal msg"

4 echo "This is Fd:3 msg" >&3

15.4.2 redirect File descriptor

Now describes how to recover redirected file descriptors.

You can assign another file descriptor to a standard file descriptor, and vice versa.

You can redirect stdout to another file descriptor, and then redirect back to stdout with that file descriptor

Example:

1 #!/bin/bash

2 # Storing STDOUT, then coming back to it

3 exec 3>&1 # 3 REDIRECT to stdout. means that 3 of the data will appear on the re-display

4 exec 1>test5log.txt # Redirects the stdout to a file. But 3 still points to the original position of the stdout, which is the display. 3 rounds will be displayed in the display. To the stdout , it will appear in the file.

5 echo "This should store in the output file"

6 echo "Alone with the".

7

8 exec 1>&3 # will stdout REDIRECT to 3 the current position (i.e. the monitor)

9 echo "Now things should is back to normal"

15.4.3 Creating an input file descriptor

Similar to the above, save stdin to another file descriptor, and then read the file before you restore stdin

Example:

1 #!/bin/bash

2 exec 6<&0 # 6 Save stdin First the location

3 exec 0<test5 # will stdin REDIRECT to Test5

4 count=1

5 while Read line

6 do

7 echo "$line"

8 count=$[$count +1]

9 Done

10

EXEC 0<&6 # after the read is completed, the stdin redirect to file descriptor 6 , which restores the previous position

Read-p "Is you do now?" Answer

Case $answer in

Y|y) echo "GoodBye!!!";;

N|n) echo "Sorry, this is the end";;

*) echo "Error End";;

Esac

15.4.4 creating read-write file descriptors

You can open a single file descriptor as input and output. You can use the same file descriptor to read and write to the same file.

Use caution : Because data is read and written to the same file, the shell maintains an internal pointer indicating the current position in the file. Any read or write starts at the last position of the file pointer.

Example:

1 #!/bin/bash

2 exec 3<> testfile

3 Read Line <&3

4 echo "Read : $line "# Note that this is written from the last position of the file pointer, that is, the position after reading a line

5 echo "Write:this is Test line" >&3

15.4.5 closing file descriptors

If you create a new input or output file descriptor, the shell automatically closes them when the script exits.

But at some point, you have to shut yourself down.

How to close: Redirect file descriptors to be closed to special symbols &-

Once closed, you cannot write data to him in the script, or the shell generates an error message.

Example:

1 #!/bin/bash

2 # Close FD test

3 exec 3>test8log.txt

4 echo "This was normal to Fd:3" >&3

5 exec 3>&-

6 echo "After close write:his are normal to Fd:3" >&3 # It's going to go wrong when you close it and write it .

7

8 exec 3>test8log.txt # This is the equivalent of reopening it.

9 echo "This is the bad normal to Fd:3" >&3 # will overwrite the original

15.5 List Open File descriptors

The lsof command lists all the file descriptors that are open for the entire Linux system. Produces a lot of output.

You can also connect options and parameters:

-P followed by the process to be viewed. $$ indicates that the current process

-D after specifies the file descriptor number to display.

Example:

1 #!/bin/bash

2 exec 3> testfile

3 lsof-a-P $$-D 0,1,2,3,4

15.6 Block Command output

Sometimes you don't want to show the output of the script. You can redirect the output to a special file called a null file.

Like what:

$ls –al >/dev/null

You can also empty the log file like this

$ cat/dev/null > TestLog.txt

15.7 Creating temporary files

Linux uses the/tmp directory to store files that do not require permanent retention. Most Linux distributions configure the system to automatically delete all files in the/tmp directory at startup.

Any user account on the system has permission to read and write files in the/tmp directory.

Mktemp can create a unique temporary file in the/tmp directory. Once the file is created, you have full read and write permissions in the script, and others cannot access it.

15.7.1 creating a local temporary file

Just specify a filename template and add 6 x to the end of the file.

$mktemp testing. Xxxxxx

Note: Be sure to have an uppercase x here. Here x is a bit of a wildcard meaning. And you can write not X.

The output of the Mktemp command is the name of the file it creates. Saved in the script, it can be referenced in a later script.

Example:

1 #!/bin/bash

2 # Create and using temp file

3 tempfile=$ (Mktemp test10. XXXXXX)

4 echo "tempfile = $tempfile"

5 exec 3> $tempfile

6 echo "This script writes to TMP file $tempfile"

7 echo "This was first line" >&3

8 echo "This was second line" >&3

9 echo "This was third line" >&3

Ten exec 3>&-

11

echo "Now delete file $tempfile"

Rm-f $tempfile >/dev/null

15.7.2 creating temporary files in the/tmp directory

The-t option forces mktemp to create the file in a temporary directory on the system.

This time returns the full path used to create the temporary file, not just the file name.

It would be nice to add –t to the above example.

。。。

tempfile=$ (Mktemp-t test10. XXXXXX)

。。。

15.7.3 Creating a temp directory

The-D option is used to create a temporary directory. This allows you to do whatever you want with the directory.

Example:

1 #!/bin/bash

2 # Create and using temp dir

3 tempdir=$ (mktemp-d test12dir.12xxxx)

4 CD $tempdir

5 echo this in dir:$ (PWD)

6 tempfile=$ (Mktemp test12. XXXXXX)

7 echo "tempfile = $tempfile"

8 EXEC 3> $tempfile

9 echo "This script writes to TMP file $tempfile"

echo "This is first line" >&3

echo "This was second line" >&3

echo "This was third line" >&3

15.8 Logging Messages

sending the output to both the monitor and the log file requires a special command tee to do so.

The tee command corresponds to the first T-connector of the pipe. It sends the STDIN data to two places at the same time, one is stdout, one is the specified file.

Like what:

$date | Tee Log.txt

$date | Tee–a Log.txt # This is appending the data to the file

Example:

1 #!/bin/bash

2 # Tee Test

3 echo "This is 1 msg" | Tee Test13log.txt

4 echo "This is 2 msg" | Tee-a Test13log.txt

5 echo "This is 3 msg" | Tee-a Test13log.txt

15.9 Example

File redirection is common when scripts need to be read into files and output files.

Requirements: Put data data into a spreadsheet (. csv file), read the file, and create an INSERT statement.

Example:

1 #!/bin/bash

2 outfile= ' Members.sql '

3 ifs=,

4 while read name age sex num

5 Do

6 Cat >> $outfile << EOF

7 INSERT INTO members (name, age, sex, num) values (' $name ', ' $age ', ' $sex ', ' $num ');

8 EOF

9 Done <${1}

1)${1} represents the first command-line argument. It indicates the file to read the data .

2) read uses the IFS character to parse the text, and here we specify the IFS as a comma.

Cat >> $outfile << EOF//This paragraph is still not very understanding

This includes an output append redirect (>>) and an input append redirect (<<).

>> appends the output of the cat command to the file specified by the $outfile variable.

The input to the Cat command is not taken from standard input, but is redirected to the data stored in the script.

The EOF symbol marks the beginning and end of the data appended to the file.

Input file + run + Result:

Description

Special redirects (here Document):

Command << delimiter

Document

Delimiter

The function is to pass the contents (document) between the two delimiter as input to the command

Note: the end of the delimiter must be shelf write, cannot have spaces .

(1)

6 Cat >> $outfile << EOF

7 INSERT INTO members (name, age, sex, num) values (' $name ', ' $age ', ' $sex ', ' $num ');

8 EOF

(2)

6 Cat << EOF

7 INSERT INTO members (name, age, sex, num) values (' $name ', ' $age ', ' $sex ', ' $num ');

8 EOF

The yellow highlight is passed to cat as input. (1) Redirect to OutFile, (2) Still standard output (screen)

The 15th chapter of the Linux command line and Shell Scripting Encyclopedia presents data

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.