Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert a ruby ​script to a bash script

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

#!/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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading