I just wanted to mention that almost all of the external binary advice goes out the window if you use a busybox shell (ash/hush) with the busybox_prefer_applets option enabled. No major x86 distro, with the exception of maybe alpine linux, enables it by default though.

here is a template that I use to parse desktop files

Code:

#!/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

#this time with fgrep, it needs newline separators and doesn't support "^" (beginning of line)
#This pulls in GenericName and other problem children ... just for demo, then back to grep -E or egrep
#... thus the 4th sed (a blank entry becomes ="$ so fix it)
#This time we will thread the parsing of each file - it is ~10x faster but borks sorting by *.desktop name
#perhaps we want to sort by app name or category ... or, if you are using bash, store them in an mxn array

String="Name=
Categories=
Comment=
Icon=
Exec="

parse(){ #by pulling this out of the main loop we can fork it with the & and run many simultaneously
eval $(fgrep "$String" ${1} |sed 's/=/=\"/g ; s/$/\"/g ; s/\"\"/\"/g ; s/=\"$/=\"\"/')
echo ${Name:-UNDEFINED}\|${Categories:-UNDEFINED}\|${Comment:-UNDEFINED}\|${Icon:-UNDEFINED}\|${Exec:-UNDEFINED}
}

#hmm the recursing of the files is done, but what about all of the threads?
#lets try sorting the file and see?
#sort file > ${1:-sortedfile}
#nope missing entries, need to wait ... but how long
#don't just make a sufficiently long sleep for _your_ box
#we will just wait for sed to complete

"grep -F by default". Yeah that's okay. Of course it's an extra process since it's a wrapper. On my older Slack, fgrep is a *link* to grep. But ony my own kiss-linux I install the real binary of fgrep. It's a smaller bin so has less latency.

Techno's bit of code makes a good example. I compared it to a pure-shell solution which gives the same output:

Code:

#/bin/bin/bsh
# this runs fine on 'real' ash and should work with bb ash
#/usr/bin/bsh
# It will also run using 'bsh' the 'real' Bourne shell
# Quite obvioulsy would run with any bash, as well.

#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)
function techno() {
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
}
#time techno
# On my 700mHz P-III the above runs in
#real 0m0.882s
#user 0m0.380s
#sys 0m0.430s
# on a dir with 56 items
# Hmm, that echo -e may not be enirely portable, but you could use printf or /bin/echo instead

That is great option but seems we must mount /proc before using it.
After making this in sysinit:
/bin/busybox mount -t proc proc /proc
We can call any builtin application directly.
Is that right?_________________SUUM CUIQUE.

That is great option but seems we must mount /proc before using it.
After making this in sysinit:
/bin/busybox mount -t proc proc /proc
We can call any builtin application directly.
Is that right?

I don't know about _any_, but most (some _may_ need other stuff like sys/dev or specific files/nodes) however, if you have busybox ash or hush as /bin/sh and a shell-compliant-script relies on standard utils, it will fail ... you'd need to mod it from util <args> to /path/to/util <args> for those instances ... which is why no major distro is doing it_________________Web Programming - Pet Packaging 100 & 101

Thanks to amigo for the read line trick.
I used it to throw together a jwm_menu_create script that completes in 1/15th the time of fixmenus using only shell commands (and one echo -e, because some of Puppy's busyboxes don't have printf)

Todo:
1. What the heck is up the default*.desktop file stubs - do I need a work around or are they misplaced/malformed (they are missing most entries)
2. Localization - just needs another case statement, but few .desktop files support it anyways ... and my Locale is already default

Thanks, this is very educational. I don't use the jwm so the script isn't much help for me, but the idea behind it is very helpful for future projects._________________Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread.

Once again I started a thread I'm too thick to follow This thread went over my head about 2 pages ago.. I'm gonna read, re-read, and re-read again until it makes sense! But I'm still loving the results! _________________Akita Linux, VLC-GTK, Pup Search, Pup File Search

when you have correctly formatted files there is another way to pull out values

*so if you start out creating a correctly formatted file
getting info is easy

here is an example using a .desktop
we all know that desktops are not formatted correctly or is there an official
line by line template that is used by all linux distros

but we could make a template

then read it with this
using shell and arrays
--------------------------------------------------------------------------------------------

Code:

# advanced arrary use from info in advanced bash scripting modified by Joe Arose
# for using correctly formatted templates

# this separates the strings using a pipe symbol this allows for spaces in strings to be read
# to later be converted back to spaces
# if not done this way every space is a new arrary value causing undesired word splitting
# this fixes many problems with getting just one line of information

#this is beautiful it replaces the pipe for a space
#echo ${desktop_array[4]//|/ }

#how that reads is echo array 4 and //substitue all , pipe symbols, with a space
# it looks a bit like sed but using only the shell

# set this to what you want
fileplace=/usr/share/applications/
filename=Axel-download-accelerator.desktop

# read line by line of the formatted array
# one problem was the Name= and the comment didnt get the spaces converted into
# the pipe symbol because the pipe was after the string with spaces
# but this gets fixed with underscores in the last echo using tr ' ' '_'

# how to read line by line from a correctly forrmatted array now
# just a sample of 5 diferent desktops read from the array desktop_array2

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum