首页 > 代码库 > Shell编程 之 条件表达式

Shell编程 之 条件表达式

  1 #!/bin/bash - 
  2 #===============================================================================
  3 #
  4 #          FILE: condition_expression_test.sh
  5 
  6 #         USAGE: ./condition_expression_test.sh 
  7 
  8 #   DESCRIPTION: 
  9 
 10 #       OPTIONS: ---
 11 #  REQUIREMENTS: ---
 12 #          BUGS: ---
 13 #         NOTES: ---
 14 #        AUTHOR: daojoo, daojoo@qq.com
 15 #  ORGANIZATION: lit
 16 #       CREATED: 2012年03月10日 22时55分23秒 CST
 17 #      REVISION:  ---
 18 #===============================================================================
 19 
 20 set -o nounset                              # Treat unset variables as an error
 21 
 22 #以下内容来源于bash的man手册 [man bash]
 23 
 24 #CONDITIONAL EXPRESSIONS 
 25 #Conditional expressions are used by the [[ compound
 26 #command and the test and [ builtin commands to test file attributes and perform
 27 #string and arithmetic comparisons. Expressions are formed from the following
 28 #unary or binary primaries. If any file argument to one of the primaries is of
 29 #the form /dev/fd/n, then file descriptor n is checked. If the file argument to
 30 #one of the primaries is one of /dev/stdin, /dev/stdout, or /dev/stderr, file
 31 #descriptor 0, 1, or 2, respectively, is checked.
 32 #条件表达式
 33 #1.用于何处?
 34 #   用于[[组合命令中
 35 #   用于test和[这两个内建命令中
 36 #2.用来干什么?
 37 #   测试文件属性
 38 #   字符串和算术比较
 39 #3.如何构建?
 40 #   由参数、unary primaries、binary primaries构建
 41 #
 42 #Unless otherwise specified, primaries that operate on files follow symbolic
 43 #links and operate on the target of the link, rather than the link itself.
 44 #如果primaries后面的参数是一个符号链接,除非另行指明,否则运算符将作用于目标文件,
 45 #而非链接本身
 46 #
 47 #When used with [[, The < and > operators sort lexicographically using the
 48 #current locale.
 49 #当使用[[p这个组合命令时,< 和 > 这两个二元操作符会使用当前locale将它的两个按字典排序
 50 #
 51 #See the description of the test builtin command (section SHELL BUILTIN COMMANDS
 52 #below) for the handling of parameters (i.e. missing parameters).
 53 
 54 #以下是primaries及测试
 55 
 56 #-a file
 57 #    True if file exists. 
 58 #    [all 只要文件存在]文件存在时为true
 59 #-e file
 60 #    True if file exists. 
 61 #    [exist 存在]作用同-a,存在即为true
 62 touch test.txt;
 63 if [ -a test.txt ]; then
 64     echo "test.txt exists.";
 65 fi
 66 rm -f test.txt;
 67 if [ ! -a test.txt ]; then
 68     echo "test.txt not exists.";
 69 fi
 70 #output:
 71 #test.txt exists.
 72 #test.txt not exists.
 73 
 74 #-b file
 75 #    True if file exists and is a block special file. 
 76 #    [block 块]文件存在且是块文件时为true
 77 BLOCK_FILE=/dev/sda; 
 78 if [ -b $BLOCK_FILE ]; then
 79     echo "$BLOCK_FILE is a block file.";
 80 fi
 81 unset BLOCK_FILE;
 82 #output:
 83 #/dev/sda is a block file.
 84 
 85 #-c file
 86 #    True if file exists and is a character special file. 
 87 #    [character 字符]存在且是字符文件时为true
 88 
 89 
 90 #-d file
 91 #    True if file exists and is a directory. 
 92 #    [directory 目录]存在且是目录时为true
 93 DIR=/
 94 if [ -d $DIR ]; then
 95     echo "$DIR is a directory.";
 96 fi
 97 unset DIR;
 98 #output:
 99 #/ is a directory.
