File tree Expand file tree Collapse file tree 2 files changed +157
-2
lines changed Expand file tree Collapse file tree 2 files changed +157
-2
lines changed Original file line number Diff line number Diff line change 1+ # bash&shell 编程
2+
3+ ## 目录
4+ * [ hello_world] ( #hello_world )
5+ * [ 定义变量] ( #定义变量 )
6+ * [ 字符串运算] ( #字符串运算 )
7+ * [ 获得输入] ( #获得输入 )
8+
9+ ---
10+
11+ ## hello_world
12+
13+ 一个简单的demo,输出一个hello world。
14+ ```
15+ #!/bin/bash
16+ echo "hello world"
17+ ```
18+
19+ ## 定义变量
20+
21+ 注意 : var=hello world ** 等号两边不能有空格**
22+ ```
23+ var=hello world #等号两边不能有空格
24+ echo $var
25+ echo ${var} #两种打印变量的方式
26+
27+ echo "this is : $var" # 感觉都想php了
28+ ```
29+
30+ ## 字符串运算
31+ 注意 : [ $a1 = $b1 ] ** 等号两边必须有空格**
32+ ```
33+ a1="123"
34+ b1="dsafdakjkl"
35+
36+ #判断等于
37+ if [ $a1 = $b1 ]
38+ then
39+ echo "eq"
40+ else
41+ echo "not eq"
42+ fi
43+
44+ # 判断不等于
45+ if [ $a1 != $b1 ]
46+ then
47+ echo "not eq"
48+ fi
49+ ```
50+
51+ ## 数学运算
52+
53+ ```
54+ num1=100
55+ num2=200
56+
57+ if [ $num1 == $num2 ]
58+ then
59+ echo "$num1 等于 $num2"
60+ else
61+ echo "$num1 不等于 $num2"
62+ fi
63+
64+ if [ $num1 != $num2 ]
65+ then
66+ echo "$num1 等于 $num2"
67+ fi
68+
69+ # 大于小于
70+ if [ $num1 -lt $num2 ] # -gt 大于 ; -eq等于 -ne 不等于;
71+ then
72+ echo "$num1 小于 $num2"
73+ fi
74+
75+ # 与 和 或运算
76+
77+ num3=300
78+ num4=400
79+
80+ if [[ $num3 -lt $num4 && $num1 -lt $num2 ]]
81+ then
82+ echo "yes"
83+ fi
84+ ```
85+
86+ ## 条件和分支
87+
88+ if-else
89+ ```
90+ if condition
91+ then
92+ command1
93+ command2
94+ ...
95+ commandN
96+ else
97+ command
98+ fi
99+ ```
100+
101+ if else-if else
102+ ```
103+ if condition1
104+ then
105+ command1
106+ elif condition2
107+ then
108+ command2
109+ else
110+ commandN
111+ fi
112+ ```
113+
114+
115+ ## 获得输入
116+ ```
117+ echo "1 : $1"
118+ echo "2 : $2"
119+ echo "3 : $3"
120+ ```
121+
122+ ```
123+ ./bash_test.sh 100 200 300
124+ ```
125+
126+
127+ ### 写个小demo
128+ ```
129+ #!/bin/bash
130+
131+ flag1="make"
132+ flag2="clean"
133+ flag3="run"
134+
135+ if [ $flag1 = $1 ]
136+ then
137+ echo "flag1 = $1"
138+ g++ main.cc
139+ elif [ $flag2 = $1 ]
140+ then
141+ rm a.out
142+ else
143+ ./a.out
144+ fi
145+ ```
146+
147+ ```
148+ ./bash_test.sh make #编译
149+ ./bash_test.sh run #运行
150+ ```
151+
152+
153+
154+
155+ ## 参考
156+
157+ http://www.runoob.com/linux/linux-shell.html
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments