#!/bin/bash


# 删除文件管理器的搜索栏的搜索历史缓存

# 具体操作：

# 将当前用户的目录下的文件管理器的缓存文件中的相关记录删除掉。


echo "检查、删除文件管理器的搜索栏的搜索历史缓存："

startline=0

del_st=0

endline=0

del_end=0

linecount=0

restart=0

# 测试用

#filepath=~/Documents/dde-file-manager.obtusely.json

filepath=~/.config/deepin/dde-file-manager/dde-file-manager.obtusely.json

if [ ! -f  "${filepath}" ];then

echo ""

echo "文件${filepath}不存在。"

echo "已退出检查。"

else

# 在命令行中显示目前的缓存文件的内容。不想显示的话，将下面五行注释掉即可。

#echo "当前用户的文件管理器的搜索缓存文件内容如下："

#echo "文件名：${filepath}"

#echo ""

#cat ${filepath}

#echo ""

# 以上五行可注释掉。

while read line

do

linecount=$(expr ${linecount} + 1)

#        echo ${linecount}

if [ "${line}" == "\"SearchHistroy\": [" ]; then

startline=${linecount}

del_st=$(expr ${linecount} + 1)

#                echo ${startline}

fi

if [ "${line}" == "]" ]; then

endline=${linecount}

del_end=$(expr ${linecount} - 1)

#                echo ${endline}

fi

done < ${filepath}

del_is=0

del_is=$(expr ${endline} - ${startline} - 1)

if [ ${del_is} -gt 0 ]; then

sed -i ${del_st},${del_end}d ${filepath}

echo "已删除 ${del_is} 条搜索记录缓存！"

restart=1

else

echo "没有检查到搜索记录缓存。"

echo "小提醒：执行本脚本前，请关闭文件管理器。"

fi

echo "检查完成！"

if [ ${restart} -eq 1 ];then

# 在命令行中显示删除了搜索记录缓存后的文件内容。不想显示的话，注释掉下面五行即可。

#        echo "删除搜索记录缓存后的文件内容如下："

#        echo "文件名为${filepath}"

#        echo ""

#        cat ${filepath}

#        echo ""

# 以上五行可注释掉。

echo "现在可以重新打开文件管理器使用了。"

echo ""

fi

fi