Shell script learning tutorial-Memorandum

Source: Internet
Author: User
Tags echo command

1. Preface

This is a series of learning documents. Based on the spirit of Internet sharing, it mainly comes from the shell area on the Forum. It has been organized and is the content of a book. Therefore, copyright belongs to the original author of the book, here we only use learning materials. In fact, these documents are still very fast to organize. After all, the format on the forum is still difficult to read. At least it is not fun. Of course, I would like to pay tribute to bamboo, if you think this seriesArticlePlease let us know.

There are about six articles in this series, which are about basic entry points. As O & M personnel, it is necessary to master some script knowledge. At the same time, I will also write several study notes, mainly as my memorandum, which can be turned over when I have nothing to forget.

A learning memorandum will be attached later.

2. Chapter 1

We have knowledge about environment variables and shell variables. Because it is relatively simple, it is no longer necessary to search online.

We Reprinted from quotation marks:

I sorted it as follows:

2.1 necessity of reference
only the reference basic rules are described here. There are many examples to be referenced. The next two parts of this book will be described one by one. When you execute row operations in the script, shell will explain the script settings. You need to take a way to prevent shell from doing so, that is, using a reference number, including various references or using a backslash.
some users may find it difficult to use references when performing feedback operations on text strings. Sometimes I don't pay attention to it. I only referenced half of it. Then the problem occurs.

it is recommended that double quotation marks be used when text strings are fed back.

The following are examples of various references.
echo hit the star button to exit *
hit the star button to exit child. sh father. sh findfile. sh param2.sh Param. sh who_is.sh
the text is returned, but because double quotation marks are not used, * is misunderstood by shell, shell considers the user as a directory list. The result is as follows:
echo" hit the star button to exit * "
hit the star button to exit *

In this way, there will be no misunderstanding. Table 1 5-1 lists various reference types.
Table 15-1 shell reference type
"" Double quotation marks (this is exactly a symbol in French)
''Single quotes \ backslash

2.1.1 double quotation marks
Double quotation marks can be used to reference any character or string except the characters $, ', and. These special characters are dollar signs, backquotes, and backslash, which have special significance for shell. If you use double quotation marks to assign a string to a variable and feed it back, it is actually no different from the direct feedback variable.
String = "May Day, May Day, going down"
Echo "$ string"
May Day, May Day, going down
$ Echo $ string
May Day, May Day, going down


Now it is assumed that you want to set the system time to output to the variable mydate.
Mydate = "date"
Echo $ mydate
Date

Because shell assigns the string in the "" symbol to the variable mydate, date has no specific meaning, so the variable only saves the word date. Double quotation marks are often used to query strings containing spaces. The following uses grep to extract the name "Davey wi r e". Because no double quotation marks are added, grep regards "Davey" as a string, while "wire" is usedCompositionComponent name
$ Grep Davey wire/etc/passwd
$ Grep: wire: no such file or directory
$ Grep "Davey wire"/etc/passwd

To solve this problem, double quotation marks can be added to the string. In this way, shell ignores spaces. When using characters, double quotation marks should always be used, whether it is a single string or multiple words.
Variables can be caused by double quotation marks in a feedback text line. In the following example, shell feedbacks the text line and encounters the symbol $, knowing that this is a variable, and then replacing the variable $ boy with the variable value boy.
$ Echo "the $ boy did well"
The boy did well
$ Echo "the" $ boy "did well"
The boy did well
The single quotes are similar to double quotes. The difference is that shell ignores any reference values. In other words, if special meanings are shielded, all characters in quotation marks, including quotation marks, are considered as a string. The result of the previous example is as follows:
$ Girl = 'girl'
$ Echo "The '$ gir' did well"
The 'gir' did well

