码迷,mamicode.com
首页 > 其他好文 > 详细

shell基础

时间:2014-08-29 00:00:09      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   java   io   ar   for   

  1 第一个shell脚本
  2 #!/bin/bash
  3 echo "Hello World"
  4 
  5 使用read从标准输入获取输入并赋值给变量,然后输出
  6 #!/bin/bash
  7 
  8 # Author : zjw
  9 # Copyright @ www.litsoft.com
 10 # Script follow here:
 11 
 12 echo "what is your name?"
 13 read PERSON
 14 echo "Hello, $PERSON"
 15 
 16 使用变量,并使用了for循环
 17 #!/bin/bash
 18 
 19 for skill in Ada Coffe Action Java
 20 do
 21     echo "I am good at ${skill} Script."
 22 done
 23 
 24 echo "Over"
 25 
 26 重定义变量
 27 #!/bin/bash
 28 
 29 myUrl="Http://www.litsoft.com"
 30 echo ${myUrl}
 31 
 32 myUrl="Http://www.myblog.com"
 33 echo ${myUrl}
 34 
 35 echo "Over"
 36 
 37 
 38 只读变量
 39 #!/bin/bash
 40 
 41 myUrl="Http://www.litsoft.com"
 42 echo ${myUrl}
 43 
 44 readonly myUrl
 45 
 46 myUrl="Http://www.myblog.com"
 47 echo ${myUrl}
 48 
 49 echo "Over"
 50 
 51 
 52 删除变量
 53 #!/bin/bash
 54 
 55 myUrl="Http://www.litsoft.com"
 56 echo ${myUrl}
 57 
 58 unset myUrl
 59 
 60 #myUrl="Http://www.myblog.com"
 61 echo ${myUrl}
 62 
 63 echo "Over"
 64 
 65 
 66 特殊变量,命令行参数
 67 #!/bin/bash
 68 
 69 echo "File Name : $0"
 70 echo "First Parameter : $1"
 71 echo "Second Parameter : $2"
 72 echo "Quoted Values : $@"
 73 echo "Quoted Values : $*"
 74 echo "Total Number of Parameters : $#"
 75 
 76 
 77 变量替换
 78 #!/bin/bash
 79 
 80 a=10
 81 echo -e "Value of a is $a \n"
 82 echo "Over"
 83 
 84 
 85 命令替换
 86 #!/bin/bash
 87 DATE=date
 88 echo "Date is $DATE"
 89 
 90 USERS=who | wc -l
 91 echo "Logged in user are $USERS"
 92 
 93 UP=date; uptime
 94 echo "Uptime is &UP"
 95 
 96 
 97 变量替换
 98 #!/bin/bash
 99 
