Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Bash] output of command in infinite loop

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
peer
Posts: 451
Joined: 2017-03-26 10:14
Has thanked: 9 times
Been thanked: 22 times

[Bash] output of command in infinite loop

#1 Post by peer »

I have a 'dvb-t dab fm' usb stick thas has a IR receiver in it. With this receiver I can read the codes that are send from a remote control. In bash I use the command: '/usr/bin/ir-keytable -s rc0 -t'. This command waits for a button press and then shows the code in the terminal. It is stopped with CTRL-C.
Her is an example of the output:

Code: Select all

/usr/bin/ir-keytable -s rc0 -t
Testing events. Please, press CTRL-C to abort.
653.122133: lirc protocol(rc5): scancode = 0xc toggle=1
653.122142: event type EV_MSC(0x04): scancode = 0x0c
653.122142: event type EV_SYN(0x00).
653.334160: lirc protocol(rc5): scancode = 0xc toggle=1
653.334169: event type EV_MSC(0x04): scancode = 0x0c
653.334169: event type EV_SYN(0x00).
^C
I would like to make a script that after each keypress show the protocol and the scancode. So I need to redirect the output to a variable. How can I do this?

CwF
Global Moderator
Global Moderator
Posts: 2719
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 201 times

Re: [Bash] output of command in infinite loop

#2 Post by CwF »

peer wrote: 2023-08-25 15:13 So I need to redirect the output to a variable. How can I do this?
I think I would output to a file, likely append, then separately read the last line.

I've had this thought for dissecting am HD-Radio module that is partial figured out. Also, is the IR part of the dongle or a cabled in extra? I have a few such gadgets that were not recognized years ago and haven't played with them since. The hauppauge's I currently use have an IR section I should investigate someday...

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1418
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 80 times
Been thanked: 191 times

Re: [Bash] output of command in infinite loop

#3 Post by steve_v »

Generally, one might use a while loop, a pipe, and the 'read' builtin, like:

Code: Select all

<some command>  |
while IFS= read -r line; do
    <do something with $line>
done
Where each line of stdout from <some command> is placed in the variable $line.
Setting IFS (to null) should prevent word-splitting on spaces in the command output, similarly the '-r' option to read prevents interpretation of escape characters.

Many manuals and tutorials exist covering this.
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

peer
Posts: 451
Joined: 2017-03-26 10:14
Has thanked: 9 times
Been thanked: 22 times

Re: [Bash] output of command in infinite loop

#4 Post by peer »

CwF wrote: 2023-08-25 17:55
peer wrote: 2023-08-25 15:13 So I need to redirect the output to a variable. How can I do this?
I think I would output to a file, likely append, then separately read the last line.

I've had this thought for dissecting am HD-Radio module that is partial figured out. Also, is the IR part of the dongle or a cabled in extra? I have a few such gadgets that were not recognized years ago and haven't played with them since. The hauppauge's I currently use have an IR section I should investigate someday...
The IR is part of the dongle

I tried to redirect the output to a file. I tried both:

Code: Select all

/usr/bin/ir-keytable -s RC0 -t > test.txt      and
/usr/bin/ir-keytable -s RC0 -t >> test.txt

The file test.txt is created but the size = o.

peer
Posts: 451
Joined: 2017-03-26 10:14
Has thanked: 9 times
Been thanked: 22 times

Re: [Bash] output of command in infinite loop

#5 Post by peer »

steve_v wrote: 2023-08-26 01:54 Generally, one might use a while loop, a pipe, and the 'read' builtin, like:

Code: Select all

<some command>  |
while IFS= read -r line; do
    <do something with $line>
done
Where each line of stdout from <some command> is placed in the variable $line.
Setting IFS (to null) should prevent word-splitting on spaces in the command output, similarly the '-r' option to read prevents interpretation of escape characters.

Many manuals and tutorials exist covering this.
I tried:

Code: Select all

/usr/bin/ir-keytable -s RC0 -t |
while IFS= read -r line; do
    echo $lin>
done
There is no output at all

peer
Posts: 451
Joined: 2017-03-26 10:14
Has thanked: 9 times
Been thanked: 22 times

Re: [Bash] output of command in infinite loop

#6 Post by peer »

I think redirecting does not work with the command '/usr/bin/ir-keytable -s RC0 -t'. The command listens to the remote control and gives output when a button is pressed on the remote. This is done during the execution of the command, not after.
I tried the scripting method:

Code: Select all

/usr/bin/script -c "/usr/bin/ir-keytable -s RC0 -t"test.txt
Now the test.txt file is created and is showing the output but only after the command is finished. And I would like access to the output during the execution of the command

lindi
Debian Developer
Debian Developer
Posts: 452
Joined: 2022-07-12 14:10
Has thanked: 1 time
Been thanked: 88 times

Re: [Bash] output of command in infinite loop

#7 Post by lindi »

You could try the -f option of script.

peer
Posts: 451
Joined: 2017-03-26 10:14
Has thanked: 9 times
Been thanked: 22 times

Re: [Bash] output of command in infinite loop

#8 Post by peer »

Yes, that works!
Now I have the following command:

Code: Select all

/usr/bin/script -c "/usr/bin/ir-keytable -s RC0 -t" test.txt -f
When I press a button on the remote there are a few lines added to the file test.txt.

Post Reply