2.1.2 quotation marks
Backticks are used to set the output of system commands to variables. Shell uses the content in the quotation marks as a system command and executes the content. This method can replace the output with a variable. Backticks can be used with backticks. The following is an example.
In the following example, shell tries to replace the word "hello" as a system command and execute it. Because the hello script or command does not exist, an error message is returned.
$ Echo 'hello'
Bash: Hello: Command not found
$ Echo 'date'
Sat Nov 20 21:40:47 CST 2010
This command is valid. The shell executes the command correctly. The command output is set to the variable mydate. The time format is as follows:
$ Date + % A "the" % E "of" % B "" % Y
Saturday the 20 of November 2010
$ Mydate = 'date + % A "the" % E "of" % B "" % y'
$ Echo $ mydate
Saturday the 20 of November 2010
$ Mydate = 'date'
$ Echo $ mydate
Sat Nov 20 21:43:06 CST 2010

In another example, the quotation marks are enclosed in double quotation marks:
$ Echo "the date today is 'date '"
The date today is sat Nov 20 21:44:03 CST 2010
$ Echo "there are 'who | WC-l' users on the system"
There are 3 users on the System

In the preceding example, after a string is printed, shell returns a quotation mark and regards it as a command to execute it.

If the next character has a special meaning, the backslash prevents shell from misunderstanding its meaning, that is, shielding its special meaning. The following characters have special meanings: & * + ^ $ '"| ?.
Assume that the echo command is added with *, that is, the current entire directory list is printed in serial order, rather than an asterisk *.

$ Echo *
ILD. Sh father. Sh findfile. Sh param2.sh Param. Sh who_is.sh
To block the specific meaning of an asterisk, you can use a backslash.
$ Echo \*
*
The preceding statement can also be used for the $ command. Shell interprets it as the current process ID and uses a backslash to block this.
Print $.
$ Echo $
7906
$ Echo \ $
$
When printing a string, you must add the octal character (ASCII character). You must add a backslash to the front. Otherwise, shell treats it as a common number.
$ Echo "this is a copyright 251 sign"
This is a copyright 251 sign

$ echo-e" this is a copyright \ 0373 sign "
This Is A copyright

in Linux, you must use the \ 0nnn octal character to display the copyright character.
when the command expr is used, an error occurs when * is used to represent multiplication. A backslash is added before.
$ expr 12*12
expr: syntax error
$ expr 12 \ * 12
144
Add metacharacters to the echo command, the backslash must be used for shielding. The following example shows the price of $19.99. $ Blocking and unblocking produce different results.
$ echo" that video looks a good price for $19.99 "
that video looks a good price for 9.99
$ echo" that video looks a good price for \ $19.99 "
that video looks a good price for $19.99
use the backslash to shield $ to get better results..

Some problems may occur during reference and often errors occur. I follow two rules when using references:
1) The feedback string is enclosed in double quotation marks, but do not reference the feedback itself.
2) if the reference results are not satisfactory, try another method. After all, there are only three reference methods, and you can try it out.

3. Learning memo

The content above is not correct in some places.

1. Reverse quotation marks
Double quotation mark nesting
Example: $ mydate = 'date + % A "the" % E "of" % B "" % y'
$ Echo $ mydate

You can try to remove double quotation marks, and you will be prompted Date: Additional operand "the" please try to execute "date -- Help" for more information.
In fact, the matching of quotation marks must be clear: "The", "of", "". According to the document, "" only contains the "\", "$" symbol escape, and others are output as usual, therefore, do not mistakenly recognize that "% E" is a double quotation mark.
It is interesting to verify echo "% E.
2, Echo *
Shell contains some special characters, such as: & '"|?
If you want to block special meanings, You Need To backslash \,
Try echo * and echo \ *, while echo $, *, +, ^ ,? Can be normally output, it seems that the teaching material will be wrong.
It should be wrong. There are still some special characters. <,> ,\,(,),;,~, #, Where #; the symbol has no output .~ Output user path. Other data cannot be output directly.
3, expr
Expr 12*12
OUTPUT 12*12, which is considered to be the first operand, separated by spaces, such as expr 12*12, with a backslash \, expr 12 \ * 12

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.