Ubuntu Exprot,source and Basic symbol description

Source: Internet
Author: User
Tags arithmetic mkdir
Shell and Export commands After the user logs on to the Linux system, a user shell is started. In this shell, you can use shell commands or declare variables, or you can create and run shell script programs. When you run the shell script program, a child shell is created. At this point, there will be two shells in the system, one for the system-initiated shell at logon and the other for the shell created by the system to run the script. When a script is finished, its script shell terminates and can be returned to the shell before executing the script. In this sense, users can have many shells, each of which is derived from a shell called the parent shell.   variables defined in the child shell are valid only within that child shell. If a variable is defined in a shell script, when the script is run, the defined variable is only a local variable within the script, and the other shell cannot reference it, so that the value of a variable can be changed in the other shell. You can use the Export command to export a defined variable. The export command will allow the system to define a copy of this variable when creating each new shell. This process is called variable output.   [Example] in this case, the variable myfile is defined in the Dispfile script. The Export command is then used to output the variable myfile to any child shell, such as the child shell that was generated when the Printfile script was executed.   Dispfile Script list:/**************begin dispfile**************/myfile= "list" Export myfile echo "displaying $ MyFile "Pr–t–n $myfile printfile/**************end dispfile***************/printfile script list:/**************begin pri ntfile**************/echo "Printing $myfile" lpr $myfile &/**************end printfile**************/$dispfile displaying List 1 Screen 2 Modem 3 paper Printing lis 

And the source executes the script to be able again the father sh to take effect is because this command actually simply reads the script inside the statement to execute in sequence in the current shell, did not establish the new child shell. Then all the new, variable-changing statements in the script will be saved in the current shell.

"Reprint" Linux source command and script execution mode analysis

When I modify the/etc/profile file, I want it to take effect immediately, without having to log in again, and then think of the source command, such as: Source/etc/profile
The source was studied and compared with the SH execute script, now to summarize.

SOURCE command:
The source command, also known as the Point command, is a dot symbol (.) and is the internal command for bash.
Function: Enables the shell to read the specified shell program files and execute all statements in the file sequentially
The source command is typically used to rerun an initialization file that has just been modified to take effect immediately, without having to log off and log on again.
Usage:
SOURCE filename or. FileName
The source command (from C Shell) is a built-in command for the bash Shell; the point Command (.), which is a point symbol (from the Bourne Shell) is another name for source.

SOURCE filename and SH filename and./filename the difference between executing scripts is there.
1. When the shell script has executable permissions, there is no difference between executing the script with SH filename and./filename./filename is because the current directory is not in path, all "." is used to represent the current directory.
2.sh filename to re-establish a child shell, in the child shell execute the statement inside the script, the child shell inherits the parent shell environment variables, but the child shell new, changed variables will not be brought back to the parent shell, unless the use of export.
3.source FileName: This command actually simply reads the statements in the script and executes them sequentially in the current shell, without creating a new child shell. Then all the new, variable-changing statements in the script will be saved in the current shell.
An example is provided:
1. Create a new test.sh script, which reads: a=1
2. Then make it executable chmod +x test.sh 3. After running SH test.sh, Echo $A, displayed as null because A=1 was not passed back to the current shell. After/test.sh, it is the same effect,

4. Run source test.sh or. Test.sh, and then echo $A, it displays 1, indicating that the a=1 variable is in the current shell

basic symbol description in Ubuntu

These special symbols are listed roughly as follows:
Copy code code as follows: #;;; . ,//' String ' |! $ ${} $? $$ $* "String" * * * *? : ^ $# $@ ' command ' {} [] [[]]] (()) | | && {Xx,yy,zz,...} ~ ~+ ~-&/+–%= = =!=

Output/Input Redirect:
Copy code code as follows: > >> < <<: > &> 2&> 2<>>& >&2

Here is a description of the meaning and use of these special symbols ...

# Well No. (comments)
It's almost a sign that's all over the field.
#!/bin/bash
Well numbers often appear at the beginning of a line, or after a complete instruction, which indicates that the symbol is followed by the callout text and is not executed.
# This is comments.
echo "a = $a" # a = 0
Because of this feature, when you temporarily do not want to execute a line of instructions, simply add a # at the beginning of the line. This is commonly used in the writing process.
#echo "a = $a" # a = 0
If used in a directive, or in quotation marks or in double quotes, or behind a slash, he becomes a general symbol without the special features described above.

