Sobell talks about the fate of bash and Linux command lines

Source: Internet
Author: User
Article title: Sobell talks about the fate of bash and Linux command lines. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

[Introduction] Mark G. Sobell, author of "Linux command, editor and shell programming practice guide", talked about his own views on the fate of Linux command lines in an interview with LinuxPlanet.

  LinuxPlanet (LP): is the command line dead?

A: No, there is no death at all. For some people and certain tasks, it is easier and more concise to use the GUI. This actually depends on what you want to do and who you are. The differences between the graphic user interface and the command line are just like the automatic shift and shift lever. I use a shift lever because it gives me better control over cars, and gives me more experience about what cars are doing and how they are doing.

Of course, this discussion assumes that you operate files at the system administrator level. Some applications have a GUI, some applications may not have this interface, or they only have a very primitive command line interface. It makes no sense to try to run these applications from the command line.

One thing that is good for command line is that it allows you to prepare hundreds of tool software. On the command line interface, you can use a pipeline to combine the tool software to execute a task that cannot be completed by a single tool software. The following is an excerpt from my book "Linux command, editor, and shell programming practice guide". I have discussed these pipelines and their connection processes:

"One process is to execute a command in Linux. Communication between processes is one of the proofs of UNIX/Linux verification. A channel (written as a vertical line "|", which is a vertical line in the command line or on the keyboard) provides the simplest way to communicate with each other. Simply put, a channel accepts the output of a tool software and then inputs the output to other tool software. Using the UNIX/Linux vocabulary, this channel accepts the standard output of a process and uses the standard output as the standard input of another process. Most of the content displayed on the screen in a process will be sent to the standard output. If you do not redirect the output, it will be displayed on the screen. With one channel, you can redirect this output so that it becomes the standard input of another tool software ."

For example, you can combine the "ls" command for listing files in a directory with the "wc-w" command for calculating files and words in a directory:

$ Ls | wc-w

45

In the field of Linux system management, the user Gui is usually created outside the command line tool. Therefore, you cannot get the benefits of the user GUI tool. Unless you can use the mouse. The work you can do under the command line is often not completed in the graphical user interface system management tools.

 Bourne and Bourne shell

LP: Can you discuss bash (Bourne Again Shell) and explain its differences with the original Bourne Shell program?

A: This shell is the command line interpreter. it analyzes the command line you entered and calls the program you applied, and pass the parameters you entered in the command line to this program. This shell is also an advanced programming language. Bash is the default shell program for many Linux systems. Most Linux releases also contain other shell programs, and even more shell programs for download.