100 echo ${var:-"Variable is not set"}
101 echo "1 - Value of var is ${var}"
102 
103 echo ${var:="Variable is not set"}
104 echo "2 - Value of var is ${var}"
105 
106 unset var
107 echo ${var:+"This is default value"}
108 echo "3 - Value of var is $var"
109 
110 var="Prefix"
111 echo ${var:+"This is default value"}
112 echo "4 - Value of var is $var"
113 
114 echo ${var:?"Print this message"}
115 echo "5 - Value of var is ${var}"
116 
117 
118 ===========================shell运算符============================================
119 算术运算符
120 #!/bin/bash
121 
122 val= expr 2 + 2
123 
124 echo "Total value : $val"
125 
126 #!/bin/bash
127 
128 a=10
129 b=20
130 val= expr $a + $b
131 echo "a + b : $val"
132 
133 val= expr $a - $b
134 echo "a - b : $val"
135 
136 val= expr $a \* $b
137 echo "a * b : $val"
138 
139 val= expr $b / $a
140 echo "b / a : $val"
141 
142 val= expr $b % $a
143 echo "b % a : $val"
144 
145 if [ $a == $b ]
146 then
147     echo "a is equal to b"
148 fi
149 
150 if [ $a != $b ]
151 then
152     echo "a is not equal to b"
153 fi
154 
155 
156 关系运算符
157 #!/bin/sh
158 
159 a=10
160 b=20
161 if [ $a -eq $b ]
162 then
163     echo "$a -eq $b : a is equal to b"
164 else
165     echo "$a -eq $b : a is not equal to b"
166 fi
167 
168 if [ $a -ne $b ]
169 then
170     echo "$a -ne $b : a is not equal to b"
171 else
172     echo "$a -ne $b : a is equal to b"
173 fi
174 
175 if [ $a -gt $b ]
176 then
177     echo "$a -gt $b : a is greater than b"
178 else
179     echo "$a -gt $b : a is not greater than b"
180 fi
181 
182 if [ $a -lt $b ]
183 then
184     echo "$a -lt $b : a is less than b"
185 else
186     echo "$a -lt $b : a is not less than b"
187 fi
188 
189 if [ $a -ge $b ]
190 then
191     echo "$a -ge $b : a is greater or equal to b"
192 else
193     echo "$a -ge $b : a is not greater or equal to b"
194 fi
195 
196 if [ $a -le $b ]
197 then
198     echo "$a -le $b : a is less or equal to b"
199 else
200     echo "$a -le $b : a is no less or equal to b"
201 fi
202 
203 
204 布尔运算符
205 #!/bin/bash
206 
207 a=10
208 b=20
209 
210 if [ $a != $b ]
211 then
212     echo "$a != $b : a is not equal to b"
213 else
214     echo "$a != $b : a is equal to b"
215 fi
216 
217 if [ $a -lt 100 -a $b -gt 15 ]
218 then
219     echo "$a -lt 100 -a $b -gt 15 : return true"
220 else
221     echo "$a -lt 100 -a $b -gt 15 : return false"
222 fi
223 
224 if [ $a -lt 100 -o $b -gt 100 ]
225 then
226     echo "$a -lt 100 -o $b -gt 100 : return true"
227 else
228     echo "$a -lt 100 -o $b -gt 100 : return false"
229 fi
230 
231 if [ $a -lt 5 -o $b -gt 100 ]
232 then
233     echo "$a -lt 100 -o $b -gt 100 : return true"
234 else
235     echo "$a -lt 100 -o $b -gt 100 : return false"
236 fi
237 
238 
239 字符串运算符
240 #!/bin/bash
241 
242 a="abc"
243 b="efg"
244 
245 if [ $a = $b ]
246 then
247     echo "$a = $b : a is equal to b"
248 else
249     echo "$a = $b : a is not equal to b"
250 fi
251 
252 if [ $a != $b ]
253 then
254     echo "$a != $b : a is not equal to b"
255 else
256     echo "$a != $b : a is equal to b"
257 fi
258 
259 if [ -z $a ]
260 then
261     echo "-z $a : string length is zero"
262 else
263     echo "-z $a : string length is not zero"
264 fi
265 
266 if [ -n $a ]
267 then
268     echo "-n $a : string length is not zero"
269 else
270     echo "-n $a : string length is zero"
271 fi
272 
273 if [ $a ]
274 then
275     echo "$a : string is not empty"
276 else
277     echo "$a : string is empty"
278 fi
279 
280 
281 文件测试运算符
282 #!/bin/bash
283 
284 file="/mnt/hgfs/work/mycode/shell/1.sh"
285 
286 if [ -r $file ]
287 then
288     echo "File has read access"
289 else
290     echo "File does not have read access"
291 fi
292 
293 if [ -w $file ]
294 then
295     echo "File has write permission"
296 else
297     echo "File does not have write permission"
298 fi
299 
300 if [ -x $file ]
301 then
302     echo "File has execute permission"
303 else
304     echo "File does not has execute permission"
305 fi
306 
307 if [ -f $file ]
308 then
309     echo "File is an ordinary file"
310 else
311     echo "This is a spcial file"
312 fi
313 
314 if [ -d $file ]
315 then
316     echo "File is a directory file"
317 else
318     echo "This is not a directory file"
319 fi
320 
321 if [ -s $file ]
322 then
323     echo "File size is zero"
324 else
325     echo "File size is not zero"
326 fi
327 
328 if [ -e $file ]
329 then
330     echo "File exists"
331 else
332     echo "File does not exist"
333 fi

 

shell基础

标签:style   blog   http   color   使用   java   io   ar   for   

原文地址:http://www.cnblogs.com/lit10050528/p/3943361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!