linux shell脚本怎么判断字符串中存在gdb的coredump文件(core.数字)
假设有两个字符串
a=/home/core.48000
b=/home/core.py
可以看出a是gdb的coredump文件,b是py文件。
怎样用shell判断a/b是否是coredump文件,即怎样判断该字符串结尾是core.加数字。
有知道的麻烦帮帮忙,先谢过各位
你可以使用grep
命令来查找包含指定字符串模式的文件。例如,你可以使用以下命令来查找包含"core."和一个数字的文件:
grep -E 'core.[0-9]+' /path/to/coredump/directory
这个命令会在/path/to/coredump/directory目录中查找任何文件名中包含"core."和一个数字的文件。
如果你想在shell脚本中使用这个命令,你可以使用以下代码来判断是否找到了匹配的文件:
if grep -Eq 'core.[0-9]+' /path/to/coredump/directory; then
echo "Found matching coredump file"
else
echo "No matching coredump file found"
fi
这段代码会在/path/to/coredump/directory目录中查找任何文件名中包含"core."和一个数字的文件,并根据找到的结果输出相应的信息。
如下shell function可实现
function checkIsCoredump()
{
str=$1
str=${str##*/}
strnum=${str:5}
res=`echo "$strnum" | grep '[^0-9]'`
if [[ $res ]]; then
#非core.数字格式
tmp=0
else
#是core.数字格式
tmp=1
fi
}