Bash, compiled by the GNU program, contains the original version of the Bourne shell, which is the first shell under UNIX published by AT&T. I have suggested that readers consider using the C shell program as their interactive shell program because it has some important features not available in the original version of the Bourne shell program. Currently, bash has all these functions, and some bash also includes command completion and history (so that you can edit and repeat previous commands) and work control (allowing you to transfer work between the front-end and back-end. Of course, you can use bash to write shell scripts (batch files ).

Many Linux shell scripts are from "#! /Bin/sh. This command allows the script to run in the shell. This shell is not part of the Bourne shell, but a link to bash.

With a long and successful history, the original Bourne shell program has been used to write many shell scripts that help manage Unix systems. Some scripts in Linux are called bash scripts. Although the bash script contains many of the extended features and features not available in the original Bourne shell, bash maintains compatibility with the original Bourne shell, therefore, you can run the Bourne shell script under bash. The original Bourne shell is called sh in Unix systems. In Linux, sh is a symbolic link to bash to ensure that scripts of the Bourne shell are run. When it is called sh, bash tries its best to follow the original Bourne shell program.

LP: do you recommend Linux beginners learn bash or TC shell programs?

A: If you are a stubborn C-language shell programmer, you can continue to use the TC shell program (tcsh ). Otherwise, we recommend that you use bash. Almost all script programs that control Linux Management shell are run by bash. Therefore, if you learn bash, you can easily understand and modify these scripts.

Awk

LP: Why do you use awk?

A: This is a good question, especially when many people use the Perl language directly. This tool is simple and powerful. Before Perl appeared, awk was always one of the tools used to operate files. Currently, awk is still useful. The GNU version of awk is called gawk and has some new functions to make it a very useful tool. The following section describes how to make gawk and the collaboration process communicate with each other:

Collaboration Process: Bidirectional I/O

A collaborative process is a process that runs in parallel with another process. Starting from version 3.1, gawk can start a collaboration process to directly exchange information with the background process. When you work in a client/server environment and set up an SQL front-end and backend, or exchange data with a remote system on a network, the collaboration process is very useful. Gawk syntax identifies a assisting process by adding an operator "| &" before the program name that starts the background process.

A helper process command must be a filter (that is, it reads standard input and writes standard output) and must be refreshed after a line of output is completed, instead of accumulating a lot of rows and then outputting them. When an instruction is started as a collaboration process, it connects to a gawk program through a two-way channel, so that you can read and write the collaboration process.

When used together with the tr tool, this tool does not refresh its output after each line of commands is completed. The "to_upper" shell script does not refresh the shell of the output tr command. This filter can run as a collaboration process. For each line of instruction read, "to_upper" writes these lines and translates these lines into uppercase letters and standard output. If you want to_upper to display debugging output, you can delete "#" before "set-x".

$ Cat to_upper

#! /Bin/bash

# Set-x

While read arg

Do

Echo "$ arg" | tr '[a-z] ''[A-Z]'

Done

$ Echo abcdef | to_upper

ABCDEF

The g6 program starts "to_upper" as a collaboration process. The gawk program reads standard input or a file specified in the command line, translates the input into uppercase letters, and writes the translated data into a standard output.

$ Cat g6

{

Print $0 | & "to_upper"

"To_upper" | & getline hold

Print hold

}

$ Gawk-f g6 <alpha

AAAAAAAAA

BBBBBBBBB

CCCCCCCCC

DDDDDDDDD

The g6 program contains a mixed instruction in parentheses, which contains three instructions. Since there is no execution method, gawk executes this hybrid command once for each line of input content.

The first command "print $0" sends the current record to the standard output. The "| &" operator points the standard output to a program named "to_upper. "To_upper" is running as a collaboration process. An external bracket is required for these programs. The second command points the standard output from to_upper to a new "getline" command. This command copies the standard output to the variable named "hold. The third instruction "print hold" sends the content of the "hold" variable to the standard output.

The tool name is "tr"

 

A: Oh, tr, OK. The first thing that comes to mind is that this is the answer to a trivial question. Name a Linux tool. This tool only receives input from standard input and never receives input from files from command line variables. This monster is only useful sometimes, but it is very useful when it is useful. The following is an excerpt about "tr:

The tr tool reads each character entered in the standard input, converts the character image to an alternative character, deletes the original character, or removes the character. This tool reads standard input and writes it to standard output.

The tr tool is generally used with two parameters, string1 (String 1) and string2 (string 2 ). The position of each character in the two strings is very important: every time tr finds a character in string1, it replaces the character with the corresponding character in string2.

If you use the options of string1 and -- delete, tr deletes the characters specified in string1. This "squeeze-repeats" option replaces consecutive occurrences in string1 with an existing character. for example, abbc is changed to abc.

You can use a hyphen to represent a series of characters in inging1 or string2. The two command lines generate the same result in the following example:

$ Echo abcdef | tr 'abcdef' 'xyzabc'

Xyzabc

$ Echo abcdef | tr 'A-f'' x-za-C'

Xyzabc

The following example demonstrates how to hide text. This method is usually called "ROT13" (rotate 13) because it replaces the first letter with 13th letters, 14th letters instead of the second letter, and so on.

$ Echo The punchline of the joke is... |

> Tr 'A-M N-Z A-m n-z'' N-z A-M n-Z a-M'

Gur chapuyvar bs gur wbxr vf...

In order to make the text intelligent again, the order of parameters for tr is reversed:

$ Echo Gur chapuyvar bs gur wbxr vf... |

> Tr 'n'-Z A-M N-z a-m'' A-m N-Z a-M n-z'

The punchline of the joke is...

This "-- delete" option causes tr to delete the selected characters:

$ Echo If you can read this, you can spot the missing vowels! |

> Tr -- delete 'aeiou'

If y cn rd ths, y cn spt th mssng vwls!

In the following example, tr replaces several characters and generates the same pair of children as a single character: $ echo tennessee | tr -- squeeze-repeats 'tnse' 'srne'

Serene

In the next example, a new file character is used to replace each character that is not listed alphabetically in the draft1 file. The output is a vocabulary list with one word per line:

$ Tr -- complement -- squeeze-repeats '[: alpha:] ''n' <draft1

The last example is to use the character class to promote the string "hi there ":

$ Echo hi there | tr '[: lower:]' [: upper:]'

HI THERE

Summary

LP: Do you have any ideas?

A: I want to say that the command line does not apply to everyone. The command line applies to users who require hands-on operations and stronger control over the beasts they tame. Learning what Shell programs can do is half done. The other half of the success is to learn some of the many tools released with the Linux release. You do not need to know every parameter of each command. It is sufficient to understand the meaning of each instruction name and what each instruction can do. You can read about people and information in my book or instructions. Check the tac tool and you will get a laugh at the origin of the tool name at the beginning.

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.