BASH entry foundation

xiaoxiao2021-03-06  120

The shell is then trying to find these applications in the search path. Search path is a list of directory that can find executable programs. If you type it is not an internal command and does not find this executable file in the path, an error message will be displayed. If the command is found successfully, the SHELL internal command or application will be decomposed into a system call and pass it to the Linux kernel. Another important feature of the shell is that it itself is an interpreted programming language. The shell programming language supports the vast majority of program control structures that can be seen in advanced languages, such as loops, functions, variables, and arrays. Shell programming language is easy to learn, and once it masters it, it will become your strength tool. Any command that can be typed in a prompt can also put it in an executable shell program, which means that a task can be easily repeated with the shell language. How to start the shell after you successfully log in into the system, and always use the interaction with the system kernel until you exit the system. Each user on your system has a default shell. The default shell of each user is specified in the passwd file in the system, the path of the file is / etc / passwd. There is anything else in the Passwd file: each person's user ID number, a password encrypted copy and the program executed immediately after logging in, (Note: In order to strengthen security, the current system generally puts the encrypted password Another file - Shadow, and the part of the password in Passwd is replaced by an X-character) Although this program must be a Linux Shell, most of the cases. The most common shell can use a variety of different shells in Linux and UNIX systems. Several of the most commonly used Bourne Shell (SHELL (CSH), and Korn Shell (KSH). Three shells have their advantages and disadvantages. The Bourne Shell is the Steven Bourne. It is the original use of UNIX and can be used on each UNIX. Bourne Shell is quite excellent in shell programming, but it is not as good as many other shells in handling and user interaction. C shell Written by Bill Joy, which considers the friendlyness of the user interface. It supports features that are not supported by the Bourne Shell such as a command complement (COMMAND-LINE Commention). It is not as good as Bourne Shell, but C shell is used by many C programmers because C shell's syntax and C language are very similar because C shell's syntax and C language are also the origin of the c shell name. Korn Shell (KSH) is written by Dave Korn. It combines the advantages of C shell and Bourne Shell and is fully compatible with the Bourne Shell. In addition to these shells, many other shell programs have absorbed the advantages of these original shell programs and become new shells. There is a common TCSH (CSH extension), Bourne Again Shell (extension of PDKSH, KORN SHELL (Extension of PDKSH, KORN SHELL). Bash is the default shell of most Linux systems. The Bourne Again Shell Bourne Again Shell (BASH), as its name is implied, is the expansion of the Bourne Shell. Bash is fully compatible with Bourne Shell and has increased and enhanced many features on the basis of Bourne Shell. BASH also includes the advantages of many C and Korn shells.

Bash has a very flexible and powerful programming interface, while also a very friendly user interface. Why use BASH instead of SH? The biggest disadvantage of Bourne Shell is that it handles users' input. Type commands in the Bourne Shell, especially when you type a lot of similar commands. BASH prepared several features make the command input easier. Command-line completion, you don't have to enter the command to enter the command to entroy when you enter the command. For example, suppose the current working directory contains the following files and subdirectory: news / bin / games / mail / sample file test / if you want to enter the test subdirectory, you will enter the following command: CD test This command can meet your needs. But BASH also provides a slightly different ways to complete the same thing. Because Test is the only subdirectory in the current directory, Bash can judge what you want after you only enter the letter t: CD T After you type the letter, the only possibility is Test. Want Bash to help you end the command, press the Tab button: CD T When you do this, Bash will help you upset the command and display it on the screen. But when you press the Enter key, the command is not executed, BASH will make you inspected whether the commands are really needed. When you enter a short command like this, you may not see its value, and even simply enter the speed in the command, but when you want to enter the command, you will find how this feature is. Beautiful.

But what happens when there is more than one file that starts with the letter T? There is a problem when you use the command to make up, let's take a look at the situation below, the current directory has the following:

News / Bin / Mail / Samplefile Test / Tools / Working /

There are two files that start with letters T now. Suppose you still want to enter the Test subdirectory, how to use the command to make up? If you type it like previous:

CD T