~ Account's Home directory
is a common symbol, representing the user's home directory: cd ~; You can also add the name of an account directly after the symbol: CD ~user
Or as part of a path: ~/bin;~+ the current working directory, which represents the current working directory, and she has the same effect as the built-in instruction pwd.
# echo ~+/var/log
~-Last working directory, this symbol represents the last working directory.
# echo ~-/etc/httpd/logs

; Semicolon (Command separator)
In the shell, the symbol that acts as a "continuous instruction" function is "semicolon." For example, the following examples: CD ~/backup; mkdir startup; CP ~/.* startup/.

;; Consecutive semicolon (Terminator)
Dedicated in the case of the option to assume the role of Terminator.
Case "$FOP" Inhelp) echo "usage:command-help-version filename"; Version) echo "version 0.1″;; Esac

. Comma (dot)
In the shell, users should be aware that a dot represents the current directory and that two dot represents the upper directory.
Cdpath=.:~:/home:/home/web:/var:/usr/local
In the uplink cdpath setting, the dot after the equals sign means the current directory.
If the file name starts with Dot, the file is a special file, and the LS directive must be added with the-a option to be displayed. In addition, in regular expression, a dot represents matching a character.

' String ' single quotation mark (quote)
The enclosed quotation marks are treated as a single string. Within the quotation marks represents the variable's $ symbol, which has no effect, that is, he is treated as a generic symbol to prevent any variable substitution.
Heyyou=homeecho ' $heyyou ' # We get $heyyou

"string" double quotation mark (double quote)
Content that is enclosed in double quotes will be treated as a single string. It prevents wildcard extensions, but allows variable extensions. This differs from the way a single argument is handled.
Heyyou=homeecho "$heyyou" # We get Home
' Command ' inverted quotation mark (backticks)
In front of the single double quotes, the string is enclosed, but what happens if the string is a column of command columns. The answer is not to execute. To handle this situation, we have to do it in inverted single quotes.
fdv= ' Date +%f ' echo ' Today $FDV '
The date +%f in the inverted quotation mark is treated as an instruction, and the result of execution is brought into the FDV variable.

, comma (comma)
This symbol is often used in the operation as a "partition" use. The following example
#!/bin/bashlet "T1 = ((A = 5 + 3, B = 7–1, c = 15/3))" echo "T1 = $t 1, a = $a, B = $b"

/slash (forward slash)
Represents a directory when the path is represented.
CD/ETC/RC.DCD.. /.. CD/
Usually a single/representative root directory meaning; in arithmetic, a symbol for division.
Let "NUM1 = ((a = 10/2, B = 25/5))"

\ slash (Escape)
In the interactive mode of the escape character, there are several functions, before the instruction, there is a cancellation of aliases function, placed in the special symbol before the role of the special symbol disappeared; at the very end of the instruction, the instruction connects the next line.
# type RM
RM is aliased to ' rm-i '
# \RM \*.log.
In the example above, I added an escape character before the RM instruction to temporarily remove the alias and restore the RM directive.
# Bkdir=/home
# echo "Backup dir, \ $bkdir = $bkdir"
Backup dir, $bkdir =/home
In the above example, the \ $bkdir in Echo, Escape will cancel the function of the $ variable, so output $bkdir, and the second $bkdir will output the contents of the variable/home.

| Pipe (Pipeline)
Pipeline is a UNIX system, a fundamental and important concept. Link the standard output of the previous instruction to the standard input for the next instruction.
who | Wc-l
The use of this concept is quite helpful in streamlining script.

! Exclamation point (negate or reverse)
Usually it represents the function of the inverse logic, for example, in conditional detection, using!= to represent "not equal"
If ["$?"!= 0]thenecho "executes error" Exit 1fi
She plays the role of "anti-logic" in regular expressions
LS A[!0-9]
In the example above, the representative shows other documents in addition to A0, A1 ... A9 these several documents.

