Actually i make 4 access on the single file for obtain each time one single data,is it possible obtain all the data on a single pass?
While is ok for me maintain 4 distinct variable.
Naturally using only bash or bash+sed or bash+awk or bash+grep+cut.
I find myself doing this sort of thing a fair bit and setting up a local param_val() function makes the code much more maintainable/extendable/readable,
and as long as your only processing a few files the overhead can pretty much be ignored, more often than not the file will still be in the cache anyway.
Remember your going to be comming along in a year or two and going "What the $%#$ is this damn awk script doing!"
#!/bin/bash
function param_val {
sed -n "s/^${1}=//p" $2 2> /dev/null
}
for i in `ls -c1 /usr/share/applications`
do
CF=/usr/share/applications/$i
name=$(param_val Name $CF)
executable=$(param_val Exec $CF)
icon=$(parm_val Icon $CF)
categories=$(param_val Categories $CF | cut -d\; -f1)
done
Interesting.
I fix one error:
icon=$(param_val Icon $CF)
and i put the output and now work.
What mean the regular expression used on sed?
---------- Post updated at 10:14 AM ---------- Previous update was at 08:12 AM ----------
After more then one hour of search on that follow command:
sed -n "s/^${1}=//p" $2 2> /dev/null
i don't understand why use the symbol { and } over the first parameter.
And the last:
2.
While the /p if i understand correctly from Sed - An Introduction and Tutorial means:
I even make an attempt on test of the sed outside the script but i don't known where i made a mistake:
$ sed -e "s/^{Name}=//p" /usr/share/applications/firefox.desktop
Naturally using -n i don't have output so i used e.
{ } work when substituting any shell variable in any context shell variables can be substituted in. It's normal syntax, and the safe way to do substitution, because it's less ambiguous. Would $var1_$var2 substitute $var1 or $var1_ ? The latter, to prevent that you'd do ${var1}_${var2}.
I replace:
categories=$(param_val Categories $CF | cut -d\; -f1)
with:
categories=$(param_val Categories $CF | awk -F ";" '{print $1}')
for remove the dependency with the command line cut.
This is the first release with some minor adjustment.