Bash will not know which subdirectory you want to enter, because the information given is not unique. If you do this, Bash will send a buzz to remind you that there is not enough information to make up your command. Beep, Bash does not change the input command, which will enable you to enter more information on the original basis. In this example, you only need to type an E and press the Tab button, then Bash Have enough information to complete your command:

CD test

When you enter the command, you will press the TAB button, Bash will try to make up the command, and you will send a beep to remind you to need more information. You need to type more characters and press the Tab key again to repeat this process until you expect.

Wildcard

Another way to make the command input easier is to use wildcards in the command. Bash supports three wildcards:

* Match any character and any number of characters

? Match any single character

[...] matches any single character contained in parentheses

* The use of wildcards Some icon commands are complement. For example, assume that the current directory contains the following files:

News / bin / Games / Mail / Samplefile TEST /

If you want to enter the Test directory, you will type CD test, or you want to use the command to make up:

CD T

There is now a third method to do the same thing. Because only one file begins with the letter T, you can use * wildcards to enter this directory. Type the following command: CD T *

* Match any characters and any number of characters, so the shell will replace T * with Test (the only file that matches the unique and wild schemes in the current directory). There is only one file in the current directory to start with the letter T, this will be reliable. However, if there is more than one file in the current directory, the shell will try to enter the first directory that meets the matching scheme. This directory is the first directory sorted alphabet, this directory may be Maybe not what you expect. of.

A more practical use of wildcard * is a file similar to multiple names you want to execute. For example, assume that the current directory contains the following files:

CH1.DOC Ch2.Doc Ch3.Doc Chimp Config Mail / TEST / TOOLS /

If you need to print all extensions, you can use the simplified command like this:

lpr * .doc

In this example, BASH will replace * .doc to match all file names in the current directory that meets the works. This command will be processed after BASH.

Lpr ch1.doc ch2.doc ch3.DOC

The LPR command will be called as ch1.doc, ch2.doc, and ch3.doc as parameters.

-------------------------------------------------- ------------------------------

Note: In addition to the examples given above, there are several ways:

Lpr * DOC

Lpr * OC

Lpr * c

-------------------------------------------------- ------------------------------

Wildcards? In addition to only match a single character, other functions are the same as wildcard *, if you use wildcard characters to print all extensions in the previously mentioned file, type the following command:

LPR ch? .doc

Wildcard [...] can match the characters or character range given in parentheses. Similarly to the previous directory as an example, print all extensions in that directory is the file file, you can type one of the following commands:

LPR CH [123] .doc

or:

LPR CH [1-3] .doc

Command history

BASH also supports command history. This means that BASH retains a certain number of commands you have previously entered in the shell. This number depends on a variable called HISTSIZE. For more information on HISTSIZE, please see the "Bash Variable" section later in this article.

Bash saves your previous command text in a historical list. When you log in with your account, the historical list will be initialized according to a historical file. The file name of the historical document is specified by a Bash variable called Histfile. The default name of the historical document is .bash_history. This file is usually in your user directory. (Note that the file name of the file is starting with a period, which means it is implied, only when you take -a or the LS command column directory of the -a or -a parameter)

Only the previous command has a history file, so BASH provides several ways to call them. The easiest way to use the history list is to use the upper orientation. The last typing command will appear on the command line after pressing the upper arrow keys. Then press one down, the second command will appear, and so on. If the upper flour, you can also turn over the down direction. (Like DOS Utilities DOSKEY) If needed, the history command displayed on the command line can be edited.

Another way to use the command history file is to display and edit the history command with the BASH internal command History and FC (Fix Command) commands. The History command can be called in two different ways. The first is: history [n]

When the History command does not have a parameter, the content of the entire history command list will be displayed. Here is an example of a list of command history:

1 MKDIR / USR / GAMES / POL

2 cp xpooltable-1.2.linux.tar.z / usr / games / pool

3 CD / USR / GAMES / POOL /

4 ls

5 gunzip xpooltable-1.2.linux.tar.z

6 Tar -xf XPoolTable-1.2.Linux.tar

7 LS

8 CD XPool

9 ls

10xinit

11 EXIT

12 Which Zip

13 ZIP