: Colon
In bash, this is a built-in instruction: "Do nothing", but return a status value of 0.
:
echo $? # response to 0
: > f.$$
The above line is equivalent to Cat/dev/null > f.$$. Not only is the writing brief, but also the execution efficiency is many.
Sometimes, the following types of usage can also occur
: ${hostname?} ${user?} ${mail?}
The purpose of this line is to check that these environment variables are set and that no settings will display the error message as a standard error. Such checks, if used like test or if, can be handled basically, but not as simple and efficient as the example above.
In addition to the above, there is a place that must use a colon
Path= $PATH: $HOME/fbin: $HOME/fperl:/usr/local/mozilla
In the user's own home directory, in the. bash_profile or any feature-like file, in the context of "path", we all use colons to do the partition.

? Question mark (wild card)
The role played on the filename extension (filename expansion) matches an arbitrary character, but does not contain a null character.
# ls A?A1
Use her features to make more precise file name matching.

* asterisk (wild card)
A fairly common symbol. On the filename extension (filename expansion), she is used to represent any character, including null characters.
# ls a*a A1 Access_log
In an operation, it represents "multiplication."
Let "Fmult=2*3″
In addition to the built-in instruction let, there is also an instruction on the operation of expr, where the asterisk also acts as a "multiplication" role. But be careful in use, he must be preceded by an escape character.

* * Sub-square operation
The two asterisks represent the meaning of the "square" at the time of operation.
Let "sus=2**3″echo" sus = $sus "# sus = 8

$ Money Number (dollar sign)
Variable substitution (Variable substitution) for the representative symbol.
Vrs=123echo "VRS = $vrs" # VRS = 123
In addition, the Regular Expressions is defined as the end of the line (End-of-line). This is commonly used in grep, SED, awk, and Vim (vi).

The regular expression
bash for the

