[英]Trace Words in Shell Script
我正在嘗試找到無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的個人模式。
還有其他檢查方法:
使用awk:
awk '/malloc/ || /free/{count++;}END{printf "%d",count}' fileundertest
這將在文件中搜索“ malloc”和“ free”單詞,並打印出計數。 如果計數是偶數,您可以說每個malloc都有一個空閑空間。
使用grep:
grep -c "malloc" fileundertest
這將計算文件中的malloc字
同樣,
grep -c "free" fileundertest
列出行號
grep -n "malloc" fileundertest
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.