Shell Programming Summary

Source: Internet
Author: User

Because the shell script is used in the project, the system looks at it a bit. Previously, it was a general understanding.

Variables: Environment variables, pre-defined variables, positional variables, custom variables.

Environment Variables This is to say that you can see the list by setting or the ENV command, which can then be displayed via Echo ${var}. You need to mention that there are environment variables under windows, or you can use echo%{var}% to display them.

Such as:

[[Email protected]]#Env Term=Xtermshell=/bin/bashhistsize= +Ssh_tty=/dev/pts/0USER=Rootmail=/var/spool/mail/RootPath=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binpwd=/Rootjava_home=/opt/Javalang=en_us. utf-8Histcontrol=IGNOREDUPSSHLVL=1HOME=/Rootlogname=Root[[email protected]]#Echo$USERroot [[Email protected]]#

predefined variables , as the name implies, are defined variables, such as "$"/"?" /”!”。

The functions are: representing the current PID, representing the result of the last execution (returned NUM), representing the previous background process pid!

Such as:

Echo  $$ 2118  Echo $? 0  Echo $! ~]#

positional variables are, in fact, one of the predefined variables, $0~$9, and ${n}. Represents a parameter when the script is executed.

For example: Suppose we have a demo.sh script when you execute./demo.sh p1 p2 p3. , it is./demo.sh,$1 is the P1, in turn, and so on.

But there are three predefined variables, namely "*"/"@"/"#", which represent all parameters (as a whole), all parameters (as arrays), the number of parameters!

We are given the following script demo.sh:

#! /bin/bash# on the top two .Echo "the command executed is: $"Echo "The first parameter is: $"Echo "the number of parameters is: $#"# These two look the same, but they can be seen in the for.Echo "all parameters are: $*"Echo "all parameters are: [email protected]"# Note that you have to use double quotation marks, otherwise it will be forIinchp1 p2 p3, and [email protected] no difference.  forIinch "$*"     Do Echo$i DoneEcho "---" forIinch "[email protected]"     Do Echo$i Done
[Email protected]]#./demo.SH  One  A  -  -  -the command executed is:./demo.SHThe first parameter is: Onethe number of parameters is:5all parameters are: One  A  -  -  -all parameters are: One  A  -  -  - One  A  -  -  ---- One A - - -[[Email protected]]#

Custom variables are not said, and Java is no different.

Operation

In addition to variables, the next step is the operation, which is the expression.

However, the awkward thing is, the shell is all strings, this is too uncomfortable

Such as:

[Email protected] ~]# x=3~]# y=4~]# z= $x +echo  $z  3+4    ~~]# z=$ (($x +$y))7    expr $x + ~]#

It is recommended that you use the "$ (Calculate expression)" method because the other two are inconvenient!

But to mention declare!.

Declare–i is considered an integer (note, not a floating-point number)

Declare–x is exported as an environment variable (export is the variant of this)

Declare–p is the definition of the print out variable

Declare–r is set to read-only (cannot cancel the kind of ... )

What's more awkward is that if you want to cancel the setting, you need to turn the minus sign "-" into a plus "+"--but not for read-only ...

The above is a numerical operation, the next is the logical operation , that is, we say the judgment is true or false.

The Shell's judgment is a bit like the El expression in the JSP, and its defined operators can perform the corresponding operations, such as-GE/-GT/-LE/-LT/-EQ/-NE/!/-Z/-N/-D/-F/-E and so on.

In fact, are English words of the first letter, very good understanding is also very good remember-except-Z and-N, the former is for the empty judgment, the latter is non-null judgment, as zero and not, too awkward!

There are two forms of logical operations:

Test–e "/root" # to determine if a file exists

[11–GT 22] # Determine if 11 is greater than 22

How do I see the results? Can be passed "echo $?" To view!

However, it is simpler to use && and | |, as follows:

Test–e "/root" && echo yes | | Echo No

[11–GT] && echo yes | | Echo No

Equal judgment, can use = =, can also use-eq, but the latter can only be used for integers!

It is important to note that a single equal sign =, can also be used to judge equality, but easy to cause misunderstanding, so be sure not to use!

After the operation, it is logical control, we all know the logical control of the three big statements if/for/while, Hmm, haven't seen while, but should have, is awkward.

It's too late, and the Super boss is coming in tomorrow morning to go to bed.

Shell Programming 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.