1. Shell basic format, variable
# Add an interpreter to the shell script #! /bin/bash
# Print out Hello world with a script
#! /bin/bash
echo "Hello World"
Exit
Parameters: A parameter is an entity that stores real values that are typically referenced in a script
#利用变量回显hello World, the following example will echo two Hello world.
#! /bin/bash
A= "Hello World"
Echo $a
echo "Hello World"
Exit
If additional content is appended to the argument, you need to use {}
#! /bin/bash
A= "Hello World"
Echo ${a},i am coming
Exit
Echo Result:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8B/62/wKioL1hMuz7woK_1AAAKPycrSYA319.png-wh_500x0-wm_3 -wmp_4-s_1341639348.png "title=" Hell.png "alt=" Wkiol1hmuz7wok_1aaakpycrsya319.png-wh_50 "/>
#计算字符串长度, hello World with spaces, total 11
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8B/63/wKioL1hMvLXyneNkAAAZ9zCP5LE624.png-wh_500x0-wm_3 -wmp_4-s_2360012519.png "title=" Count.png "alt=" Wkiol1hmvlxynenkaaaz9zcp5le624.png-wh_50 "/>
2. Script exit
#一个执行成功的脚本返回值为0, unsuccessful for non 0
[Email protected] script]# Clear
[Email protected] script]# sh hello.sh
Hello world,i am Coming
11
[[email protected] script]# echo $?
0
#判断一个目录内是否有某个文件, there is a echo of file Exsit, no return 1
[email protected] script]# cat test.sh
#! /bin/bash
Cd/home
If [-F file];then
echo "File Exsit"
Else
Exit 2
Fi
[Email protected] script]# sh test.sh
[[email protected] script]# echo $?
2
[[Email Protected]ost script]#
3. Test statement, if statement
test-d XX directory is present
test-f xx file is present
Test-b xx is a block file
Test-x xx is an executable file
A-eq B determine equality
A-ne B judged unequal
A-le B is less than or equal to
A-lt B is less than
A-GT B is greater than
#if条件判断语句
if [];then
......
Else
......
Fi
#if语句嵌套
if [];then
.......
elif [];then
.......
elif [];then
.......
Else
......
Fi
#test举例, determine whether the test file exists if there is a backup operation for it. If the message is not echoed
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/67/wKiom1hNArqz70XXAAAwDWrQKyI583.png-wh_500x0-wm_3 -wmp_4-s_2868148537.png "title=" Tesback.png "alt=" Wkiom1hnarqz70xxaaawdwrqkyi583.png-wh_50 "/>
#例子: Enter a numeric value to determine which interval it is in. Less than 80, greater than 100, located between 80 and 100, with the IF nesting!
[email protected] script]# cat if.sh
#! /bin/bash
Read-p "plz input a $num:" num
If [$num-le];then
echo "Num is less 80"
elif [$num-ge] && [$num-le];then
echo "Num between and 100"
Else
echo "num is greater than 100"
Fi
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8B/63/wKioL1hMzJySQSYkAAA52gU-bxQ461.png-wh_500x0-wm_3 -wmp_4-s_2334835825.png "title=" Ifles.png "alt=" Wkiol1hmzjysqsykaaa52gu-bxq461.png-wh_50 "/>
4. Loop statement for, while
For Var in $var 1 $var 2 $var 3 VAR4 ... $var
Do
......
......
Done
#例子: Print 1 to 10 digits
[email protected] home]# cat for1.sh
#! /bin/bash
For Var in {1..10}
Do
echo "$var"
Sleep 2
Done
[Email protected] home]# sh for1.sh
1
2
3
4
5
6
7
8
9
10
[Email protected] home]#
#打印方块内容 for nesting
[Email protected] home]# sh for2.sh
*********
*********
*********
*********
*********
*********
*********
*********
*********
[email protected] home]# cat for2.sh
#! /bin/bash
For ((i=1;i<10;i++))
Do
For ((j=1;j<10;j++))
Do
Echo-n "*"
Done
echo ""
Done
# while loop, mostly for loops, plus continue (jump out of continue below, return to top command line), break (Stop, exit Loop)
#while语句
While [conditional judgment]
Do
......
......
Done
#例子: According to the conditions, output 1-10
[email protected] script]# cat while.sh
#! /bin/bash
Var=1
While [$var-le 10]
Do
Echo $var
var=$[$var +1]
Done
[Email protected] script]# sh while.sh
1
2
3
4
5
6
7
8
9
10
[[email protected] script]# echo $?
0
This article is from the "unrestrained" blog, please be sure to keep this source http://keep11.blog.51cto.com/1443840/1881661
Shell Basics-variables, judgments, loops