LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2024, 10:02 AM   #1
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Rep: Reputation: 23
Capture the output of xinput test-xi2 into a variable?


I am trying to capture input keys using this program.

I can write the output of the program to a file this way:
Code:
xinput test-xi2 > /dev/shm/INPUT
However the output is completely blank when put into a variable:
Code:
XINPUT_OUTPUT=$(xinput test-xi2)
I have tried a large amount of variations of the above command. This is a little bit above the intensity level of programming I can understand. I would like to get back to doing more simple and understandable stuff than this. How can I fix this?

Last edited by Jason_25; 01-17-2024 at 02:23 PM.
 
Old 01-17-2024, 10:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Hm. Would be nice to see what else did you try.
by the way probably that should be
Code:
xinput --test-xi2 [device]
see man page (of xinput)
 
Old 01-17-2024, 11:35 AM   #3
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
With or without the switch mark -- the command does the same. I have been using it without -- for a long time.

I think to write what I have tried here would just be more confusing because I tried some weird things. The way I wrote it here should have worked and does work for other programs which is why I mentioned this program specifically because it is giving me so much trouble.

I believe what I wrote redirects all the program output including errors into one file or one variable and indeed it does work as expected when put into a file but not when put into a variable. Also another & must be added to the end of the line to try to echo the variable or else the script will hang. But the echo as stated before shows no output.

This is important as the script is time sensitive and I have reason to believe that significant latency is being added by having to read and write to a file versus working with the data in memory which is why I would like to use a variable instead.
 
Old 01-17-2024, 02:24 PM   #4
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
I have simplified the example commands above.

Essentially the problem is that redirection works but command substitution does not.
 
Old 01-17-2024, 03:53 PM   #5
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by Jason_25 View Post
Also another & must be added to the end of the line to try to echo the variable or else the script will hang. But the echo as stated before shows no output.
This appears to be the problem.
See this link here: https://forums.debian.net/viewtopic.php?t=52361

All I wanted to do was improve this little script I made - I figured it would take 30 minutes - and I am 12 hours in and talking about asyncronous command substitutions.
 
Old 01-17-2024, 04:07 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Code:
#!/bin/bash
xdata=$(xinput --test-xi2)
echo "$xdata" > xtest.txt
Depends on your actual code.
Attached Files
File Type: txt xtest.txt (108.8 KB, 6 views)
 
Old 01-17-2024, 04:53 PM   #7
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
The link I posted had a hint to the bash coproc directive and that is what I used. Not before reading some condescending stuff about how coproc is useless compared to the complicated file descriptor stuff. It is remarkable that people that do this stuff from day to day would not have an open mind about when it should be used.

This fixes it.
Setup the coproc by running the program in the background:
Code:
coproc xinput test-xi2
Read the coproc any time you need to and will read all lines with '' but may not terminate until the 1 second delay specified:
Code:
read -d '' -t 1 XINPUT_OUTPUT <&"${COPROC[0]}"
Actually display the results if you want:
Code:
echo "Finally the output: $XINPUT_OUTPUT"
 
Old 01-17-2024, 04:58 PM   #8
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by michaelk View Post
Code:
#!/bin/bash
xdata=$(xinput --test-xi2)
echo "$xdata" > xtest.txt
Depends on your actual code.
That should work if not backgrounded. I needed to background xinput test-xi2 so the script would continue. Thus the coproc solution.

I have never needed to background a command substitution and pull information from it so the idea that it would not work and need to be replaced with something else was new to me and required me to spend a huge amount of time figuring out what was wrong.

I do think it is important for people to see the "easy way" that you have posted so new users do not get the impression that scripting is always this hard.
 
Old 01-17-2024, 05:18 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Sorry, the background part was not obvious at least for me...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting output of one function into array and feed value into variable in other function in ruby Sivagurunathan Programming 3 11-23-2021 09:38 AM
[SOLVED] Just curious: xinput output BW-userx Linux - Hardware 6 02-17-2019 10:03 AM
LXer: Gnome 3.8 Features: Port Gnome-Shell to use XI2 LXer Syndicated Linux News 0 11-04-2012 04:20 PM
LXer: Wine 1.3.18 Adds Support For XI2 Raw Mouse Events LXer Syndicated Linux News 0 04-16-2011 06:12 PM
capture the output to a variable Melsync Linux - General 4 09-07-2006 02:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration