here is a template that I use to parse desktop files
Code: Select all
#!/bin/ash
#Copywrong 2011 Brad Conroy - released to the public domain
#Parse through the .desktop files, generate use{ful,less} output
#Though they have Var=value structure, we cannot source them directly (damn) because:
#1. many Vars have illegal characters ... but not the ones we actually want (for now - see TODO)
#2. many values have spaces
#Solution
#1. grep for only the fields that we want to eliminate problem #1
#2.(a) use sed to put quotes after the = and at the end of the line to accomodate spaces
#2.(b) ayttm (possibly others) has a non-standard set of quotes so add a sed for that (could tr -s '"',but sed is already called)
#But wait we didn't _actually_ modify the file, so we can't source it - what a waste of time
#That's ok, we can just use eval on a variable (or in this case a return) to do the same thing
#
#Ok now we have the values, but what to do with them? We will just output them to an easily awkable file
#but writing to a file is slow, so store it in a Variable in-loop and write once out-of-loop
#... at this point you could instead add a single case statement to generate menu entries or anything useful
#TODO add parameters to the sed/grep parts to localize if available (trivial, but would complicate the demo)
for x in /usr/share/applications/* ; do
eval `grep -E ^Name=\|^Categories=\|^Comment=\|^Icon= $x |sed "s/=/=\"/g ; s/$/\"/g ; s/\"\"/\"/g"`
OUTPUT=${OUTPUT}${Name:-UNDEFINED}\|${Categories:-UNDEFINED}\|${Comment:-UNDEFINED}\|${Icon:-UNDEFINED}"\n"
done
echo -e $OUTPUT