You are not logged in.

#1 2016-01-03 08:26:25

esa
Member
Registered: 2011-12-29
Posts: 143
Website

[SOLVED] [lua] AwesomeWM - Assign values from a shell script

Hello everyone

So, i've made up some bash scripts to get image names according to hardware status, such as volume level or battery power.
Basicly, i wanted to use the script to get the filename, put the proper DIR variable in front, and let the WM load the proper icon.

The bash scripts work like:

/p/a/t/h/snd.sh image
normal3.png

Then in my volume widget i'm having preps like:

--
--	Functions
--
	function file_exists(name)
		local f=io.open(name,"r")
		if f~=nil then io.close(f) return true else return false end
	end
	function get_bash_string (cmd,args)
		local t = os.execute( cmd .. " " .. args)
		print(t)
	end
--
-- Volume widget
--
	SCRIPT	= DIR_SCRIPTS .. "/snd.sh"
	ICONS	= DIR_ICONS .. "/snd/"
	
	if file_exists( SCRIPT )
	then 	volume_icon 	= wibox.widget.imagebox()
		
		-- I get this displayed, so it finds the script, but fails executing it.. (script has +x)
		naughty.notify({ preset = naughty.config.presets.info, title = "Script actually found: " .. SCRIPT, text = err })
		
		-- 'direct' Returns boolean / should string
		vol_icon	= os.execute( SCRIPT .. " " .. "image" )
		
		-- 'function' Returns empty (nil value)/ should string
		vol_icon 	= get_bash_string( SCRIPT , "image")		
		
		-- So setting these values as image, obviously fails
		volume_icon:set_image(ICONS .. vol_icon)				
	end

Someone able to help to getting 'vol_icon' set properly, so it actually contains the string returned from the 'script image'?
Thank you in advance

EDIT:

http://lua-users.org/wiki/OsLibraryTutorial wrote:

Execute an operating system shell command. This is like the C system() function. The system dependent status code is returned.

So, how would i get the string?
Do i have to write (read from) a tempfile?

Last edited by esa (2016-01-03 10:29:14)


Author of: TUI (Text User Interface for scripts), VHS (Video Handler Script, using ffmpeg) and YASSI (Yet Another Simple Script Installer)

Offline

#2 2016-01-03 10:19:50

esa
Member
Registered: 2011-12-29
Posts: 143
Website

Re: [SOLVED] [lua] AwesomeWM - Assign values from a shell script

So, since os.execute() only returns wether its been successfull or not, i tried something new...

	function get_bash_string (cmd,args)
		ret = os.execute( cmd .. " " .. args .. " > " .. "tmp" )
		-- if t == 1
		-- if (t)
		-- if t == true
		if t == 0
		then	-- It succeeded, so read from tempfile which contains the string result
			file = io.open("tmp", "r")
			output = print(file:read())
			file:close()
			os.remove("tmp")
		else	-- It failed, so print the required 'none' icon
			output = "none.png"
		end
		
		naughty.notify({ preset = naughty.config.presets.info, title = output, text = err })
		print(output)
		return output
	end

	SCRIPT	= DIR_SCRIPTS .. "/snd.sh"
	ICONS	= DIR_ICONS .. "/snd/"
	
	if file_exists( SCRIPT )
	then	-- Icon
		volume_icon 	= wibox.widget.imagebox()
		vol_icon 	= get_bash_string( SCRIPT , "image")
		volume_icon:set_image(ICONS .. vol_icon)
	end

This seems to work, with the exception that i always get 'none.png', regardless of how i compare the boolean 't'.
What am i missing?

Thank you in advance

EDIT:
Should have removed the print(file::read())..

/solved

Last edited by esa (2016-01-03 10:29:02)


Author of: TUI (Text User Interface for scripts), VHS (Video Handler Script, using ffmpeg) and YASSI (Yet Another Simple Script Installer)

Offline

Board footer

Powered by FluxBB