Tuesday, October 25, 2016

Using extended regular expressions in sed

I've never quite figured out Posix regular expressions, and was trying to replace the first n characters for every line in a file the other day using sed. This is easy if all the lines have at least n characters in them.
sed s/^....//

But sed doesn't suppor the "optional" question mark modifier so lines with less than n characters would not be matched.

It turns out you can turn on extended regex support with "-r" or "--regexp-extended" allowing the following.
sed s/^.?.?.?.?//

Tuesday, September 13, 2016

Enumerate USB devices with udevadm

There's a lot of ways to find USB information, but they seem to give too much or too little information. This script from http://unix.stackexchange.com/questions/144029/command-to-determine-ports-of-a-device-like-dev-ttyusb0 is perfect for me:

for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
    (
        syspath="${sysdevpath%/dev}"
        devname="$(udevadm info -q name -p $syspath)"
        [[ "$devname" == "bus/"* ]] && continue
        eval "$(udevadm info -q property --export -p $syspath)"
        [[ -z "$ID_SERIAL" ]] && continue
        echo "/dev/$devname - $ID_SERIAL"
    )
done

Running it on my system gives:
~ $ usbdiscover.sh 
/dev/input/event9 - Chicony_USB2.0_HD_UVC_WebCam
/dev/video0 - Chicony_USB2.0_HD_UVC_WebCam
/dev/ttyUSB0 - Parallax_Inc_DEF_CON_22_Badge_DAXU3JTE
/dev/input/event1 - Logitech_USB_Receiver
/dev/input/mouse0 - Logitech_USB_Receiver

Monday, August 29, 2016

Make plasma panel visible again after disconnecting external display

I've been having this weird issue which is apparently known, but the workaround I got from https://bugs.kde.org/show_bug.cgi?id=356225 is below:

In $HOME/.config/plasma-org.kde.plasma.desktop-appletsrc there's a section "[Containments][1]". There within the option "lastScreen" often refers to the wrong screen, i.e. "1" instead of "0". When I change that option to "lastScreen=0" before starting the KDE session the panel is visible again on my laptop screen if it is the one and only available screen.

This is an important workaround because this panel not showing up on my laptop prevented my wifi from connecting and caused other odd behaviors.

Thursday, August 18, 2016

Ralink RT3290 Bluetooth in Arch Linux

I've been having all sorts of weird issues getting bluetooth to work in Arch. My card is the Ralink RT3290 Wireless 802.11n 1T/1R PCIe which is both a WiFi and Bluetooth controller.

Found this on the Arch AUR site (https://aur.archlinux.org/packages/rtbth-dkms/?comments=all) and I knew I was on the right track because the author mentioned his machine would freeze when rmmod the bluetooth driver which is a symptom I also had.

By the way, you also need to have installed the rtbth-dkms package from the AUR (and linux-headers). And finally, afterwards I couldn't get high quality sound (A2DP) to work until I unpaired and re-paired my head phones....WHEW.
And oh yeah, everytime I shutdown I get a "watchdog didn't stop" and after a minute or so of waiting I have to hard power off. Still looking into that, but didn't happen before all this.

1. blacklist the module to Prevent it from load on boot:
#echo "blacklist rtbth" > /etc/modprobe.d/ralink-bt.conf

2. Local script, / usr / local / sbin / rtbth, to load and start rtbt tool:

#!/bin/bash
sleep 5
/sbin/modprobe --ignore-install rtbth; /usr/bin/mknod /dev/rtbth c 192 0; /usr/bin/rtbt &

3. systemctl service, the /etc/systemd/system/rtbth.service file:

[Unit]
Description = Fix rtbth bluetooth after gmd start
After = display-manager.service

[Service]
Type = oneshot
ExecStart =/usr/local/sbin/rtbth
TimeoutSec = 0
RemainAfterExit = true

[Install]
WantedBy = multi-user.target

4. enable it to start on boot:
#systemctl enable rtbth

Calculating date offsets with datemath

A quick utility for performing date calculations on the CLI.

Example:
datemath "8/22/16 - 1/11/16"
224

Monday, August 8, 2016

Use bleachbit to clear out unwanted file dregs

Free space on your file system from command line, more comprehensive than simply emptying your trash, good for privacy too.

sudo bleachbit --clean system.trash

From: https://bbs.archlinux.org/viewtopic.php?id=131374

Monday, June 27, 2016

Getting support for BLU phones

It's not obvious how to get help with BLU phone issues, but I received an email from BLU in response to a problem (I had already solved by the time I got the email) that has a link to a page for getting help from a technician. The link is http://bluproducts.force.com/, the email content is reproduced below.

Hello,

Thank you for contacting BLU Products

A technician would be more than happy to help you.

In order to expedite your request, please click below and provide us with all the information required:

**If there is any missing information or incorrect information we will not be able to further assist you**


**Please DO NOT respond to this email**

Should you have any additional questions please contact us at 1-877-602-8762 (Monday-Friday 9:30am-5:30pm EST).

Thank you for choosing BLU!

Best Regards,

Tuesday, November 3, 2015

Mount partition from multi-partition drive image

I've had to do this a few times. Linux is awesome for dealing with devices and images. From http://askubuntu.com/ is the simple procedure.

Get the partition layout of the image
$ sudo fdisk -lu sda.img
...
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...
  Device Boot      Start         End      Blocks   Id  System
sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)
 
 
Calculate the offset from the start of the image to the partition start
Sector size * Start = (in the case) 56 * 512 = 28672
Mount it on /dev/loop0 using the offset
$ sudo losetup -o 28672 /dev/loop0 sda.img
 
