Linux Shell Script Introduction Learning notes 1__linux

Source: Internet
Author: User
Tags stdin

One: Cat command

Cat (CONCATENATE) is one of the commands that the command line player must learn first, and he is usually used to read, display, and splice files, but the cat's ability is much more than that, with a line of commands to combine data from standard inputs and files, which is a scratching question. To see the basic usage and advanced usage of cat

(1) The general writing of reading the contents of a file with cat is:

      $ cat File1 file2 file2
This command will stitch together the contents of the file as arguments to the command line.

To print the contents of a single file:

To print the contents of multiple files:

Similarly, we can use cat to stitch together the content from the input file and the standard input, combining stdin with the data in another file. The method is as follows:

  $ echo "Text through stdin" | Cat-file.txt
Cases:

In the above code,-the file name that is used as stdin text


(2) Other uses of cat:

A) Get rid of excess blank lines

Sometimes a text file may contain multiple contiguous blank lines. If you need to remove these extra blank lines, you can use the following:

   $ cat-s File
Cases:

In addition, sometimes in order to distinguish multiple spaces and tabs, you can use the cat-T parameter to make the tab characters in the script become ^|, which makes it easy to debug

b) Display line number (-n parameter)

The-n option with the cat command adds the line number before each line of output. Don't worry, cat commands will never change your file, he just generates a modified output in stdout based on user-supplied options, such as:


Note :-n even adds line numbers to blank lines. If you want to skip a blank line, you can use the option-B.


Second: Recording and replaying a terminal session

When you need to demonstrate something for someone else on the terminal or need to prepare a command-line tutorial, you usually have to do it by manually typing a command, or you can record a video of a screen demo, and then replay it, but there are other ways to do it, using script and Scriptreplay Command , we can record the order and timing of orders, recording the relevant data in a text file. With these files, other people can replay and view the output of the command at the terminal.

Cases:

Start recording a terminal session

$ script-t 2> timing.log-a output.session

Type commands

.....

Exit

Two configuration files are used as parameters for the script command. One of the files (Timing.log) dares to store timing information, describe when each command is run, and another file (output.session) to store the command output. The-t option dares to import time-series data into stderr. 2> is brave to import stderr into Timing.log.

With this two-file Timing.log and output.session, we can replay the command execution process as follows:

$ scriptreplay Timing.log output.session #按播放命令顺序输出




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.