Shell 13 Q Summary

Source: Internet
Author: User
Tags echo command

We know that the hardware resources of the computer, such as disk, IO, memory are unified management by software, this kind of special software is often said operating system, windows in the underlying resource control based on the building of its own interface, is very suitable for use, just need to go around a little bit to complete the functions we need. This is a way to control resources, and you can use command to manipulate the underlying resources. The most important part of LIUNX is its kernel, which manages the resources of the system, and also gives us the interface to operate the kernel, which is the shell (shell) We often use, and the following kinds of main shell:

Sh:

Burne Shell (SH)
Burne again shell (bash)
Csh
C Shell (CSH)
TC Shell (TCSH)

Korn Shell (Ksh)

More contact is bash and ksh, several different shell functions are broadly similar, of course, each has its own characteristics.

Typically for non-admin users, the display is $, and the administrator displays root. New line characters under Linux (press ENTER) with CR, the shell interpreter interprets the command line in front of the CR when it encounters the CR, explaining that it first divides the command lines according to IFS, and then handles the metacharacters, for example: Echo $A Where A is the value of Hello, disassembled by IFS (default tab,newline,space) to Echo and $ A and then to the meta-character $, take the value of the A variable, and finally output hello to the screen.

First, Echo

The echo command is the output command we often need to see the values of the parameters, which provide several parameters that can be used to achieve different effects, such as Echo-n is the end of the line break, you can test the environment on the output echo-n "a" and echo "a" is different. If we want to add a new line in the default may think of Echo "a\n", this is not feasible, echo by default is the-e parameter, indicating not to interpret the backslash character conversion, so need to open \ Function, need to use the-e parameter, as follows: Echo-e "a\n".

Two, double quotes "" and single quotes "

In the shell, the role of double and single quotes is different, the character in the shell can be divided into two, one is ordinary characters, one is a meta-character, such as $ is a meta-character, when executed to the meta-character when the shell will do the corresponding action, such as Echo $A is to print the value of variable A, But if we want to print "$A" then what should be done, the answer is to use single quotation marks, echo ' $A ', all the metacharacters in single quotes are treated as ordinary characters, then the double quotation marks are different, the double quotes will turn off most of the metacharacters processing, But there are still a few parts that will be interpreted as metacharacters, such as the $ symbol, and another way to turn off metacharacters is in the meta-character Fubanga/backslash.

Iii. Var=value and Export

Strictly speaking, the variables defined in the current shell are local variables, and only the export will become an environment variable for use by subsequent commands, and the detailed explanation of the Export command will be mentioned later.

D. exec and source

When executing the shell, we often use source *.sh, in fact, when we execute the *.sh script, we generate a subshell process to execute, and the environment variables of the child process and the environment variables of the parent process are not shared, so if we define the cd/home/a/b/ This is why the D command is in the test.sh script and executes test.sh under/HOME/A, in fact the directory does not switch to/home/a/b/d. So how can you perform test.sh to make directory switching, the shell provides two ways to prevent the Subshell process from executing scripts, source and exec, which will complete the script invocation in the current shell process. So what's the difference between these two ways of calling? Look at one of the following examples:

1.sh

#!/bin/basha=becho "PID of 1.sh before exec/source/fork:$$" Export Aecho "1.sh: \ $A is $A" case $ in        exec)                echo "US ing exec ... "                exec./2.sh;;        SOURCE)                echo "Using source":                /2.sh;;        *)                echo "Using fork"                ./2.sh;; Esacecho "PID of 1.sh after exec/source/fork:$$" echo "1.sh:\ $A is $A"

2.sh

#!/bin/bashecho "PID for 2.sh are: $$" echo "2.sh get \ $A = $A form 1.sh" A=cexport Aecho "2.sh:\ $A is $A"

Executes the./1.sh Fork Result:

PID of 1.sh before exec/source/fork:18885
1.sh: $A is B
Using fork
PID for 2.sh is:18886
2.sh get $A =b form 1.sh
2.sh: $A is C
PID of 1.sh after exec/source/fork:18885
1.sh: $A is B

After you execute the./1.sh exec Result:

PID of 1.sh before exec/source/fork:20952
1.sh: $A is B
Using exec ...
PID for 2.sh is:20952
2.sh get $A =b form 1.sh
2.sh: $A is C

Result of executing./1.sh Source: A

PID of 1.sh before exec/source/fork:26519
1.sh: $A is B
Using source
PID for 2.sh is:26519
2.sh get $A =b form 1.sh
2.sh: $A is C
PID of 1.sh after exec/source/fork:26519
1.sh: $A is C

The following analysis prints the results:

The default fork execution produces a child process that executes the shell script, where the export effect in the parent process is passed to the child process, but the export effect in the child process is not passed to the parent process, so the Export command has a one-way effect that passes the child process from the parent process.

exec and source execution do not produce child processes, but instead execute scripts in the current process, and the difference is that exec terminates the current script execution, and when the sub-script finishes executing, the entire execution process is complete.

Differences between the five, (), and {}

Here to introduce the concept of a command group, just like a function in another language, () and {} The difference is () is executed in the child shell, {} is executed in this shell, said here have to come up with a concept, function, in the shell to define functions in two ways, one is Function_ name{}, there is also a function_name () {}.

The difference between six, $ (()), ${}, and $ ()