14 more readme

15 vi readme

16 exit

The role of using N parameters is that only the last N history commands will be listed. For example, History 5 displays the last five commands.

The second method of calling the history command is used to modify the contents of the command history list file. The syntax of the command is as follows:

History [-r | w | a | n] [filename]

In this form, the -r option tells the History command to read the content of the historical list file and treat them as the current command history list. The -w option will write the current command history to the file and override the original content. The -a option adds the current command history to the file. The -n option reads the content in the file and add it to the current history command list. If the FileName option is not specified, the History command will replace the value of the variable Histfile.

The FC command can use two ways to edit the history command. The first use of the following syntax:

FC [-e editor_name] [-n] [-l] [-r] [first] [last]

All parameters here are optional. The -e editor_name option is used to specify a text editor used to edit commands. The first and last options are used to select the range of historical commands, or both numbers or strings. The -n option disables the number of the commands. -r option reverse lists the matching commands. The -l option puts the matching command line on the screen (instead of in the editor). If the -e editor_name parameter is not specified, the value of the variable fcedit is replaced. If the variable does not exist, the value of the variable editor is replaced, and the VI editor will be used without existence.

Alias

Another way to make your work makes your work. Command alias is usually an abbreviation for other commands to reduce keyboard input. For example, you often want to type the following command, you may tend to build an alias for it to reduce workload:

CD / USR / X11 / LIB / X11 / FVWM / Sample-Configs

If you create an alias called GoConfig for this long command, type the following command at the BASH prompt:

Alias ​​Goconfig = 'CD / USR / X11 / LIB / X11 / FVWM / Sample-Configs'

Now, unless you quit Bash, type GoConfig will have the same role as the original long command. If you want to cancel alias, you can use the following command:

UNALIAS GOCONFIG

This is some alias that many users think useful, you can write them into your .Profile file improved work effect:

Alias ​​ll = 'ls -l'

Alias ​​log = 'logout'

Alias ​​ls = 'ls -f'

If you are a DOS user and get used to the DOS command, you can define the following alias to make your Linux performance like DOS: Alias ​​Dir = 'ls'

Alias ​​Copy = 'CP'

Alias ​​rename = 'mv'

Alias ​​MD = 'MKDIR'

Alias ​​rd = 'rmdir'

-------------------------------------------------- ------------------------------

Note: When defining an alias, the two heads of the equal sign cannot have spaces, otherwise shell cannot decide what you need to do. Quotation marks only when you contain spaces or special characters in your command.

-------------------------------------------------- ------------------------------

If you type an alias command without any parameters, all defined alias will be displayed, for example:

Alias ​​Dir = 'ls'

Alias ​​ll = 'ls -l'

Alias ​​ls = 'ls -f'

Alias ​​MD = 'MKDIR'

Alias ​​Net = 'Term / dev / modem 2> / dev / null &'

Alias ​​rd = 'rmdir'

Input redirection

The input redirection is used to change the input source of a command. Some commands need to enter enough information in the command line to work. For example, RM, you must tell RM in the command line you want to delete the file. Other commands require more detailed input, and the input of these commands may be a file. For example, the command WC statistics enters the number of characters, words, and number of lines in the files in the file. If you only type WC on the command line

WC will wait for you to tell it what to count, then Bash is like dead, you typed everything appears on the screen, but what will not happen. This is because the WC command is collecting input for ourselves. If you press Ctrl-D, the result of the WC command will be written on the screen. If you enter a file name to do parameters, like the following example, the WC will return the number of characters, words, and number of lines included in the file:

WC Test

11 2 1

Another way to pass the Test file content to the WC command is to redirect the input of the WC.

WC

11 2 1

Input redirects are not frequently used because most commands specify the file name of the input file on the command line in the form of parameters. Despite this, when you use a command that does not accept the file named input parameters, the input required is in an existing file, you can use the input redirection to solve the problem.

Output redirection

Output redirection is more common than input redirection. The output redirect allows you to redirect an output of a command to a file instead of display on the screen.

