Shell scripts are easy to make mistakes

Source: Internet
Author: User

Easy-to-make errors with shell scripts

1. For i in X y z: This sentence is delimited by a space, and when our $n contains spaces, there will be errors

such as for i in ' LS *.mp3 ', this ls *.mp3 after execution if there is a space on GG

Direct for I in *.mp3 just fine


2. CP $file $target: Error occurs when there are spaces in the $file

Correct: CP "$file" "$target"

But $file--the beginning of the word, will be treated as a command line line, just like the-ap in Cp-ap

Complete method: Use for loop

For i in./*.mp3;do cp "$i"/$target;d One


Summary: In the shell script encountered $n need to pay attention to the space problem, in general, variables to use "" to do the processing

In general, before the command such as the CD direct operation, the conversion of such as $ or ', you need to pay attention to the space problem


3.["$foo" = "Bar"]: An error will be made when the $foo begins.

[[can correctly handle blanks, blanks, with horizontal lines and other issues

Correct: [[$foo = bar]], of course [bar = "Foo"] can also

4. CD ' dirname ' $f ': when there are spaces in the path, an error occurs and the processing type is summarized above

Correct: The CD "' DirName" $f "'" in the Shell "is a match made from the inside out, which is not the same as the nearest match in the C language


5. [bar = "$foo" && foo = "$bar"]: In the learning shell, we compare test with [], the two are the same, so change the equation to test bar = "$foo" && foo = "$bar" It's actually two formulas.

correct [bar = "$foo"-a foo = "$bar"] or [[bar = ' $foo ' && foo = ' $bar ']] or [bar = ' $foo ']&&[foo = ' $ba R "]


6.[[$f > 7]]: I'm sorry, [[Can't be used as a comparison of numbers, I haven't noticed before, because neither

(($f >7)) or I often use ["$f"-GT 7], but first to judge $f is a number, otherwise it will be wrong. -----${string##*[0-9]} is empty, indicating that $string is a purely numeric

[Email protected] ~]# f=123456

[Email protected] ~]# echo $f

123456

[[email protected] ~]# echo ${f##*[0-9]}


[[email protected] ~]# echo ${f#*[0-9]}

23456


7.grep foo file|while read Line;do ((count++));d One

After execution, the value of count does not change, because AH | A pipeline is a shell subprocess, and a variable in a child shell cannot be passed.

[[email protected] ~]# grep s/etc/passwd|while read Line;do ((count++));d One

[Email protected] ~]# echo $count


[[email protected] ~]# grep s/etc/passwd|while read Line;do ((count++)); Echo $count |tr "\ n" "";d One

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32


8.If [grep s/etc/passwd]:if is followed by a judgment statement, it treats all command return values between if and then as a condition of judgment

But [] is a grammar, he is not part of the IF, there is no necessary connection

So the above should be written if grep s/etc/passwd >/dev/null;then.


9.if [[a = b]&&[c = d]];then: Reason ibid should be changed to if [a = b]&&[c =d];then


sed ' s/foo/bar/g ' file >file: Cannot read from a file and then directly modify it, but I remember sort has the parameter-O can

Echo $foo

msg= "Please enter a file form like *.zip"

Echo $MSG, there will be a case of *.zip matching a.zip, the correct way is echo "$MSG"


foo = bar or $foo =bar: variable assignment, correct for foo=bar no spaces, this is not C language


Cat >>file<<eof can also be used cat <<eof here is cat, don't make it echo


Cd/foo;rm *: Very dangerous operation, because/foo foo may not exist, then this sentence will be deleted in the/directory

Correct: Cd/foo && RM *; Execute multiple statements based on the return value of the CD so Cd/foo | | Exit 1;CD. Rm....

[bar = = "$foo"]: When you study, you say it very clearly. [] is not supported for a comparison other than =, and = is used only on the comparison of strings


for i in {1..10};d o something &;d One error, this is directly removed; Because & can play a role in segmentation

[[email protected] ~]# for i in {1..10};d o echo 1 &;d One

-bash:syntax error near unexpected token ';


cmd1 && cmd2 | | CMD3: I like it very much, but when Cmd2 returns a value other than 0, CMD3 is also executed.

The correct is: if cmd1; then Cmd2;else Cmd3;fi


at the command line echo "hello!" will be reported! Error and cannot be escaped, but there is no such error in the shell script

Ifconfig can be extracted separately from the command line, but not in the shell script-----It hurts.


$*:The error point is $* itself as a string, and when the command line argument has a space, an error is generated.

Correct procedure for I-in "[email protected]"--------take command-line arguments first, that's fine.


function foo () is not a problem in bash, but it is possible to make an error in other shells, so do not use function with parentheses

Direct foo () just fine.



Shell scripts are easy to make mistakes

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.