In Bash, $ () and ' are used as command replacements, command substitution and previous variable substitution concepts are somewhat similar, such as dir=$ (PWD), where the function of the PWD will be assigned to the variable dir,${} for variable substitution, such as A=b;echo ${a}, You can actually echo $A here, but if you write the echo $AA, the shell does not know anything else, so ${} acts as a boundary, but if you only think that ${} is just acting as a boundary, it is too much to underestimate.

Suppose there is a variable file=/dir1/dir2/dir3/my.file.txt

We can use ${} to replace different variables to get different values.

${file#*/}: Removes all characters from the first/its left and the result is Dir1/dir2/dir3/my.file.txt

${file##*/} Removes all characters from the last/and its left, and the result is My.file.txt

${file%/*} Removes all characters from the last/and right, resulting in/dir1/dir2/dir3

${file%%/*}: Remove the first/its right string: (null value)
#是去掉左边,% is removed to the right, one is the shortest match, two is the longest match

${file:0:5} The result is 5 characters to the right from the beginning of the No. 0/dir1

Therefore ${file:offset:length} takes a length character from offset to the right

${file/dir/path} replaces the first dir with path, and the result is/path1/dir2/dir3/my.file.txt

${file//dir/path} replaces all dir with path.

The above is the substitution of the character, and can also be replaced according to whether the current value is empty, as follows:

${file-myfile.txt} output MyFile.txt if file is undefined

${file:-myfile.txt} output If file is undefined or empty for definition myfile.txt

${file+myfile.txt} outputs MyFile.txt if file has a definition, whether or not it is empty

${file:+myfile.txt} If file is not empty, the output myfile.txt

${file=myfile.txt} If file is undefined, the output myfille.txt is also assigned a value of file MyFile.txt

${file:=myfile.txt} If file is undefined or empty, the output myfile.txt


${#file} The length of the output file variable

By the way, let me introduce you to the arrays array in bash.

The definition of an array is similar to the following: A= (A b c d e) split between spaces

If you want to get the contents of this array, you can use ${a[@]} or ${a[*]}

Finally look at the use of $ (()), $ (()) convenient for us to do integer operations, such as A=1;b=2;echo $ ((a+b)) output of 3. $ (()) provides a Class C language style for us to do comparisons and calculations.

Vii. [email protected] and $* differences

For example, we have a SH script name of test.sh, it can accept parameters, test.sh a b C below, the way to get external parameters for $ $ $ $ A B C, if you want to get the name of the foot itself, use $. Note the functions defined in the script can also receive parameters that can be used in the same way. Gets the arguments that are passed in. Different time is not the name of the function but the name of the outermost script.

Instead of getting the tenth argument, the $ $ is getting the first argument followed by the string 0, so if you want to get the tenth argument you can use ${10}, or shift 1 to remove the first argument ($), shift 5 is to cancel the top 5 arguments.

Introduce a parameter $ #表示获取传入的参数总数, such as test.sh one and three $ #为3.

If you want to get all the parameters you can use [email protected], such as test.sh one two three get to three word, and $* is the parameter as a whole.

Viii. && | | The difference

In the introduction of $$ and | | First to understand a new concept, return value, in the execution of command or function will be passed the value of the parent process, we can use $? To get the latest return value, return values only two states, 0 is true, not 0 is false, Like what:

Suppose MyFile.txt exists, and a.txt does not exist, LS a.txt after the echo $? is not 0, if LS myfile.txt is 0. You can test the return value by using Test expersion or [expersion]. But I'm more accustomed to using [] this syntax.

Next you can see $$ and | | , Command1 && Command2 indicates that Command2 is executed if the Command1 command returns TRUE. Command1 | | Command2 indicates that Command2 is executed if Command1 returns FALSE.

Nine, < and >

There are approximately three types of fd (file descriptors), 0 standard input, 1 standard output, and 2 error output,> to indicate that standard output redirection is the same as 1>.

The 2>&1 indicates that the standard error output is redirected to standard output.

The &>file indicates that the standard output and the standard error output are redirected to file files.

2>&-indicates the standard error output is turned off

>/dev/null 2>&1 means that the standard error output is bound to the standard input and the standard input is redirected to/dev/null, which means nothing is output

X. Case and if

Sometimes there is a need to perform different logic based on the current value, if this comes in handy, the standard if usage is:

If Comd1;then

Comd2

Elif Comd3;then

Comd4

Else

Comd5

Fi

If then do not want to run any command can use: This null command to replace.

If the condition is more and the string is a different syntax, case

Case "$" in

Start

Start

;;

Stop

Stop

;;

Restart|reload)

Restart

;;

*)

Exit 1

Esac

Xi. for, while and until

Loop is a very basic logical control, and all languages provide their own loop control statements

The For loop reads the value from a list of listings and then processes it:

For Var in one, three four five

Do

...

Done

A statement of the cumulative variable is given below:

For ((i=1;i<=10;i++))

Do

...

Done

If you change to a while implementation, the following:

Num=1

While [$num-le];d o

...

Done

-le means less than or equal to

While a loop is entered when the condition is true, until, in contrast, enters the loop when the condition is false.

There are also break and continue keywords in the same loop, and the usage of other languages such as Java is not explained.

Above is the Shell 13 asked the general content, some content does not involve, follow-up will be some of the more commonly used commands such as Grep,sed,awk and so on simple explanation.

Shell 13 Q Summary

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.