This feature can be used in many cases. For example, if there is a lot of output, you can't display it on the screen. You can redirect it into a file, and then use the text editor to open this file later; when you want to save the output of a command This method can be used. Also, the output redirection can be used to use the output of a command as the input of another command. (There is also a simpler way to use the output of a command as an input to another command, that is, use the pipeline, the use of the pipeline will be referred to the use of the output redirection of the "pipe" section) output redirection Similar, but the output redirected symbol is>.

-------------------------------------------------- ------------------------------

Note: The best way to remember the input / output redirect symbol is to point it as a funnel, and the funnel points point to the command you need to enter (because the command you need to accept will be at the left hand side), and put> A big mouth points to the funnel of the command with the output.

-------------------------------------------------- ------------------------------

Redirect an example, when you want to save the output of the ls command as a file named Directory.out, you can use the following command:

LS> Directory.out

pipeline

The pipe can connect a series of commands. This means that the output of the first command is passed to the second command through the pipe, and the output of the second command will be used as the input of the third command. The output of the last command in the pipeline will only appear on the screen (if the output redirection is used in the command line, it will be placed in a file).

You can build a pipeline by using pipelines |, the following example is a pipeline:

CAT Sample.Text | GREP "High" | WC -L

This pipe will give the output of the CAT command (listed on the content of a file) to the GREP command. The grep command finds the word high in the input, the output of the grep command is all rows that contain the word high, and this output is sent to the WC command. The WC command with the -l option will count the number of rows in the input. Suppose the contents of Sample.txt are as follows:

THINGS to do Today:

Low: Go Grocery Shopping

High: Return Movie

HIGH: CLEAR Level 3 in Alien vs. Predator

Medium: Pick Up Clothers from Dry Cleaner

The pipeline will return results 2, pointing out that there are two important things you have today:

CAT Sample.Text | GREP "High" | WC -L

2

Prompt

BASH has two-level user prompt. The first level is the prompt that you often see when waiting for the command to enter. The default level prompt is a character $ (if it is a super user, ##). You can change your default prompt by changing the value of the BASH PS1 variable, for example:

PS1 = "please enter a command"

Take the Bash Shell's prompt to the specified string.

When BASH looks forward to entering more information to complete the command, the second level prompt is displayed. The default second level prompt is>. If you want to change the second level prompt, you can implement the value of the PS2 variable:

PS2 = "I NEED More Information" In addition, you can also define your prompt with special characters, the following list lists the most common special characters.

Prompt Special Character Code

Character meaning

/! Display the history number of the command.

/ # Display the command number of the current command.

/ $ The display is as a prompt. If the user is root, the ## is displayed.

// Show a backslash.

/ d shows the current date.

/ h Shows the host name.

/ n Print a new line.

/ NNN displays an octave value of NNN.

/ s shows the name of the current running shell.

/ t shows the current time.

/ u Shows the username of the current user.

/ W Displays the name of the current work directory.

/ w Displays the path to the current work directory.

These special characters can be combined into many useful probes (which can also be combined into a very singular scheme), such as setting PS1:

PS1 = "/ t"

This causes the prompt to display the current time, just like the display below (the prompt will not have spaces):

02:16:15

And below:

PS1 = / T

Will cause the prompt to become the following:

t

This shows the importance of setting the quotation marks, the following prompt string:

PS1 = "/ t //"

Will make the prompt look like this:

02: 16: 30 /

In this case, there will be a space behind the prompt because there is a space in quotation marks.

Job Control

The job control controls the behavior of the current running process. In particular, you can hang a running process and restore its operation later. BASH keeps track of all launched processes, you can hang it or turn it to run in any time of a running process.

Press CTRL-Z to hang a running process. The bg command enables a hang-haunken process to restore running in the background, and the counter FG command enables the process to recover run at the front desk. These orders are often used when the user wants to run it in the background. When a command is run at the front desk, it disables the user from interacting with the shell until the command ends. This usually doesn't cause trouble, because most commands have been implemented soon. If you want to run the command to spend a long time, we usually put it in the background so that we can continue to enter other commands at the front desk. For example, you enter this command:

Command Find / -Name "Test"> Find.out

It will look for files named TEST over the file system and save the results in a file called fing.out. If you run at the front desk, depending on the size of the file system, your shell will have few seconds or even minutes, you don't want this, you can enter the following content:

Control-z

BG

The Find command is first hang, and then continue to be executed in the background, and you can go back to BASH immediately.

User-configured Bash

This article has been described in many ways to configure Bash. But know now, what we do is only valid under the current running Bash. Once the system exits the system, all changes have also disappeared. In order to save these customized configurations, you must save them to an initialization file of a BASH.

You can put any commands that you want to perform every time you enter Cash in the initialization file. The most common commands in this file are usually an initialization of Alias ​​and variables. Bash's initialization file is called Profile. Every user who uses Bash has a .profile file in his user directory. BASH reads this file at each startup and performs all included commands. The following code is the content of the default .profile file. The location of this file is in the / etc directory. If you want to set your own bash, you will copy it to your user directory (if you haven't yet,) and name it .profile.

-------------------------------------------------- ------------------------------

Note: Some SETUP programs will automatically put a copy of a .profile file when establishing a user in your user's directory. But not all do this, so it's best to check your user's directory first. Remember all files starting with a sentence, which is implied, only listed in ls -a or ls -a commands.

-------------------------------------------------- ------------------------------

# Commands Common To All logins

EXPORT OpenWinHome = / usr / openwin

Export minicom = "- c on"

Export manpath = / usr / local / man: / usr / man / preformat: / usr / man: / x11 / man: / usr / openwin / m

AN

Export hostname = "` cat / etc / hostname` "

Path = "$ PATH: / USR / X11 / BIN: $ OpenWinHome / bin: / usr / games :."

Less = -mm

# I had problem 'Eval test' instead of 'term =', but you might want to # TRY

I think with the right / etc / termcap it 10 Work Great. # Eval 'Tset

-SQ "$ TERM" 'IF ["$ TERM" = "" "$ term" =

"unknown"]; then

Term = Linux

# Ps1 = '' Hostname ':' pwd '# `

IF ["$ shell" = "/ bin / pdksh" -o "$ shell" = "/ bin / ksh"]; then

PS1 = "! $"

Elif ["$ shell" = "/ bin / zsh"]; then

PS1 = "% m:% ~% #"

Elif ["$ shell" = "/ bin / ash"]; then

PS1 = "$"

Else

PS1 = '/ h: / w / $ `

Fi

PS2 = '> `

Ignoreeof = 10

Export Path Display Less Term PS1 PS2 ignoreeof

Umask 022

# Set up the color-ls Environment variables: IF ["$ shell" = "/ bin / zsh" l; then

Eval 'Dircolors -z'

Elif ["$ shell" = "/ bin / ash" l; then

Eval 'Dircolors -s'

Else

Eval 'DIRCOLORS -B'

Fi

echo

Fortune

echo

Export Tape = "/ dev / nftape"

Bash command summary

This is a few most useful Bash internal commands:

Alias: Set the Bash alias.

BG: Make a hang-haunted process continues to execute in the background.

CD: Change the current working directory.

EXIT: Terminate the shell.

EXPORT: Make the value of the variables to all the sub-process of the current shell.

FC: The command used to edit the list of history commands.

FG: Make a hang-haunged process continues to execute at the front desk.

Help: Displays help information for the BASH internal command.

Kill: Terminate a process.

PWD: Displays the current work directory.

Unalias: Delete the defined alias.

Bash has many commands, but these are most common, want to know more detailed, please refer to Bash's Manual - Type Man Bash at the prompt.

Bash variable

Here is a few most useful Bash variables, including variable names and brief description.

Editor, Fcedit: The default editor of the BSAH FC command.

Histfile: Files used to store historical commands.

Histicsize: The size of the history command list.

HOME: The current user's user directory.

OldPWD: The previous working directory.

PATH: BASH looks for the search path for executable.

PS1: The command line level prompt.

PS2: The secondary prompt of the command line.

PWD: The current work directory.

Seconds: The number of seconds passing after the start of the shell.

转载请注明原文地址:https://www.9cbs.com/read-126725.html

New Post(0)