100 
101 #-f file
102 #    True if file exists and is a regular file. 
103 #    [file 普通文件]存在且是普通文件时为true
104 REGULAR_FILE=/etc/profile ;
105 NONREGULAR_FILE=/dev/sda ;
106 if [ -f $REGULAR_FILE ]; then
107     echo "$REGULAR_FILE is a regular file." ;
108 else
109     echo "$REGULAR_FILE is not a regular file." ;
110 fi
111 if [ -f $NONREGULAR_FILE ]; then
112     echo "$NONREGULAR_FILE is a regular file." ;
113 else
114     echo "$NONREGULAR_FILE is not a regular file." ;
115 fi
116 unset REGULAR_FILE ;
117 unset NONREGULAR_FILE ;
118 #output:
119 #/etc/profile is a regular file.
120 #/dev/sda is not a regular file.
121 
122 #-g file
123 #    True if file exists and is set-group-id.
124 #    [setgid]存在且文件属性为"......s..."时为true,其中.代表-,r,w,x中的任意一个
125 touch setgid.txt ;
126 chmod 6755 setgid.txt ;
127 if [ -g setgid.txt ]; then
128     echo "setgid.txt is set-group-id."
129 else
130     echo "setuid.txt is not set-user-id."
131 fi
132 chmod 0755 setgid.txt ;
133 if [ -g setgid.txt ]; then
134     echo "setgid.txt is set-group-id."
135 else
136     echo "setuid.txt is not set-user-id."
137 fi
138 rm -f setgid.txt ;
139 #output:
140 #setgid.txt is set-group-id.
141 #setuid.txt is not set-user-id.
142 
143 #-L file
144 #    True if file exists and is a symbolic link. 
145 #    [link 链接]同-h,存在且是符号链接时为true
146 #-h file
147 #    True if file exists and is a symbolic link. 
148 #    存在且是符号链接时为true
149 touch source.txt ;
150 ln -s source.txt target.txt ;
151 SOURCE=source.txt ;
152 TARGET=target.txt ;
153 if [ -h $SOURCE ] ; then
154     echo "$SOURCE is a symbolic link." ;
155 else
156     echo "$SOURCE is not a symbolic link." ;    
157 fi
158 if [ -h $TARGET ] ; then
159     echo "$TARGET is a symbolic link." ;
160 else
161     echo "$TARGET is not a symbolic link." ;    
162 fi
163 rm -f $SOURCE $TARGET ;
164 unset SOURCE TARGET ;
165 #output:
166 #source.txt is not a symbolic link.
167 #target.txt is a symbolic link.
168 
169 
170 #-k file
171 #    True if file exists and its ``sticky‘‘ bit is set. 
172 #    存在且文件属性中设置了t位时为true
173 TMP=/tmp ;
174 if [ -k $TMP ] ; then
175     echo "$TMP‘s \"sticky\" bit is set."    
176 else
177     echo "$TMP‘s \"sticky\" bit is not set."    
178 fi
179 unset TMP ;
180 
181 #-p file
182 #    True if file exists and is a named pipe (FIFO).
183 #    [pipe 命名管道]存在且是命名管道时为true
184 #以下脚本需要以root身份执行
185 if [ $USER = "root" ]; then
186     FIFO=/proc/1/fd/4 ;
187     if [ -p $FD ] ; then
188         echo "$FD is a named pipe."
189     else
190         echo "$FD is not a named pipe."
191     fi
192     unset FIFO;
193 fi
194 
195 
196 #-r file
197 #    True if file exists and is readable. 
198 #    [readable 可读]存在且可读时为true,注意不是只读
199 touch readable.txt ;
200 touch unreadable.txt ;
201 chmod 444 readable.txt ;
202 chmod 000 unreadable.txt ;
203 READABLE=readable.txt ;
204 UNREADABLE=unreadable.txt ;
205 if [ -r $READABLE ] ; then
206     echo "$READABLE is readable."
207 else
208     echo "$READABLE is unreadable."
209 fi
210 if [ -r $UNREADABLE ] ; then
211     echo "$UNREADABLE is readable."
212 else
213     echo "$UNREADABLE is unreadable."
214 fi
215 rm -f $READABLE $UNREADABLE ;
216 unset READABLE ;
217 unset UNREADABLE ;
218 #output:
219 #readable.txt is readable.
220 #unreadable.txt is unreadable.
221 
222 #-s file
223 #    True if file exists and has a size greater than zero. 
224 #    存在且不为空时为true
225 touch test_file ;
226 TEST_FILE=test_file ;
227 if [ -s $TEST_FILE ] ; then
228     echo "$TEST_FILE has something."
229 else
230     echo "$TEST_FILE has nothing."
231 fi
232 echo "something" > $TEST_FILE ;
233 if [ -s $TEST_FILE ] ; then
234     echo "$TEST_FILE has something."
235 else
236     echo "$TEST_FILE has nothing."
237 fi
238 rm -f $TEST_FILE ;
239 unset TEST_FILE ;
240 #output:
241 #test_file has nothing.
242 #test_file has something.
243 
244 #-t fd
245 #    True if file descriptor fd is open and refers to a terminal. 
246 #    [terminal 终端]文件描述符已打开且关联了一个终端时为true
247 #    可用于判断一个脚本是运行于交互模式(终端模式)还是非交互模式(cron或at)
248 if [ $USER = "root" ]; then
249     FD=/proc/1/fd/4
250     if [ -t 0 ] ; then
251         echo "File descriptor $FD is open and refers to a terminal."
252     else
253         echo "File descriptor $FD is not open or not refers to a terminal."
254     fi
255     unset FD ;
256 fi
257 if [ -t 1 ] ; then
258     echo "File descriptor 1 is open and refers to a terminal."
259 else
260     echo "File descriptor 1 is not open or not refers to a terminal."
261 fi
262 
263 #-u file
264 #    True if file exists and its set-user-id bit is set. 
265 #    [setuid]存在且文件属性为"...s......"时为true,其中.代表-,r,w,x中的任意一个
266 SETUID=/usr/bin/passwd ;
267 if [ -u $SETUID ] ; then
268     echo "$SETUID‘s set-user-id bit is set." ;
269 else
270     echo "$SETUID‘s set-user-id bit is not set." ;
271 fi
272 unset SETUID ;
273 #output:
274 #/usr/bin/passwd‘s set-user-id bit is set.
275 
276 #-w file
277 #    True if file exists and is writable. 
278 #    [writable 可写]存在且可写时为true
279 touch writable.txt unwritable.txt ;
280 WRITABLE=writable.txt ;
281 chmod 666 $WRITABLE ;
282 UNWRITABLE=unwritable.txt ;
283 chmod 444 $UNWRITABLE ;
284 if [ -w $WRITABLE ] ; then
285     echo "$WRITABLE is writable."
286 else
287     echo "$WRITABLE is not writable."
288 fi
289 if [ -w $UNWRITABLE ] ; then
290     echo "$UNWRITABLE is writable."
291 else
292     echo "$UNWRITABLE is not writable."
293 fi
294 rm $WRITABLE $UNWRITABLE ;
295 unset WRITABLE UNWRITABLE ;
296 #output:
297 #writable.txt is writable.
298 #unwritable.txt is not writable.
299 
300 #-x file
301 #    True if file exists and is executable.
302 #    [executable 可执行]存在且可执行时为true
303 if [ -x $0 ] ; then
304     echo "$0 is executable.";
305 else
306     echo "$0 is not executable.";
307 fi
308 
309 #-O file
310 #    True if file exists and is owned by the effective user id.
311 #    [owner 属于]存在且文件的所有者为执行脚本的用户时为true
312 if [ -O $HOME ] ; then
313     echo -e "$HOME is owned by the effective user \"$USER\".";
314 else
315     echo -e "$HOME is not owned by the effective user \"$USER\".";
316 fi
317 
318 #-G file
319 #    True if file exists and is owned by the effective group id. 
320 #    [group 所属组]存在且文件的所属组为执行脚本的用户所在的组时为true
321 EFFECTIVE_GROUP=`groups | cut -f1 -d  ` ;
322 if [ -G $HOME ] ; then
323     echo "$HOME is owned by the effective group \"$EFFECTIVE_GROUP\"." ;
324 else
325     echo "$HOME is not owned by the effective group \"$EFFECTIVE_GROUP\"." ;
326 fi
327 unset EFFECTIVE_GROUP ;
328 
329 
330 #-S file
331 #    True if file exists and is a socket. 
332 #    [socket 套接字]存在且是套接字文件时为true
333 
334 #-N file
335 #    True if file exists and has been modified since it was last read. 
336 #    存在且最后一次读取该文件时改变了文件的内容时为true
337 touch test.txt ;
338 TEST_FILE=test.txt ;
339 echo "test.txt" > $TEST_FILE ;
340 if [ -N $TEST_FILE ] ; then
341     echo "$TEST_FILE exists and has been modified since it was last read. "
342 else
343     echo "$TEST_FILE not exists or has not been modified since it was last read. "
344 fi
345 sleep 1 ;                                       # 休眠1秒钟,否则看不出效果。
346 cat $TEST_FILE > /dev/null ;
347 if [ -N $TEST_FILE ] ; then
348     echo "$TEST_FILE exists and has been modified since it was last read. "
349 else
350     echo "$TEST_FILE not exists or has not been modified since it was last read. "
351 fi
352 rm $TEST_FILE ;
353 unset TEST_FILE ;
354 #output:
355 #test.txt exists and has been modified since it was last read. 
356 #test.txt not exists or has not been modified since it was last read. 
357 
358 #file1 -nt file2
359 #    True if file1 is newer (according to modification date) than file2, or if
360 #    file1 exists and file2 does not. 
361 #    当file1比file2新或file1存在而file2不存在时为true
362 
363 #file1 -ot file2
364 #    True if file1 is older than file2, or if file2 exists and file1 does not.
365 #    当file1比file2旧或file2存在而file1不存在时为true
366 
367 #file1 -ef file2
368 #    True if file1 and file2 refer to the same device and inode numbers.
369 #    当file1和file2引用相同的设备或具有相同的节点号时为true
370 
371 #-o optname
372 #    True if shell option optname is enabled. See the list of options under the
373 #    description of the -o option to the set builtin below. 
374 
375 #-z string
376 #    True if the length of string is zero. 
377 #    [zero]字符串长度等于0时为true
378 
379 #string 
380 #-n string
381 #    True if the length of string is non-zero.
382 #    字符串不为空时为true
383 
384 #string1 == string2 
385 #string1 = string2
386 #    True if the strings are equal. = should be used with the test command for
387 #    POSIX conformance.
388 #    string1和string2相同时为true
389 
390 #string1 != string2
391 #    True if the strings are not equal. 
392 #    string1和string2不同时为true
393 
394 #string1 < string2
395 #    True if string1 sorts before string2 lexicographically. 
396 #    string1在字典中排在string2之前时为true
397 
398 #string1 > string2
399 #    True if string1 sorts after string2 lexicographically. 
400 #    string1在字典中排在string2之后时为true
401 
402 #arg1 OP arg2
403 #    OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary
404 #    operators return true if arg1 is equal to, not equal to, less than, less
405 #    than or equal to, greater than, or greater than or equal to arg2,
406 #    respectively. Arg1 and arg2 may be positive or negative integers. 
407 #       -eq     equal to                    等于
408 #       -ne     not equal to                不等于
409 #       -lt     less than                   小于
410 #       -le     less than or equal to       小于或等于
411 #       -gt     greater than                大于
412 #       -ge     greater than or equal to    大于或等于


来自为知笔记(Wiz)