Sh编程与EXT2文件系统全解析
1. sh编程基础
在sh编程中,我们会遇到各种有趣的问题和实用的技巧。下面我们先来看一个练习。
testFile() # test whether $1 is a REG file; return 0 if yes, 1 if not { if [ -f $1 ]; then return 0 else return 1 } } for A in f1 D2 # assume f1 is a REG file, D2 is a DIRectory do testFile $A # testFile return $?=0 or 1 if [ $? = 0 ]; then echo $A is a REG file else echo $A is not a REG file fi done这个程序的目的是测试一个文件是否为普通文件(REG file)。然而,结果总是显示 “$A is a REG file”,即使 $A 是一个目录。这是因为代码中存在语法错误,else语句块缺少fi来结束if语句。修改后的代码如下:
testFile() # test whether $1 is a REG file; return 0 if yes, 1 if not { if