Author Archives: Klemen

Randomize xterm colors

A bash script to randomize the xterm colors defined in the .Xresources file.

Requirements: An original .Xresources file. It must, among other definitions, specify all the neccesary colors in the following form (order is irrellevant):

xterm*color0    :  #151503
xterm*color1    :  #843706
xterm*color2    :  #69750e
...
xterm*color15    :  #f4dfd8
xterm*background    :  #211d22

Usage:
Navigate to the directory where your system’s .Xresources file is located (most likely your home directory). Make sure it complies with the listed requirements. If so, copy it into a new file to which we from now on refer as the original file. Else edit it so it does and then create the copy:

 cp .Xresources .XresourcesOriginal 

Now we are ready to run the script. Verify that it is located in current directory and classified executable. Then run:

 ./randXcolors .XresourcesOriginal .Xresources 10 

Fell free to replace the number 10 with any integer in range from 0 to 255. That is the limit on the randomization range. As it increases the output will in average differ more and more from the original.
After the new .Xresources is generated, you can load it:

 xrdb .Xresources 

Restart your xterm to see the change take place. Repeating this process a couple of times will likely provide you with a truly amazing xterm color scheme.

Code:
Paste this into the randXcolor file.

#!/bin/bash 

# randXcolors - a bash script to randomize xterm colors

# Operating on the standard Xresources file it changes the xterm*color* and
# xterm*background variables in order to produce a copy with colors randomized.#
# The new color definitions are placed at the end of the new file.

# args: 1. filename - Original Xresources file
#       2. filename - Output Xresurces file
#       3. integer - The radius of randomization with regard to the 255 color
#           range

usage(){
    echo -e "Usage: $0 originalFilename destinationFilename randomRadius\n"
    exit 1
}

