What you need to know before you learn a shell script

Source: Internet
Author: User
Tags aliases echo command


Knowledge before learning shell scripts

1) Record command history

The commands we've knocked on, Linux will be recorded, and presets can record 1000 historical commands. These commands are saved in the. bash_history file in the user's home directory. One thing you need to know is that commands that run in the current shell are saved to the. bash_history file only when the user exits the current shell normally.

There is an interesting character associated with the command history that is "!" The There are several applications commonly used: (1)!! (Two consecutive "!" ), which represents the execution of the previous instruction, (2)!n (n here is a number), indicating that the nth instruction in the history of the command is executed, such as "!100" means the 100th command in the history of the Execution command; (3)! string (string greater than or equal to 1), such as!ta, Represents the last instruction in the command history to begin with TA.

The basics before you learn shell scripting


2) command and file name completion

At the beginning of this tutorial, I introduced this feature, remember? The right thing to do is to press the TAB key, which can help you to complete a command, or to help you complete a path or a file name. Press the TAB key two times consecutively, and the system will list all the instructions or filenames.

3) Aliases

There has been an introduction to Alias, which is one of the features of bash. We can alias a commonly used and very long instruction by aliases a simple and easy-to-remember instruction. If you don't want to use it, you can also use Unalias to remove the alias feature. Directly hitting alias will see the current system preset alias:

The basics before you learn shell scripting

See, the system preset alias instructions are just a few, you can also customize the command alias you want. Alias syntax is simple, alias [command alias]=[' specific Command '].

4) wildcard characters

Under Bash, you can use * to match 0 or more characters, and match one character with a.

The basics before you learn shell scripting

5) input/output redirection

Input redirection is used to change the input of the command, and output redirection is used to change the output of the command. Output redirection is more common, and it is often used to enter the results of a command into a file, rather than on the screen. The command to enter the redirect is &lt, the command to output redirection is, and the error redirect 2&gt, and the Append redirect >&gt, which will be described in more detail later.

6) Pipe character

The pipe symbol "|" has been mentioned earlier, which is to throw the result of the previous command running to the following command.

7) Operation control.

When you run a process, you can pause it (press CTRL + Z), then use the FG command to restore it, use the BG command to get him to run in the background, or you can make it stop (press CTRL + C).

Variable

In the previous section, I introduced the environment variable path, which is a variable of the shell preset, usually the shell preset variables are uppercase. Variable, the simple point is to use a simpler string instead of some special meaning settings and data. Taking path, this path replaces the absolute path setting for all common commands. Because of the variable path, we no longer have to enter the global path when we run a command, just hit the command name. You can use the echo command to display the value of a variable.

The basics before you learn shell scripting

Besides path, HOME, logname, what are the system preset environment variables?

The basics before you learn shell scripting

All system variables for system presets are listed using the ENV command. However, users who log on are different. These environment variables also have different values. The environment variables for the account root are currently displayed. The following is a brief introduction to the common environment variables:

path determines to which directories the shell will look for commands or programs

Home Current User Home directory

Histsize Number of historical records

LogName The current user's login name

Hostname refers to the name of the host

Shell Front user shell type

Lang language-related environment variables, multiple languages can modify this environment variable

Mail storage directory for the current user of mail

PWD current Directory

The env command displays variables that are only environment variables, and there are many more variables in the system preset, and you can use the SET command to display all the variables of the system preset.

The basics before you learn shell scripting

Confined to space, the author in the above example did not put all the results shown. Set can not only display the system preset variables, but also can be displayed with user-defined variables. User-defined variables? Yes, the user can also define the variables themselves.

The basics before you learn shell scripting

Although you can customize the variable, the variable can only be used in the current shell, do not believe you login to a shell to try?



Using the Bash command to open a shell again, the previously set myname variable no longer exists, exiting the current shell back to the original Shell,myname variable is still there. What if the variable you want to set is always in effect? There are two types of situations:

1) You can use this variable if you want all users in the system to log in

You need to add "export myname=aming" to the last line of the/etc/profile file and then run "source/etc/profile" to take effect. At this point you can run the Bash command again or su-test the account directly.


2) Just want the current user to use the variable

You need to add "export myname=aming" to the last line of the. bashrc file in the user's home directory and then run "source. BASHRC" to take effect. When you log in to the test account again, the myname variable does not take effect. The function of the source command above is to say that the configuration refresh is currently set, that is, it can take effect without logging off or logging on.

The author uses "Myname=aming" in the above example to set the variable myname, so what are the rules for setting custom variables under Linux?

A. The format of the variable is "a=b", where a is the variable name, B is the contents of the variable, there can be no space on either side of the equal sign;

