免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1072 | 回复: 0
打印 上一主题 下一主题

用gawk写了个函数,检查传给gawk的数据文件是否存在的函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-03 20:20 |只看该作者 |倒序浏览
由于对gawk读了错误的文件后,报错的形式看了不爽写了一个检查文件是否存在的函数。
该函数是我写的一个几百多行的gawk脚本中的一个模块函数,不过是花时间最多的一部分,目前功能
可以实现,支持文件名中不含这样的一些<, >, {, }, [, ],.,=等特殊符号的数据文件名。
  中间用到了一些动态正则表达式,如下:
#if(CfgFile~/^\.\//)
  #{rep=sprintf("./%s\\>",substr(CfgFile,3));printf "rep %s\n",rep}
  #else
  #rep="\\<[ \t]*"  CfgFile "\\>"
  rep=sprintf("%s\\>",CfgFile)
if(tmpRes!~rep)

虽然现在功能可以实现,但是我对动态正则表达式有些疑惑,
就是像#rep="\\<[ \t]*"  CfgFile "\\>"这种表达式,不能对CfgFile是绝对路径/xxx/xxx,或者相对路径
的./xxx,起作用,就是if(tmpRes!~rep)不能正确判断!
  但是将rep改为这样就可以了"[ \t]*"  CfgFile "\\>"
还请各位高手指点迷津啊

下面有一些注释掉的部分,放在中间,能看见我的修改的转变,也就没有去掉
格式不好,见谅
---------------------------------------------------------------------------------------------------------

  1. #main for test
  2. BEGIN
  3. {
  4. for(i=1;i<ARGC;i++)
  5. sts=CheckCfgFile(ARGV[i])
  6. }

  7. #
  8. #支持函数 检查脚本配置文件是否存在
  9. #
  10. function CheckCfgFile(CfgFile,tmpCmd,tmpRes,retSts,idx,rep)
  11. {
  12.   
  13. if(CfgFile~/[/][/]+/)
  14.   {
  15.    printf "ERROR: The path of \"%s\" is not true!\n",CfgFile>"/dev/stderr"
  16.    printf "\tPlease check the path of \"%s\"\n" ,CfgFile>"/dev/stderr"
  17.    retSts =1
  18.    return retSts
  19.   }
  20.   else if(CfgFile~/[^/]\/$/)
  21.   {
  22.    printf "ERROR: \"%s\" is not a ordinary file!\n",CfgFile>"/dev/stderr"
  23.    printf "\tIt's maybe a directory file!\n" >"/dev/stderr"
  24.    retSts =2
  25.    return retSts
  26.   }
  27.   
  28.   idx=match(CfgFile,/[^/]+$/)
  29.   #if(CfgFile!~/^\.\//)

  30.   if(idx&&index(CfgFile,"/"))
  31.   {
  32.    tmpCmd=sprintf("echo %s%s",substr(CfgFile,1,idx-1),"*")
  33.    #printf "tmpCmd %s\n",tmpCmd
  34.   }
  35.   else
  36.   {
  37.    tmpCmd=sprintf("echo %s","*")
  38.    #printf "tmpCmd %s\n",tmpCmd
  39.   }

  40.   #tmpCmd=sprintf("ls -ld %s ",CfgFile)
  41.   tmpCmd | getline tmpRes
  42.   #printf "tmpRes %s\n",tmpRes
  43.   close(tmpCmd)
  44.   #if(CfgFile~/^\.\//)
  45.   #{rep=sprintf("./%s\\>",substr(CfgFile,3));printf "rep %s\n",rep}
  46.   #else
  47.   #rep="\\<[ \t]*"  CfgFile "\\>"
  48.   rep=sprintf("%s\\>",CfgFile)

  49.   if(tmpRes!~rep)
  50.   {
  51.    printf "ERROR:  \"%s\" is not exist!\n",CfgFile>"/dev/stderr"
  52.    printf "\tPlease check it!\n" >"/dev/stderr"
  53.    retSts =3
  54.    return retSts
  55.    }
  56.    
  57.   tmpCmd=sprintf("ls -ld %s",CfgFile)
  58.   tmpCmd | getline tmpRes
  59.   close(tmpCmd)
  60.   if(tmpRes !~/^-/)
  61.   {
  62.    printf "ERROR:  \"%s\" is not a ordinary file!\n",CfgFile>"/dev/stderr"
  63.    printf "\t\"%s\" maybe a directory file,Please check it!\n",CfgFile>"/dev/stderr"
  64.    retSts =4
  65.    return retSts
  66.    }
  67.   return retSts
  68. }
复制代码

----------------------------------------------------------------------------------------------------

[ 本帖最后由 mefit 于 2005-11-3 20:57 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP