1

I used to use the "execute_command" found in the former awesome wiki. This command uses io.popen and the lines method to return the command's result.
Now, the doc's advice is to avoid io.popen.

My rc.lua uses io.popen to assign hostname's computer to a variable ordinateur (I'm trying to maintain a unique rc.lua for two quite different computers).

I used to have this line : ordinateur=execute_command( "hostname" ) I replace it with :

awful.spawn.easy_async_with_shell( "hostname" ,    
   function(stdout,stderr,reason,exit_code)
       ordinateur = stdout                                                      
   end)

Further in the script, I have tests like if ordinateur == "asus" then .... But it fails. Actually ordinateur is nil
I think the rc.lua is read before ordinateur gets its assignment, right ?

So, what can I do ? I'm thinking replace the command with the reading of the /etc/hostname file, is that better ? How am I going to do this with awful.spawn.* commands ?

Thank you
David

david
  • 1,302
  • 1
  • 10
  • 21

2 Answers2

1

If at all possible, use LuaSocket.

> socket = require "socket"
> print(socket.dns.gethostname())
myhost

Another option is to run hostname from the script that launches the window manager, and store the result in an environment variable. Who knows, if you're lucky, it's already in there?!

> print(os.getenv("HOSTNAME") or os.getenv("HOST"))
myhost
Mike Andrews
  • 3,045
  • 18
  • 28
  • 1
    Or `print(os.getenv("HOSTNAME") or os.getenv("HOST"))` – lhf Mar 15 '18 at 18:35
  • 1
    Actually I get `nil` using `os.getenv` and I don't want to create an environment variable. So I choose your LuaSocket solution. I had put your code to the top of my rc.lua and it's doing the job! Thanks! – david Mar 15 '18 at 20:31
  • lhf, that's great - I've added that into the answer. david, glad the LuaSockets solution worked for you! – Mike Andrews Mar 15 '18 at 21:56
0

It fails later in the script because the command is asynchronous. This means it Awesome keeps going during the command execution and the result will be available later.

This is the whole point of not using io.popen. io.popen will stop everything [related to X11, including all applications] on your computer while it is being executed.

You need to modify your code so all things that access ordinateur do so after the callback has been called. The easiest way to do so is adding that code in the callback.