功能: grep [OPTION]... PATTERN [FILE] ... Search for PATTERN in each FILE or standard input. Example: grep -i 'hello world' menu.h main.c -v 显示所有不包含指定 string 的行。 -c 计算包含指定 string 的行并显示总数。 -n 将文件行号置于每行开头。 -i 指定搜索不区分大小写。 可以实现多参数的情况,如 grep -v -i menu.h main.c 1.该批处理文件的思想是:通过对各参数的判断,构造出DOS中对应的命令字符串(var),运行之。 2.由于事先无法确定命令有多少参数,用shift语句让参数左移,对$1参数的情况进行判断,将其对应DOS下的命令参数加到将要运行的命令字符串的后面。 3.由于不存在case语句,所以通过一系列的if语句来实现。而循环则是通过call语句实现。 4.由于grep和find语句的参数大部分一一对应,因此所需要考虑的相对于在bash下实现dir功能要简单一些。 代码: @echo off if "%1"=="" goto forhelp if "%1"=="--help" goto help set %var=find goto findoperation goto end :forhelp ::forhelp switch echo Usage: grep [OPTION]... PATTERN [FILE]... echo Try `grep --help' for more information. goto end
:help ::help switch echo Usage: grep [OPTION]... PATTERN [FILE] ... echo Search for PATTERN in each FILE or standard input. echo Example: grep -i 'hello world' menu.h main.c echo -v 显示所有不包含指定 string 的行。 echo -c 计算包含指定 string 的行并显示总数。 echo -n 将文件行号置于每行开头。 echo -i 指定搜索不区分大小写。 goto end :findoperation if "%1"=="" call %var%&goto end if "%1"=="-v" set var=%var% /V&shift&goto findoperation if "%1"=="-n" set var=%var% /N&shift&goto findoperation if "%1"=="-i" set var=%var% /I&shift&goto findoperation if "%1"=="-c" set var=%var% /C&shift&goto findoperation set var=%var% "%1"&shift&goto findoperation :end ::the end of the .bat 
|