Tiny Core Extensions > TCE Bugs

wbar.tcz bug and fix

<< < (4/6) > >>

GNUser:
How about this?


--- Code: ------ a/wbar_update.sh
+++ b/wbar_update.sh
@@ -1,9 +1,11 @@
-#!/bin/sh
+#!/bin/busybox ash
 # (c) Robert Shingledecker 2010
 # Called from desktop.sh to update wbar icons.
+. /etc/init.d/tc-functions
+useBusybox
 
 writeWBARitem() {
-busybox awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
+awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
 BEGIN {
   FS = "="
   name = ""
@@ -19,7 +21,7 @@
     test = match(exec,"%")
     if ( test ) exec = substr(exec,0,test-1)
   } else if ( $1 == "X-FullPathIcon" ) {
-    icon = $2
+    icon = rtrim($2)
   } else if ( $1 == "Terminal" ) {
     terminal = $2
   }
@@ -72,6 +74,7 @@
 FREEDESK=/usr/local/share/applications/"$APPNAME".desktop
 if [ -e "$FREEDESK" ]; then
    ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK")"
+   ICONCHECK=$(trim "$ICONCHECK")
    NAMECHECK="$(awk 'BEGIN{FS = "="}$1=="Name"{print $2; exit 0}' "$FREEDESK")"
    if grep -qw "^t: *${NAMECHECK// /}$" "${TCEDIR}"/xwbar.lst 2>/dev/null; then exit 0; fi
    TARGET="$APPNAME".img

--- End code ---

GNUser:
Hmm, I don't like using trim/echo here. It does more than just get rid of trailing whitespace. I favor sticking with sed.


--- Code: ---bruno@x230:~$ trim() { echo $1; }
bruno@x230:~$ var="   abc   def   ghi   "
bruno@x230:~$ var2=$(trim $var)
bruno@x230:~$ echo "$var2"x
abcx

--- End code ---

GNUser:
This is better:

--- Code: ---bruno@x230:~$ trim() { echo $1; }
bruno@x230:~$ var="   abc   def   ghi   "
bruno@x230:~$ var2=$(trim "$var")
bruno@x230:~$ echo "$var2"x
abc def ghix

--- End code ---
I went back and added quotes at the appropriate spot in Reply #15.
Using quotes and echo to get the desired result feels risky. I think trimming ICONCHECK with sed is cleaner.

GNUser:
I like this one the best:


--- Code: ------ a/wbar_update.sh
+++ b/wbar_update.sh
@@ -1,9 +1,11 @@
-#!/bin/sh
+#!/bin/busybox ash
 # (c) Robert Shingledecker 2010
 # Called from desktop.sh to update wbar icons.
+. /etc/init.d/tc-functions
+useBusybox
 
 writeWBARitem() {
-busybox awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
+awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
 BEGIN {
   FS = "="
   name = ""
@@ -19,7 +21,7 @@
     test = match(exec,"%")
     if ( test ) exec = substr(exec,0,test-1)
   } else if ( $1 == "X-FullPathIcon" ) {
-    icon = $2
+    icon = rtrim($2)
   } else if ( $1 == "Terminal" ) {
     terminal = $2
   }
@@ -71,7 +73,7 @@
 #
 FREEDESK=/usr/local/share/applications/"$APPNAME".desktop
 if [ -e "$FREEDESK" ]; then
-   ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK")"
+   ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK" | sed -r 's/[ \t]+$//')"
    NAMECHECK="$(awk 'BEGIN{FS = "="}$1=="Name"{print $2; exit 0}' "$FREEDESK")"
    if grep -qw "^t: *${NAMECHECK// /}$" "${TCEDIR}"/xwbar.lst 2>/dev/null; then exit 0; fi
    TARGET="$APPNAME".img
--- End code ---

mocore:

--- Quote from: GNUser on October 30, 2024, 12:08:13 PM ---Hmm, I don't like using trim/echo here. It does more than just get rid of trailing whitespace. I favor sticking with sed.

--- End quote ---

i happened to be reading
https://git.altlinux.org/people/legion/packages/?p=libshell.git;a=blob;f=docs/README.md

--- Quote ---   1 # libshell
   2
   3 The libshell is a set of the most commonly-used shell functions. All functions use minimum
   4 of external utilities and written for POSIX shell.
   5
   6 The main idea is to get rid of implementing these functions in shell-scripts again and again,
   7 and also to protect from common mistakes.
--- End quote ---

... i like the sentiment , and even more the idea of (scripted) modularity   
both seam relevant in the case of this bug !! ...

so ....
 consider these awk functions ( or *irrelevant tangent*  the lily's  :P if you like)
https://gist.github.com/andrewrcollins/1592991 ~  ltrim(), rtrim(), and trim() in awk

--- Code: ---function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }

--- End code ---

eg


--- Code: ---f() { awk '                               
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }
'"$@" ; }

var="   abc   def   ghi   "
echo "$var" | f '{print ">" trim($0) "<"}'
#>abc   def   ghi<

--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version