Ubuntu – What does this if-then statement mean

command linesh

Playing around in my system, I stumbled upon

~$ echo $XDG_DATA_DIRS
/usr/share/ubuntustudio:/usr/share/xfce4:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/usr/share

Asking myself why /usr/share is twice in the path I found out that the following snippet in
/etc/alternatinves/x-session-manager which is a link to /usr/bin/startxfce4 is responsible:

#!/bin/sh
.
.
.
if test "x$XDG_DATA_DIRS" = "x"
then
  if test "x/usr/share" = "x/usr/local/share" -o "x/usr/share" = "x/usr/share"; then
    XDG_DATA_DIRS="/usr/local/share:/usr/share"
  else
    XDG_DATA_DIRS="/usr/share:/usr/local/share:/usr/share"
  fi
else
  XDG_DATA_DIRS="$XDG_DATA_DIRS:/usr/share"
fi
export XDG_DATA_DIRS
.
.
.

When I look at the line

if test "x/usr/share" = "x/usr/local/share" -o "x/usr/share" = "x/usr/share"; then

I have difficulties to understand this if-statement, for me it looks like a comparison of strings
where the first one is always false and the second one is always true.

Combined with a logical or the test always evaluates to true, so I could shorten the line to
if true; then or I could say I don't need an if-statement at all.

Where is my mistake? Or is it written like this to confuse beginners like me?

Best Answer

You are right, the command

test "x/usr/share" = "x/usr/local/share" -o "x/usr/share" = "x/usr/share"

returns true (0) always.

It looks like if the file in question was generated from a more generic version but the way of generation was not optimal. The script should either ask for actual paths each time or keep just the relevant branch in the generated file.

However, this particular file comes from a package – not being generated on your machine. This is likely something to be fixed/improved… You can file a bug on Xfce Bugzilla (if it’s not present there already) or fix it yourself. You can clone the Git repository for Xfce4 session, you can also get in touch with Xfce4 developers using their mailing list. Good luck with improving the code!

Related Question