B. Variable names can only consist of English, numerals, and underscores, and cannot begin with a number;

C. When the contents of a variable have special characters (such as spaces), you need to add single quotation marks;

The basics before you learn shell scripting

In one case, you need to note that the variable content itself is enclosed in single quotes, which requires double quotes.

The basics before you learn shell scripting

D. If the contents of the variable need to use other commands to run the result, you can use the back quotation marks;

The basics before you learn shell scripting

E. Variable content can accumulate the contents of other variables, need to add double quotation marks;

The basics before you learn shell scripting

If you accidentally add double quotes to single quotes, you won't get the results you want.

The basics before you learn shell scripting

As you can see from the above examples, the difference between single and double quotes is that double quotation marks do not cancel the function of the special characters that appear inside ($), and the special characters in single quotation marks all lose their function.

In the previous example, I used the bash command several times, and if you run bash in the current shell, you will enter a new shell, which is the Shell's child shell, so you might want to use the Pstree command to see it.

The basics before you learn shell scripting

Pstree This command will print all the processes in the Linux system through a tree structure. Limited to the length of the author not all listed, you can directly enter Pstree view. After setting a variable in the parent shell, the variable will not take effect after entering the child shell, and if you want this variable to take effect in the child shell, the Export command is used, which I used before.

The basics before you learn shell scripting

Export is actually a declaration of the meaning of this variable, so that the shell's child shell also know that the value of the variable ABC is 123. If no variable name is added to the export, it declares all variables.

The basics before you learn shell scripting

The last side, along with our custom variables, are declared.

How do I set a variable in front of you, and what if I want to cancel a variable? Just enter "unset variable name".

The basics before you learn shell scripting

With unset ABC, the Echo $abc will no longer output any content.

"Configuration files for system environment variables and personal environment variables"

It says a lot about the variables of the system, so where are these variables stored in the Linux system, and why do users automatically have these variables when they log on to the shell?

/etc/profile: This file presets several important variables, such as path, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, Histsize, UMAs, and so on.

/ETC/BASHRC: This file is mainly preset umask and PS1. This PS1 is the string of characters we have in front of the command, such as the author's Linux system PS1 is [[email protected] ~]#, you might as well look at the value of PS1.

The basics before you learn shell scripting

\u is the user, \h hostname, \w is the current directory, \$ is that ' # ', if the average user is displayed as ' $ '

In addition to the two system-level profiles, there are several such hidden files under each user's home directory:

. Bash_profile: Defines the file name of the user's personalization path and environment variable. Each user can use the file to enter shell information that is specific to their own use, which is performed only once when the user logs on.

. BASHRC: This file contains bash information dedicated to your shell, which is read when you log on and every time you open a new shell. For example, you can write user-defined alias or custom variables to this file.

. Bash_history: Records the command history.

. bash_logout: When you exit the shell, the file is executed. You can put some cleanup work into this file.



"Special symbols in the Linux shell"

While you are learning Linux, you may have been exposed to a special symbol, such as "*", which is a wildcard symbol that represents 0 or more characters or numbers. The following is the author of the special characters used to say.

1. *: represents 0 or more characters or numbers.

The basics before you learn shell scripting

Test can have no characters, or multiple characters, or it can be matched.

2.? : represents only one arbitrary character

The basics before you learn shell scripting

Whether it is a number or a letter, as long as it can be matched.

3. #: This symbol in Linux indicates the meaning of the comment description, that is, "#" after the content of Linux ignored.

The basics before you learn shell scripting

Insert "#" at the beginning or middle of the command, and Linux will ignore it. This symbol is used in a lot of shell scripts.

4. \: De-ideographic character, revert the following special symbol (for example "*") to normal characters.

The basics before you learn shell scripting

5. | : The pipe character, which has been said many times before, is the function of throwing the result of the preceding command to the command following the symbol. The following commands, which are mentioned here, are not available for all commands, and are commonly used for document operations such as Cat, less, head, tail, grep, cut, sort, WC, uniq, tee, tr, split, SED, awk, and so on, where G Rep, sed, awk are the tools that must be mastered for regular expressions, which are described in detail in the following sections.

6. $: In addition to the identifier used in front of the variable, there is also a magical, is and '! ' Use it together.

The basics before you learn shell scripting

The '!$ ' represents the last variable in the previous hit (perhaps called a variable, which is the last thing in the previous command), such as the last command, which is test.txt, then entering!$ under the current command represents Test.txt.

1) grep: Filtering one or more characters will be described in detail in subsequent content.

The basics before you learn shell scripting

2) Cut: intercepts a field

