我正在研究一个简单的 bash 脚本,该脚本修复了一个重复的命名问题。
该脚本本身会抓取在 gameslist.xml 文件中多次提及的任何名称,然后将这些名称存储在一个数组中以供以后使用。
我最终在索引中循环这个数组,如下所示:
pi@retropie:~ $ for game in ${game_array[@]:0:10} ; do echo $game; done
它将第一个元素拉到第 10 个元素(即${game_array[9]}
),但是输出连接到一行:
pi@retropie:~ $ for game in ${game_array[@]:0:10} ; do echo $game; done
R.B.I. Baseball '94 World Series Baseball '95 Mega Games 1 Bill Walsh College Football T2: The Arcade Game Sonic & Knuckles + Sonic the Hedgehog Sega Top Five Pyramid Magic Tecmo Super Baseball Super Chinese Tycoon
但是,如果我遍历整个数组,它会按预期在新行上输出:
pi@retropie:~ $ for game in ${game_array[@]}; do echo $game; done | head -10
R.B.I. Baseball '94
World Series Baseball '95
Mega Games 1
Bill Walsh College Football
T2: The Arcade Game
Sonic & Knuckles + Sonic the Hedgehog
Sega Top Five
Pyramid Magic
Tecmo Super Baseball
Super Chinese Tycoon
字段分隔符已设置为新行,IFS='$\n'
这就是第二个有效的原因,但我一生都无法弄清楚为什么它不适用于第一个?
这是上下文的完整测试脚本:
#!/bin/bash
user_input=$1
while [ -z "$user_input" ]; do
echo "please enter the name of the system you want to fix the game list for"
echo "(as it is labelled in /home/pi/RetroPie/roms)"
read -r user_input
done
ls "/home/pi/RetroPie/roms/$user_input" >/dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "this doesn't appear to be a system installed here. exiting."
exit 1
fi
games_to_fix()
{
IFS=$'\n'
console=$1
filepath="/opt/retropie/configs/all/emulationstation/gamelists/$console/gamelist.xml"
game_array=($(fgrep "<name>" "$filepath" | sort | uniq -c | sort -rn | awk '$1 > 1 {print $0}'| cut -d ">" -f 2 | cut -d "<" -f 1))
number_to_fix=($(fgrep "<name>" "$filepath" | sort | uniq -c | sort -rn | awk '$1 > 1 {print $1}'))
}
get_new_name()
{
mYpath=$1
new_name=$(echo $mYpath | cut -d ">" -f 2 | cut -d "<" -f 1 | sed -e 's/\.\///g' | sed -e 's/\.7z//g')
}
games_to_fix $user_input
IFS=$'\n'
index=0
for i in ${number_to_fix[@]}; do
loop=1
for game in ${game_array[@]:$index:$i}; do
# for ind in $(eval echo {1..$i}); do
line_number=$(fgrep -n "<name>$game</name>" $filepath | awk '{print $1}' | cut -d : -f 1 | sed -e "${loop}q;d")
path_line_number=$(expr $line_number - 1 )
path=$(sed "${path_line_number}q;d" $filepath | cut -d : -f 2)
get_new_name "$path"
sed -i "${line_number}s/$game/$new_name/g" $filepath
((loop++))
done
index=$(expr index + $i);
done