Search code examples
shellluaawesome-wm

reading a bash command result


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


Solution

  • 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