A lot of people are learning C + + language First, then learn shell script. If you have a little programming base, it's easy to understand the so-called order, selection, and loop. Order structure In fact, there is nothing to say, before the contact before, in this article, we have to introduce the choice of structure. Although simple, but still be skilled to use correctly, after all, and C + + syntax is still some access. carelessly, it is easy to make mistakes.
1. First look at the entry level:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo "Enter your bank password"
read passwd
if [$pa SSWD = "3.1415"]; Then
echo Yes
else
echo no
fi
[Taoge@localhost learn_shell]$
[Taoge@localhost Learn_shell] $
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$./a.sh
Enter your bank password
1
no
[taoge@localhost learn_shell]$./a.sh
Enter your bank password
3.1415
Yes
[ Taoge@localhost learn_shell]$./a.sh
Enter your bank password
5: [:/a.sh:line Too many Ents
no
[Taoge@localhost learn_shell]$
The above procedure, there are several noteworthy points to note:
(1) $passwd on both sides of the best with quotes, so that users enter 12 34, 12 34 = "3.1415" error.
(2) If there must be a space behind, so disgusting provisions ah.
(3) [] where the space must be added, or there will be errors.
(4) If then and if are in the same line, then the semicolon in front of then must not be less. Of course, then can be placed under separate lines
(5) In a more equal time, it is best to use = =, rather than =, although the latter can also, but will make many C + + program apes feel difficult to adapt, feel uncomfortable.
Next, I'll change it to make the above program better:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo "Enter your bank password"
read passwd
if ["$p asswd "= =" 3.1415 "] # Pay special attention to this line, my friends!
Then
echo Yes
else
echo no
fi
[Taoge@localhost learn_shell]$
[Taoge@localhost learn_ shell]$
[Taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$./a.sh
Enter your bank password
1
No
[taoge@localhost learn_shell]$./a.sh
Enter your bank password
3.1415
Yes
[Taoge@localhost learn_shell]$./a.sh
Enter your bank password
No
Linux shell script also makes short-circuit evaluation, as in C + +, as follows (I think the program is very subtle):
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash #!/bin/bash
echo "Enter your bank password"
read passwd
["$passwd" = "3.1415"] && echo Yes
["$passwd" = = "3.1415"] | | echo No
[Taoge@localhost Lea rn_shell]$
[Taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[Taoge@localhost Learn_shell ]$./a.sh
Enter your bank password
3.1415
yes
[taoge@localhost learn_shell]$./a.sh
Enter Your bank password
3
no
[Taoge@localhost learn_shell]$
2. Looking at a slightly more complex, in fact, is also very simple:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo ' Enter your login password '
read passwd
if [$ passwd "= = yaoming"]
then
echo "Hello yaoming, are so Tall"
elif ["$passwd" = = Liuxiang]
then
echo "Hello Liuxiang, are so fast"
elif ["$passwd" = = Wangliqin]
then
echo "Hello Wangliqin, play Pingpong so OK "else #no then in the" "" You don't
belong here
"
fi
[Taoge@localhost le arn_shell]$
[Taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[Taoge@localhost learn_ shell]$./a.sh
Enter your login password
yaoming
Hello yaoming, you are so tall
[taoge@localhost learn_shell]$./a.sh
Enter your login password
liuxiang
Hello Liuxiang, you are so fast
[ Taoge@localhost learn_shell]$./a.sh
Enter your login password
wangliqin
Hello Wangliqin Pingpong so
ok [taoge@localhost learn_shell]$./a.sh
Enter your login password
zhangyining
Don't belong here
[Taoge@localhost learn_shell]$
3. Look at the case again:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo ' Enter your login password '
read passwd case
' $PASSWD "
in" yaoming ")
echo" Hello yaoming, your are so tall "
;;
Liuxiang ")
echo" Hello Liuxiang, you are so fast "
;;
Wangliqin ")
echo" Hello Wangliqin, you play pingpong so OK "
;
*
echo "You don't belong Here"
;
Esac
[Taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[Taoge@localhost learn_shell]$
[Taoge@localhost learn_shell]$./a.sh
Enter your login password
yaoming
Hello yaoming, you are are so Tall
[Taoge@localhost learn_shell]$./a.sh
Enter your login password
liuxiang
Hello Liuxiang are so fast
[Taoge@localhost learn_shell]$./a.sh
Enter your login password
wangliqin
Hello Wangliqin, play pingpong so
OK [taoge@localhost learn_shell]$./a.sh
Enter your login password
Zhangyining you don't
belong here
[Taoge@localhost learn_shell]$
Need to note:;; It is similar to the break in C/s + + language, except that there must be no less here, otherwise there will be problems when running the shell.
In addition, the final * position is very important and must be put in the end, because the case in the Linux shell script is an article by article, and the location of the default placement in C + + is not limited, and in the first place there is no relationship.
Here, let's check together, *) and put it in front of you that will cause a logical error, as follows:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo ' Enter your login password '
read passwd case
' $PASSWD "in
*"
echo "You don't belong Here"
;;
Yaoming ")
echo" Hello yaoming, you are so Tall "
;;
Liuxiang ")
echo" Hello Liuxiang, you are so fast "
;;
Wangliqin ")
echo" Hello Wangliqin, you play pingpong so OK "
;
Esac
[Taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[Taoge@localhost learn_shell]$
[Taoge@localhost learn_shell]$./a.sh
Enter your login password
yaoming you don't
belong here
[Taoge@localhost learn_shell]$
4. Test command and [] command
First of all, the test command is completely equivalent to the [] command, you can choose one, of course, everyone has a different hobby, I personally prefer [] command, below, we take a look at:
[Taoge@localhost learn_shell]$ cat a.sh
#!/bin/bash
echo ' Enter your login password '
read passwd
if Test "$passwd" = = "Linux"; Then
echo Yes
else
echo no
fi
[Taoge@localhost learn_shell]$
[Taoge@localhost Learn_shell] $
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$./a.sh
Enter your login password
Linux
Yes
[taoge@localhost learn_shell]$./a.sh
Enter your login password
linux
no
[Taoge@localhost learn_shell]$
I look at the test is very awkward, or use [] it. In use [] must pay attention to: [Back if there is no space, it will be grammatical errors, but, if the = = around no space, that is fatal logic error, at this time if will forever be true, tens of thousands of attention ah. Linux shell script that's it, snake everywhere, watch it bite you.
Finally, we end this article with a slightly more complex program:
[Taoge@localhost learn_shell]$ cat a.sh #! /bin/bash # Compare String Name= "" If ["$name" = ""]; Then echo log1 else echo log2 fi if [-Z "$name"]; Then echo log3 else echo log4 fi name= "Taoge" if [-Z "$name"]; Then echo log5 else echo log6 fi if [-N "$name"]; Then echo Log7 else echo Log8 fi if ["$name" = = "Taoge"]; Then echo log9 else echo log10 Fi # compare number if ["$#"-eq 0]; Then echo log11 else echo log12 fi if ["$#"-lt 1]; Then echo log13 else echo log14 fi if [1-lt 2-a 2-lt 3]; Then echo log15 else echo log16 fi if [1-lt 2] && [2-LT 3]; Then echo log17 else echo log18 fi # Test directory or file pwd if [-e/home/taoge/desktop/learn_shell]; Then echo log19 else echo log20 fi if [-e/home/taoge/desktop/learn_shell/test.html]; Then echo log21 else echo log22 fi touch/home/taoge/desktop/learn_shell/test.html if [-e/home/taoge/desktop/learn_sh Ell/test.html]; Then echo log23 else echo log24 fi [TaoGe@localhost learn_shell]$ [Taoge@localhost learn_shell]$ [Taoge@localhost learn_shell]$ [Taoge@localhost Learn_shell ]$./a.sh log1 log3 log6 log7 log9 log11 log13 log15 log17/home/taoge/desktop/learn_shell log19 log22 log23 [taoge@local
Host learn_shell]$
All right, let's talk about this.