【问题标题】:Trace Words in Shell ScriptShell 脚本中的跟踪词
【发布时间】:2013-10-07 12:35:49
【问题描述】:

我正在尝试寻找无 malloc 的系列。考虑到我们有 1000 个测试用例。人们可能会忘记释放 ptr。请帮我优化脚本。

正在测试的示例文件

Test(func_class,func1)
{
  int i,j;
  char* ptr = (char*) malloc(sizeof(char));
  free(ptr);
}

Test(func_class,func1)
{
  int i,j;
  char* ptr = (char*) malloc(sizeof(char));
  / Memory Leak / 
}

脚本正在开发中:

export MY_ROOT=`pwd`
_COUNT=0
pwd
_COUNT_WORD=0
filename=test.c
cat $filename | while read line
do 
    echo "Reading Line = $LINE" 
    for word in $line
    do
    _COUNT_WORD=$(($_COUNT_WORD+1))
    echo $_COUNT_WORD $word    
    if [ "$word" == "malloc\(sizeof\(char\)\);" ]; then    
        _MALLOC_FLAG=1   #this part of the code is not reached
        echo "Malloc Flag Hi"
        echo $word[2]
    fi
    done
    _COUNT_WORD=0
done 

我在匹配 malloc 正则表达式时遇到了一些问题。我知道脚本需要大量修改,因为我们必须找到个人编写 malloc 的模式。

【问题讨论】:

    标签: regex shell


    【解决方案1】:

    还有其他检查方法:

    使用 awk:

    awk '/malloc/ || /free/{count++;}END{printf "%d",count}' fileundertest
    

    这将在文件中搜索“malloc”和“free”字样并打印出计数。如果 count 是偶数,你可以说每个 malloc 都是免费的。

    使用 grep:

    grep -c "malloc" fileundertest 
    

    这将计算文件中的 malloc 单词

    同样,

    grep -c "free" fileundertest
    

    列出行号

    grep -n "malloc" fileundertest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多