像这样的东西可能会起作用:
echo -e "<menu>\n<submenu>" > output.xml
for f in *.desktop; do
Icon=$(awk -F= '/Icon/{print $2}' "$f")
Name=$(awk -F= '/Name/{print $2}' "$f")
URL=$(awk -F= '/URL/{print $2}' "$f")
LastOpenedWith=$(awk -F= '/X-KDE-LastOpenedWith/{print $2}' "$f")
echo "<action label=\"$Name\" icon=\"$Icon\" exec=\"$LastOpenedWith $URL\"/>"
done >> output.xml
echo -e "</submenu>\n</menu>" >> output.xml
使用Config::IniFiles的 Perl 解决方案可能如下所示:
#!/usr/bin/env perl
use strict;
use warnings;
use Config::IniFiles;
open XML, ">/path/to/output.xml" or die $!;
print XML "<menu>\n<submenu>\n";
foreach my $file (</path/to/*.desktop>) {
my $ini = Config::IniFiles->new( -file => "$file" );
printf XML "<action label='%s' icon='%s' exec='%s %s'/>\n",
$ini->val('Desktop Entry', 'Name'),
$ini->val('Desktop Entry', 'Icon'),
$ini->val('Desktop Entry', 'X-KDE-LastOpenedWith'),
$ini->val('Desktop Entry', 'URL[$e]')
}
print XML "</submenu>\n</menu>\n";
close XML;