Now the partition resides on /dev/loop0. You can fsck it, mount it etc
$ sudo fsck -fv /dev/loop0
$ sudo mount /dev/loop0 /mnt
 
Unmount
$ sudo umount /mnt
$ sudo losetup -d /dev/loop0

Tuesday, September 29, 2015

JavaScript Contexts and Scopes

It took me a LONG LONG LONG time to figure out why my code re-factorization wouldn't work in JavaScript. I finally found the page ryanmorr.com/understanding-scope-and-context-in-javascript which led me in the right direction and this developer.mozilla.org page which showed me a more concrete example.

Basically, I wasn't using the "bind" function for my object references.


// Define the class.
var Construct = Construct  || (function() 
{
    // Constructor.
    var Construct = function(name) {};
   
    // Additional functions.
    Construct.prototype = 
    {
        sayHello: function() { alert("Hello"); }
        schedule_WRONG: function() { setTimeout(this.sayHello, 1500); },
        schedule_RIGHT: function() { setTimeout(this.sayHello.bind(this), 1500); },
    };
   
    // Return the type.
    return Construct;
})();

$(function()
{
    // Construct the object.
    var f = new Construct();
    f.schedule();

});

Friday, July 10, 2015

Disable debug logging in KDE 5

KDE 5 or Plasma or KF5 or whatever the hell it goes by logs WAY too much crap in the journal.

According to https://bbs.archlinux.org/viewtopic.php?id=193123 its an easy fix to disable debug logging although the kdebugdialog5 program doesn't actually do anything.

Add the lines:
QT_LOGGING_RULES="*.debug=false"
export QT_LOGGING_RULES
to /usr/bin/startkde near the top.

Friday, April 17, 2015

Directing stdout and stderr

Some interesting syntax I wasn't aware of from this stack overflow thread.


# Send stdout to sample.s, stderr to sample.err
myprogram > sample.s 2> sample.err

# Send both stdout and stderr to sample.s
myprogram &> sample.s # New bash syntax
myprogram > sample.s 2>&1 # Older sh syntax

# Log output, hide errors.
myprogram > sample.s 2> /dev/null

Thursday, April 9, 2015

Formatting code on blogger.com

This post is a work in progress, trying to get the syntax formatting code from this www.craftyfella.com, and relating to the script by Alex Gorbatchev entry to work.

For instance, the HTML code to highlight python *should be* something like this.

<h1>Highlighted Python</h1>
<pre class="brush: python; highlight: [5, 15]; html-script: true">
    Python code goes in here
</pre>
# Testing with Python here
if x > 5:
    """ Print stuff """
    print("x is more than 5")
print("done")

These are the includes you can use.

<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js" type="text/javascript"></script> 

Sunday, March 15, 2015

Playing DVDs in Arch


I've never had this much trouble before, most of my DVDs simply wouldn't play at all. I installed tons of libraries, cleaned scratches, nothing... And no error messages from any of several programs gave me a clue as to the problem.



Finally I found a post mentioning having no region set on a DVD drive. I found and downloaded "regionset" and sure enough that did the freaking trick.




~ $ regionset /dev/sr0
Current drive parameters for /dev/sr0:
RPC Type: Phase II (Hardware)
RPC Status: no region code set (bitmask=0xFF)
Vendor may reset the RPC 4 times
User is allowed change the region setting 5 times
Would you like to change the region setting for this drive? [y/n]: y
Enter the new region number for your drive [1..8]: 1
New RPC bitmask is 0xFE, ok? [y/n]: y
Region code set successfully.


After which everything played just peachy.