Score:1

Convert a ruby ​script to a bash script

gb flag

I need to turn this ruby ​​script into bash for i3 on Ubuntu:

#!/usr/bin/env ruby

sink =  %x`pacmd list-sinks | grep -e 'name:' -e 'index:' -e 'active'`

er = /\* index: ([0-9])/

er.match sink

if $1 == "1"
    print 'usb'
else 
    print 'mic'
end

The script must be used for i3block and displayed on i3bar. As an example the following script shows the result on i3block, but I don't understand how it does it.

#!/bin/bash

BAT=$(acpi -b | grep -E -o '[0-9][0-9]?%')

echo "BAT: $BAT"

exit 0

Thanks for your help!

Score:3
in flag
#!/bin/bash
sink=$(pacmd list-sinks | grep '* index:')
if [[ "${sink:11:11}" -eq "1" ]]
then
    echo 'usb'
else
    echo 'mic'
fi

This is making a lot of assumptions, as you did not explain much about the ruby script you pasted and what the actual objective is.

This bash script does a grep on the output of the pulseaudo list of sinks, where the index is preceded with a *. then from this line, the 11th character is checked. If it is a 1 then echo out USB, otherwise MIC.

As said, this is presuming a lot and this style of programming can go hilariously wrong if there are other indexes in play, or you do want the name of the device...

I don't know i3bar or i3blocks, but it maybe that you need to set an interval in i3blocks or wrap the script in a while loop, because a script runs, outputs something and then exit's, so something should continuously trigger it. The way you do it is i3 specific, which I know nothing about. Based on the man pages, this is what I would try first

[pulse]
command=/usr/local/bin/check_pulse.sh
interval=15

http://manpages.ubuntu.com/manpages/bionic/man1/i3blocks.1.html

gb flag
Thanks, the script works perfectly, but it doesn't solve my problem. I need an i3block script that shows 'usb' or 'mic' after the result on i3bar. Your script works fine on the command line, but displays nothing on i3bar. Why?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.