Syntax: cut-d "separator character" [-CF] n here n is the number

-D: followed by delimited characters, delimited by double quotation marks

-C: followed by the first few characters

-F: The next section is followed by the number of blocks

The basics before you learn shell scripting

-D followed by the delimiter character, where a colon is used as the split character, and-F 1 is the first paragraph, and the space between-F and 1 is optional.

The basics before you learn shell scripting

The-C can be either 1 digits n or an interval n1-n2, or multiple digits n1,n2,n3

The basics before you learn shell scripting

3) Sort: Used for sorting

Syntax: sort [-t delimiter] [-KN1,N2] [-NRU] Here's N1 < N2

-T delimiter: function with the-D one meaning of cut

-N: Sorting with pure numbers

-R: Reverse Sort

-U: To repeat

-KN1,N2: Sorted by N1 interval to N2 interval, can only write-kn1, sort N1 field

The basics before you learn shell scripting

The basics before you learn shell scripting

The basics before you learn shell scripting

4) WC: Count the number of lines, characters, and words of the document, and the commonly used options are:

-L: Count rows

-M: Statistics of characters

-W: Number of statistical words

The basics before you learn shell scripting

5) Uniq: To repeat the line, the author commonly used options only one:

-C: Count the number of repeated rows and write the number of lines in front

The basics before you learn shell scripting

There is a point to note that before the uniq, you need to sort and then uniq, otherwise you will not get what you want, the author of the above experiment has been sorted so omitted that step.

6) Tee: followed by the file name, similar to redirect ">", but the specific gravity is more than a function, the file is written in the following file in the same time, also displayed on the screen.

The basics before you learn shell scripting

7) TR: replacement character, commonly used to handle special symbols appearing in the document, such as the ^m symbol appearing in a DOS document. There are two common options:

-D: Delete a character, followed by the character you want to delete

-S: Remove repeated characters

The most common is to capitalize the lowercase: tr ' [A-z] ' [A-z] '

The basics before you learn shell scripting

Of course, replacing a character is perfectly possible.

The basics before you learn shell scripting

However, the substitution, deletion and repetition are all aimed at one character and have some limitations. If it is not useful for a string, so I suggest simply understand the TR can be, you will learn more in the future to implement the string manipulation tools.

The basics before you learn shell scripting

8) Split: Cut documents, common options:

-B: Divide the document by size in bytes

The basics before you learn shell scripting

Format as above, the following passwd is the prefix of the filename after the split, the file name is Passwdaa, Passwdab, Passwdac ...

-L: Split document by number of rows

The basics before you learn shell scripting

6.;: Semicolon. Usually we are in a row to hit a command, and then enter the run, so you want to run two or two more commands in a row? You need to add a ";" between the commands. The

The basics before you learn shell scripting

7. ~: User's home directory, if Root is/root, ordinary user is/home/username

The basics before you learn shell scripting

8. &: If you want to put a command in the background, you need to add this symbol. This is typically used when the command runs for a very long time.

The basics before you learn shell scripting

Jobs can be used to view tasks performed in the background in the current shell. The FG can be transferred to the foreground for execution. The Sleep command here is the meaning of sleep, followed by a number, in seconds, in the shell script of the usual term loop.

The basics before you learn shell scripting

At this point, you press CTRL +z to pause it, and then enter BG to go back into the background.

The basics before you learn shell scripting

In the case of multitasking, if you want to move the task to the foreground, FG is followed by the task number and the task number can be obtained using the jobs command.

The basics before you learn shell scripting

9. >>, 2>, 2>>: The above-mentioned directional symbols > and >> denote the meaning of substitution and addition, then there are two symbols which are 2> and 2>> here. Indicate error redirection and error append redirect respectively, when we run a command error, the error message will be output to the current screen, if you want to redirect to a text, then use 2> or 2>>.

The basics before you learn shell scripting

10. []: brackets, the middle is a combination of characters, representing any one of the intermediate characters

The basics before you learn shell scripting

&& | |

A semicolon has just been mentioned above for the delimiter between multiple commands. There are also two special symbols that can be used in the middle of multiple commands: "&&" and "| |". Here are a list of the following:

1) Command1; Command2

2) Command1 && Command2

3) Command1 | | Command2

Use ";" , the Command2 is executed regardless of whether the Command1 executes successfully, and when "&&" is used, only command2 executes after Command1 execution succeeds, otherwise command2 does not execute; use "| |" , Command1 execution succeeds Command2 not execute, otherwise go to execute Command2, in short Command1 and Command2 always have a command to execute.

The basics before you learn shell scripting

What you need to know before you learn a shell script

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.