Similar under Linux

Source: Internet
Author: User

As operations personnel, often use Linux know, under Linux, a lot of commands, keywords, looks like a long, but the actual application can be found sometimes there is a difference, of course, the person who is not careful perhaps you will never understand, in the end what to make. Today as a rookie, I will give you a simple share of a few, not all, I hope you leave a message that the blogger at any time to update, in order to facilitate urgent access to everyone.

Rookie's discovery:
First, $vra _name and "$var _name"
Second, "$*" and "[email protected]"
Three, [] and [[]]

First, $vra _name and "$var _name"
Most of the time, $vra _name and "$var _name" is no different, but because of this, so when we write scripts, often encounter some other puzzling error, a lot of people feel I write right ah, grammar is correct, command spelling is not a problem, I do not know what is wrong. But it is this quotation mark let you racked your brains, if you can see my little rookie's blog, perhaps you also thoroughly understand and remember it.
Next, let's use a short example to illustrate
[[email protected] test]# cat test.sh Display test environment
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 21:07:38
#Description:

If [$ = start];then The script, it's pretty simple, and there's no problem with grammar or spelling.
echo Start
elif [$ = Stop];then OK, next, let's take the parameters to close the test.
echo Stop
elif [$ = Restart];then
echo Restart
Else
echo "Unknown parameter"
Fi
[Email protected] test]# test.sh start with the correct parameters there's no problem.
Start
[Email protected] test]# test.sh rest
Unknown parameter
[[email protected] test]# test.sh Why did not take parameters, it reported a large list of errors, without parameters, not also belong to the Else branch?
./test.sh:line 8: [: =: unary operator expected
./test.sh:line: [: =: unary operator expected
./test.sh:line: [: =: unary operator expected
Unknown parameter
[Email protected] test]#

Take a closer look, the first three branches of judgment error, the wrong hint is probably meant to expect a unary expression, then we have to think about it, then we give the package a quotation mark to see

[Email protected] test]# vim test.sh
[email protected] test]# cat test.sh
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 21:07:38
#Description:

If ["$" = Start];then
echo Start
elif ["$" = Stop];then
echo Stop
elif ["$" = Restart];then
echo Restart
Else
echo "Unknown parameter"
Fi
[[email protected] test]# test.sh stop
Stop
[Email protected] test]# test.sh restart
Restart
[Email protected] test]# test.sh Hello
Unknown parameter
[Email protected] test]# test.sh headache problem disappeared, even without parameters, will not error, normal went to the Else branch
Unknown parameter
[Email protected] test]#

This time should be able to understand that most of the situation is the same $vra_name and "$var _name" the power of a quotation mark. So it is recommended that you develop a good programming habits, not used to the two most of the situation is not the equivalent of the effect, we do not steal that lazy, as long as the reference variable, we put the damned quotation marks on it. Lest there be a headache, somehow.

Second, "$*" and "[email protected]"
And then we'll have another one with this quote has a little relationship between the "$*" and "[email protected]", wrote the script is aware that the script involved in the location variable, $* and [email protected] All represent the variable list, But who knows how to use the status quo of the advice of the rules of the way this time off.

Test environment:
[[email protected] test]# Cat script1.sh test Environment display ("$var-name")
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 15:18:57
#Description:

Script2.sh "$*" Special variable (argument list) quoted in quotation marks
Echo ==============================
script2.sh "[Email protected]" Special variable (parameter list) quoted in quotation marks
[email protected] test]# cat script2.sh
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 15:12:52
#Description:

echo "The first parameter is:$1"
echo "The second parameter is:$2"
echo "The third parameter Is:$3"
echo "All parameters Is:[email protected]"

[[email protected] test]# script1.sh a AB ABC $* and [email protected] execution effect when using quotation marks
The first parameter is:a AB ABC
The second parameter is: "$*" means to treat the entire list as a parameter.
The third parameter is: so both $ and $ are empty values
All Parameters is:a AB ABC
==============================
The first parameter is:a
The second parameter is:ab "[email protected]" represents the entire list of scattered
The third parameter is:abc so both $ and $ can be assigned as normal
All Parameters is:a AB ABC
[Email protected] test]#
Normal Add the quotation marks, this time also dropped big, two is to represent the parameter list of variables, call the same parameter also has the difference, carefully consult related data, finally found $* and [email protected] really is twins, but they still have a difference:
"$*" (when using double quotation marks): All arguments passed to the script, all parameters are combined into a single string
"[Email protected]" (when in double quotes): All arguments passed to the script, each argument being a separate string

Let's take a look at the example when no quotation marks are used:
[[email protected] test]# Cat script1.sh test Environment display ($var-name without quotes)
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 15:18:57
#Description:

Script2.sh $*
Echo ==============================
script2.sh [email protected]
[email protected] test]# cat script2.sh
#!/bin/bash

#Author: Wangjun
#Version: 1.0
#Create time:2016-08-14 15:12:52
#Description:

echo "The first parameter is:$1"
echo "The second parameter is:$2"
echo "The third parameter Is:$3"
echo "All parameters Is:[email protected]"

[[email protected] test]# script1.sh a AB ABC $* and [email protected] execution effect without quotation marks
The first parameter is:a
The second parameter is:ab because $* and [email protected] are not quoted, both represent the entire list of scattered
The third parameter is:abc so both $ and $ can be assigned as normal
All Parameters is:a AB ABC
==============================
The first parameter is:a
The second parameter Is:ab
The third parameter is:abc
All Parameters is:a AB ABC
[Email protected] test]#

$* and [email protected] without quotation marks: two represents the entire list of scattered (that is, each parameter is a separate string), it can be seen, this time without the use of quotation marks will not go awry. So we can only conclude that any transaction is not absolute, there must be a premise or environment. Of course, this knowledge is something you have to master for the Ops people we use with Linux systems, or else you're going to freak out.

Three, [] and [[]]
In most cases, [] and [[]] are also similar, so today, you look at a piece of the trouble-racking picture
[Email protected] test]# A=good
[Email protected] test]# echo $A
Good
[[Email protected] test]# [-n $A] && echo "A is Nonespace" | | echo "A is Space"
A is Nonespace test whether the string is non-empty, no quotes call the variable, and no error

[Email protected] test]# [-Z $A] && echo "A is Space" | | echo "A is Nonespace"
A is Nonespace lucky (this belongs to most of the cases)

[Email protected] test]# [$A =~ go] && echo "Matching" | | echo "Not matching"
-bash: [: =~: binary operator expected another one with the right-hand pattern to match to the left, error report.

Not matching
[[Email protected] test]# ["$A" =~ go] && echo "Matching" | | echo "Not matching"
-bash: [: =~: binary operator expected the magic Weapon--add a quotation mark, the result is invalid

Not matching
[[Email protected] test]# [["$A" =~ Go]] && echo "Matching" | | echo "Not matching"
Matching double brackets in the big savior fell from the sky, the monster finally pressed to the mountain under the fingers

[[Email protected] test]# [[$A =~ god]] && echo "Matching" | | echo "Not matching"
Not matching at this time found to take off its magic spell-double quotation marks, it also unable to roll over

[[Email protected] test]# [[$A =~ go]] && echo "Matching" | | echo "Not matching"
Matching
[Email protected] test]#

Through this test we can also find that [] and [[]] are also twins, but sometimes their character is still a bit of community, so we are in use, the same need to be good off.
Well, today's sharing is so far, not all, I hope readers timely message to add.

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1837963

Similar under Linux

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.