Search code examples
luasyntax-errorawesome-wm

My if-then-else-end statement is failing in Lua; how can I fix it?


I'm trying to improve my rc.lua for the Awesome window manager. The default rc.lua sets up a menu with the following code:

myawesomemenu = {
   { "manual", terminal .. " -e man awesome" },
   { "edit config", editor_cmd .. " " .. awesome.conffile },
   { "restart", awesome.restart },
   { "quit", awesome.quit }
}

I use Awesome as the window manager for the GNOME desktop environment, so I want to have Awesome use the gnome-session-quit program instead of awesome.quit, but only if the environment variable "DE" is set to "gnome". Therefore, I replaced the fifth line with

{ "quit", if os.getenv("DE") == "gnome" then os.execute("/usr/bin/gnome-session-quit") else awesome.quit end }

But when I reload the file, I get "unexpected symbol near if". How can I fix this, and what causes it?


Solution

  • Try this:

    { "quit", (os.getenv("DE") == "gnome") and function() os.execute("/usr/bin/gnome-session-quit") end or awesome.quit}
    

    a and b or c is like the C expression a ? b : c, provided that b is not false.