${} variable defines a number of uses for ${}. The following is a table column
${parameter:-word} ${parameter:=word} ${parameter:?word} ${parameter:+word} ${parameter:offset that is taken from the line description Parameter:offset:length} ${!prefix*} ${#parameter} ${parameter#word} ${parameter# #word} ${parameter%word} ${ Parameter%%word} ${parameter/pattern/string} ${parameter//pattern/string}

$* referencing the script's execution reference variable. The algorithm for referencing the parameter is the same as the general instruction, the instruction itself is 0, followed by 1, and so on. Reference variables are represented as follows:
$, $, $, $, $, $, $, $, $, $, $, ${10}, ${11} ...
Single-digit, you can use a number directly, but more than two digits, you must use the {} symbol to enclose. The
$* is the symbol representing all reference variables. When used, it is necessary to enclose the case with double quotes.
echo "$*"
also has a symbol that has the same effect as $*, but the utility is slightly different from how it is handled. The

$@
$@ has the same symbolic effect as $*, but they have a different point. The
Symbol $* treats all reference variables as a whole. But the symbol $@ still retains the section idea of each reference variable.
$#
This is also a symbol associated with a reference variable, and her role is to tell you the total number of reference variables.
echo "$#"

$? status variable
in general, the process of a UNIX (Linux) system ends by executing the system call exit (). The return value is the status value. Passed back to the parent process to check the execution status of the child process.
If the normal instruction program succeeds, its return value is 0; failure is 1.
Tar cvfz dfbackup.tar.gz/home/user >/dev/nullecho "$?" $$
Because the ID of the process is unique, it is not possible to have a repetitive PID at the same time. Sometimes the script will need to generate temporary files to store the necessary data. This script may also be used by users at the same time. In this case, the fixed file name is not reliable in the wording. Only dynamic file names can be generated to meet the needs. Symbolic $$ may meet this requirement. It represents the current Shell's PID.
Echo $HOSTNAME, $USER, $MAIL "> ftmp.$$
uses it as part of the file name to avoid overwriting the same file name at the same time.
PS: Basically, the system recycles the completed PID and then assigns it again as needed. So script even if the temporary file is the use of dynamic filename, if the script is finished after the execution is still not clear, can cause other problems. The
() instruction Group (command Group)
encloses a sequence of consecutive instructions in parentheses, which is called the instruction Group for the shell. As in the following example: (CD ~; vcgh= ' pwd '; echo $vcgh), the instruction group has an attribute in which the shell executes the set of instructions by generating Subshell. Therefore, the variables defined therein are used only for the instruction group itself. Let's take a look at the example
# cat Ftmp-01#!/bin/basha=fsh (A=INCG; echo-e "/n $a/n") echo $a #./FTMP-01INCGFSH
In addition to the instruction group above, parentheses are also used in array variables and also apply to other situations where you might need to add an escape character, such as an expression.

The role of the

(())
is similar to the let instruction, and is used for the built-in function of bash for arithmetic operations. Therefore, it is much better to perform efficiently than using let directives.
#!/bin/bash ((a =)) echo-e "inital value, a = $a/n" ((a++)) echo "After a++, a = $a"

{} curly braces (block of code
sometimes appears in the script, with a paragraph or paragraphs in curly braces that end with a semicolon or a set of variables.
# cat FTMP-02#!/BIN/BASHA=FSH{A=INBC echo-e "/n $a/n"}echo $a #./FTMP-02INBCINBC
This usage is very similar to the instruction group described above, but there is a difference. It executes in the current shell and does not produce subshell. The
curly braces are also used in functions of functions. Broadly speaking, simply using curly braces, the function is like a function without a specified name. So writing a script is a pretty good thing to do. This approach, especially for the redirection of output inputs, can streamline the complexity of the script.
In addition, curly braces have another use, as follows
{Xx,yy,zz,...}
This combination of curly braces, used in the combination of strings, see example
mkdir {usera,userb,userc}-{home,bin,data}
We get Usera-home, Usera-bin, Usera-data, Userb-home, Userb-bin, Userb-data, Userc-home, Userc-bin, Userc-data, these several directories. This group of symbols is quite extensive in applicability. If you can use it wisely, the rewards are streamlined and efficient. Like the following example
Chown Root/usr/{ucb/{ex,edit},lib/{ex?.? *,HOW_EX}
If it is not for the support of this usage, we have to write several lines to repeat it several times.

[] Bracket
Often appear in the process control, play the role of the judge-style. If ["$?"!= 0]thenecho "executes error" Exit 1fi
This symbol holds a role similar to "Scope" or "collection" in a regular expression
Rm-r 200[1234]
In the example above, the Representative deletes 2001, 2002, 2003, 2004, and so on.

[[]]
This set of symbols is essentially the same as the previous [] symbol, but she allows direct use of the and && logic and other symbols.
#!/bin/bashread Akif [[$ak > 5 | | $ak < 9]]thenecho $AKFI | | This is often seen in logical notation, representing symbols of or logic. && logical Symbols This is also often seen, representing the symbols of and logic. & Background work a single & symbol, placed at the end of the complete instruction column, means that the instruction column is placed in the background to work. Tar cvfz data.tar.gz data >/dev/null &
/Word boundaries
This set of symbols is defined as the meaning of "boundary" in regular expressions. For example, when we want to find the word, if we use
grep the Filea
You will find that words like there are also used as matching words. Because it happened to be part of there. If we're going to have to avoid this, we have to add the symbol "boundary".
grep '/' Filea

+ PLUS sign (plus)
In an expression, she is used to denote "addition."
Expr 1 + 2 + 3
In addition, in regular expressions, the meaning of the front character of "many" is used.
# grep ' 10/+9′fileb109100910000910000931010009# This symbol must be preceded by an escape character when used.

-Minus sign (dash)
In an expression, she is used to denote "subtraction."
Expr 10–2
It is also an option symbol for system directives.
ls-expr 10–2
In the GNU directive, the meaning of "standard input" is represented if the-symbol is used alone, without any name of the file added. This is a common option for the GNU directive. such as the following example
Tar XPVF-
Here the-symbol, both represents reading data from the standard input.
However, in the CD directive is more special
CD-
This represents changing the working directory to the previous working directory.

% Division (modulo)
In an expression, used to represent "division."
Expr 10% 2
In addition, it is also applied to the following in the regular expression of a variable
${parameter%word}${parameter%%word}
A% represents the shortest word match and two of the longest word matches.

= equals sign (equals)
A symbol that is often seen when a variable is set.
Vara=123echo "Vara = $vara"
Or a PATH setting, or even applied to such uses as arithmetic or judgment.

= = equals sign (equals)
Often seen in conditional judgments, it means "equals".
if [$vara = = $varb]
... Next slightly
!= is not equal to
Often seen in conditional judgments, it means "not equal to".
If [$vara!= $varb]
... Next slightly

^
This symbol represents the "start" position of a row in a regular expression. Reference Link:

Http://www.jb51.net/article/51342.htm

Http://www.cnblogs.com/onemorepoint/p/5628987.html

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.