function rgb2hex()
#args: RRRGGGBBB color, -001<RRR<256 etc.
#returns #HEXHEX color
{
    local hex1=$(printf '%02x' $((10#${1:0:3}))) #10# transf 2 dec, $1 is arg
    local hex2=$(printf '%02x' $((10#${1:3:3})))
    local hex3=$(printf '%02x' $((10#${1:6:3})))
    echo "#$hex1$hex2$hex3"
}

function hex2rgb()
#args: #HEXHEX color
#returns RRRGGGBBB color
{
    local rgb1=$(printf '%03d' 0x${1:1:2})
    local rgb2=$(printf '%03d' 0x${1:3:2})
    local rgb3=$(printf '%03d' 0x${1:5:2})
    echo "$rgb1$rgb2$rgb3"
}

function randomizeHex()
#args #HEXHEX color, randomization radius
#returns #HEXHEX randomized color
{
    local rgb=$(hex2rgb $1)
    declare -a local rgbArr=(${rgb:0:3} ${rgb:3:3} ${rgb:6:3})
    local rgbRand=""
    for i in "${rgbArr[@]}"
    do
        local substr=$(echo $(($(shuf -i 0-$(($2 + $2)) -n 1) - $2)))
        local sum=$((10#$i + $substr))
        if [ $sum -gt "255" ]; then
            sum="255"
        elif [ $sum -lt "0" ]; then
            sum="0"
        fi
        rgbRand="$rgbRand$(printf '%03d' $sum)"
    done
    echo $(rgb2hex $rgbRand)
}

#input check
[[ $# -ne 3 ]] && echo -e "\nERROR\nWrong number of arguments.\n" && usage
[[ $1 == $2 ]] && echo -e "\nERROR\nDo not use the same file as input and"\
    "output.\nExiting...\n"\ && exit 1
[[ $3 -gt 255 ]] || [[ $3 -lt 0 ]] && echo -e "\nERROR.\nThe randomization"\
    "radius too large or too small to make any sense.\nUse values between 0"\
    "and 255, with less then 100 recommended.\nExiting...\n" && exit 1

#"main" function
cp "$PWD/$1" "$PWD/$2"
sed -i '/[Xx][Tt]erm\*color*/d' "$PWD/$2"
sed -i '/[Xx][Tt]erm\*background*/d' "$PWD/$2"
count=0
while read l; do
    if [ ${l:0:1} != "!" ]; then #dont touch comments
        if [[ $l == *[Xx][Tt]erm\*color* ]]; then
            echo "xterm*color"$count"    :  "$(randomizeHex "#${l#*#}" $3) \
                >> "$PWD/$2"
            count=$(($count + 1))
        elif [[ $l == *[Xx][Tt]erm\*background* ]]; then
            echo "xterm*background    :  "$(randomizeHex "#${l#*#}" $3) \
                >> "$PWD/$2"
        fi
    fi
done < "$PWD/$1"

Debian on BBB

These are instructions on how to put Debian for Beaglebone Black (BBB) on a SD card without using an additional SD card reader.

Hardware required: Beaglebone Black and a GNU/Linux computer, both connected to the same LAN, and a micro SD card.
Computer software required: a web browser, wget, md5sum, 7zip, arp-scan, openssh, dd.
BBB software required: a working GNU/Linux running from the eMMC.

Fire up your computer’s terminal. Chdir into an initially empty directory.

1) Navigate to the official BBB webpage using your preferred browser:

http://beagleboard.org/latest-images/

2) Download the latest Debian (or other) image via wget. Choose beetween the bootable and flashing-only version (according to your personal preference). You can also download the file using browser or bit torrent.

wget http://debian.beagleboard.org/images/bone-debian-7.5-2014-05-14-2gb.img.xz

3) Check its MD5 hash (optional).

md5sum *.xz

4) Unzip the file using 7zip (the extracted file will look like this: IMAGE_NAME.img):

7z x *.xz

5) Make sure your BBB is connected to the same local network your machine is connected to using ethernet cable. Also make sure the SD card has been put into its slot after BBB had been turned on and booted up.

Find your local ethernet name. It should look something like eth0. You will be able to find it using this command:

ls /sys/class/net

6) Arp-scan your home network to find the BBB’s IP adress:

sudo arp-scan --interface=YOUR_ETHERNET_NAME --localnet

7) The IP address corresponding to Texas Instruments, Inc in the third column should be your BBB’s. Now you can ssh into your device. If you’re using the default Ångström distribution, just hit enter when asked for password:

ssh root@YOUR_BBBS_IP

8) List bulk devices to find your SD card’s device name.

lsblk -f && lsblk

9) Take this step very seriously since not doing so could do harm to your BBB in step 11. You should be able to find the SD card device name by its size, filetype and name. The BBB comes with a 2GB on-board flash so make sure to filter that one out. The SD may include some partitions, for example mmcblk1p1 and/or mmcblk1p2. In my case, the SD device name was mmcblk1. We will be using the SD_NAME in the following steps.

Memorise the SD_NAME (or better yet, write it down) and unmount all of its partitions:

umount SD_NAME_PARTITION_MOUNTPOINTS

Make sure they had been unmounted using lsblk again.

10) Exit your ssh session:

exit

11) Please take this final step even more seriously and follow at your own risk. We will be using the dd image writing command, also known as the Data Destructor. You could easily destroy all data either on your machine or BBB so proceed carefully.

Clone the image to the SD card using ssh:

dd if=./IMAGE_NAME.img | ssh root@BBB_IP "dd of=/dev/SD_NAME bs=4M"

if stands for input, of for output. This will take a while. You should get a coffee. Or make tea. Take a quick nap in front of your computer. Once it’s done, you should end up with a SD-card ready for eMMC flash or boot.

Install a package from AUR on arch linux

The easiest way to install packages on arch Linux is from the official repositories using pacman. This is also the preferred way since the packages installed this way are updated through system-wide updates.

However – what if the package you need is not available in the official repos? Or perhaps you need a newer version? In this case you can follow this procedure:

1. Look it up on AUR

AUR stands for arch user repository. It is maintained by the community. Many packages are availible. AUR.

2. Check it

Check the dependencies. You will need to install them later if they are not installed on your system but no need to worry about that just yet. Read the discussion on the bottom of the page – it sometimes contains valuable info. If the package is flagged out of date you can consider yourself unlucky. These ones usually don’t work out of the box. Try – perhaps it does. Many packages are marked with “-git”. These ones use git to install the newest version of the software.

3. Download and untar

Download tarball archive using your browser. It is worthwhile using a special folder for all the programs you build for convenience. Once downloaded locate the file through console and run:

tar -zxvf package_name.tar.gz

cd package_name

4. Read the PKGBUILD file and build

PKGBUILD file contains all the information needed to install a package. You are advised to see it before install but it is not necessary.

Then run this command:

makepkg -src

s (syncdeps) installs the missing depencenies using pacman provided they are available through sync repos. In case they are not you can install them the way you are trying to install this one.

r (rmdeps) removes the dependencies after the build process is completed so your system does not get loaded with programs you do not need.

c (clean) cleans up leftover work files used during the build process.

In case build process was not not successful you can try to edit PKGBUILD file, leave a comment on the package AUR website or perhaps send a message to the mantainer.

5. Install using pacman

After a successful build process your current folder should contain a file with the extension .tar.xz. Install the package like this (as root):

sudo pacman -U *.tar.xz

U (upgrade) upgrades or adds package(s) to the system and installs the required dependencies from sync repos.

6. Mantain

You should treat your system well. Info regarding the arch linux maintenance is availible through the arch wiki.