通过关键字查询游戏的配置,方便从其他gamelist中提取配置,以定制gamelist.xml
#!/bin/bash
## filename: queryGame.sh
## 使用: ./queryGame.sh <gamelist.xml搜索目录> <游戏名>
## 功能:在目录下搜索gamelist.xml,并查找对应游戏的配置信息
## 举例:./queryGame.sh "/Volumes/EEROMS" 超级玛丽
## 注意:需要安装xmlstarlet
function pickgamelist(){
gamelistxml="${1:-gamelist.xml}"
game=$2
gamelistdir="$( cd "$(dirname "${gamelistxml}")" && pwd )"
gamelistpath="${gamelistdir}/gamelist.xml"
echo "处理文件:""${gamelistpath}"
# 修改编码为utf-8,以支持中文, macos请使用gsed,安装方法:brew install gsed
gsed -i '1c <?xml version="1.0" encoding="utf-8"?>' "${gamelistxml}"
gameList=`xmlstarlet sel -t \
-v "/gameList/game[contains(name, \"${game}\")]/name" \
--nl "${gamelistxml}"`
OLDIFS="$IFS" #备份旧的IFS变量
IFS=$'\n' #处理游戏名中带空格的情况
# 逐个修改name的值
for game in ${gameList}
do
echo "处理游戏:"${game}
gameconfig=$( xmlstarlet sel -t \
-c "/gameList/game[name = \"${game}\"]" \
--nl \
"${gamelistxml}" )
_path=$( echo $gameconfig | xmlstarlet sel -t \
-v "//path" \
--nl )
gpath=${gamelistdir}/$_path
_image=$( echo $gameconfig | xmlstarlet sel -t \
-v "//image" \
--nl )
gimage=${gamelistdir}/$_image
echo $gameconfig | xml fo -o -s 4 #缩进
echo cp \"$gpath\" ${_path%/*}
echo cp \"$gimage\" ${_image%/*}
done
IFS="$OLDIFS" #还原IFS变量
}
searchdir=$1 # 搜索目标目录
searchgame=$2 # 游戏名
for f in $( find "${searchdir}" -type f -name "gamelist.xml")
do
pickgamelist $f ${searchgame}
done