thefekete.net

$> :(){ :|:& };:

Bash script to rename gEDA PCB gerber files to BatchPCB format

Just wrote a quick bash script to rename PCB gerber exported files to the BatchPCB naming rules.

It expects a list of files as arguments. PCB exports files with a ‘grb’ extension so the following should work just fine:

#!/bin/bash
$> gpcb2batchpcb *.grb

Read the rest of this entry »

Pseudo git diff with gEDA gschem in bash

Not really a diff, but you can switch between two (or more) schematics from different commits and see what changed. Here’s how…

The ingredients

First of all, you can open multiple files with gschem by listing them after the command:

#!/bin/bash
gschem FILE1 FILE2 ...

… and you can retrieve old versions of files with git-show:

#!/bin/bash
git show HEAD^:SOME_FILE
git show ab05831:SOME_FILE

For more info on how to specify a file, see the git-show man page.

Putting it together

Unfortunately, you can’t pipe schematics to gschem’s stdin (that I know of). Even if you could, we want to compare two schematics, so how would you pipe two files to stdin? It all seemed pretty tough to me until I found a post on stack overflow showing exactly how to handle it… Using process substitution, it becomes an easy problem:

#!/bin/bash
# See the last committed and current versions of a file
gschem <(git show HEAD:SOME_FILE) SOME_FILE
# See any two files in history
gschem <(git show OBJECT) <(git show OBJECT)

Bash script / function to append file names

In my previous post about appending file names, I made a one-off solution.

Here’s a bash function that you can throw in your .bashrc and use more easily:

#!/bin/bash

# Put this in your ~/.bashrc and use it like a script...
append_filename() {
    USAGE="append: Puts text between filename and extension"
    USAGE="$USAGE\nUsage: $0 APPENDAGE file1 file2 file3 ..."

    if [ "$#" == "0" ]; then
        echo -e "$USAGE"
        return 1  # exit kills shell this is called from
    fi

    # get appended string
    APPENDAGE=$1
    shift

    while (( "$#" )); do
        if [ -f "$1" ]; then  # don't rename directories
            mv "$1" "${1%.*}$APPENDAGE.${1##*.}"
        fi
        shift  # grab the next file in list
    done
}

Appending filenames, not extensions

I had a group of files that I needed to append a suffix to, but leave the extension intact. Basically, I needed to split the filename and extension, then put them back together with some filling in the middle…

This answer @ stack overflow shows how to split them, and then it’s pretty easy to put them back together:

#!/bin/bash
FILLER='-1'  # Whatever you want to append to the filename
for i in *
do
    mv $i "${i%.*}$FILLER.${i##*.}"
done

Splitting large files for a FAT32 removable drive or flash drive

If you got a file or group of files to put on a removable drive with a FAT32 file system – and any of them are over 4GB – then you need a way to make it smaller for transport. The easiest way is with split, cat and tar.

Read the rest of this entry »

Bash: Count Lines of Code in a Python Project

#!/bin/bash
find . -name '*.py' | xargs cat | sed '/^\s*#/d;/^\s*$/d' | wc -l

xargs converts find’s output to arguments, the sed RE drops comments and blank lines, and wc counts them lines. Thanks to these posts on stack overflow for some reference material.

Quickly check line character maximums from the command line

If you want to make sure you didn’t go over 79 or 80 characters in a file you can simply run:

#!/bin/bash
grep -Pn '.{80,}' file-to-check

This uses grep’s perl regex option (-P) so we can use the expression .{80,} to find any lines that have >= 80 characters. The -n option prints the line numbers in the output so you can find them easily.

SSH Key transfer one-liner

=== UPDATE ===
Thanks to Tomasz Muras from Open Source Tech Blog, I now know about ssh-copy-id, which basically renders this post useless, except for academic reasons… Nevertheless, it’s a good example of piping through ssh to programs on a remote machine, so I’ll leave it up.


If you’ve already got access to a server or workstation via ssh, but are tired of entering your password, or need more security… You can transfer the public key on the client straight into the authorized_keys2 file on the server with one step:

PUB_KEY='id_rsa.pub'
REMOTE_HOST='example.com'
REMOTE_PORT=22

cat ~/.ssh/$PUB_KEY | ssh $REMOTE_HOST -p$REMOTE_PORT \
"cat >> ~/.ssh/authorized_keys2"

PUB_KEY will most likely be id_rsa.pub or id_dsa.pub. REMOTE_HOST and REMOTE_PORT are the hostname and port number of the server you’re sending the key to (you can leave the port option out if its on the standard ssh port 22).

Change volume from command line

To change the system volume from bash (on ubuntu) do the following:

#!/bin/bash

# To raise the volume by 3%:
amixer -c 0 set Master 3+

# To lower the volume by 3%:
amixer -c 0 set Master 3-

Bash: Copy all files in an m3u playlist to folder

I’ve got a pretty good list of songs I like, and I wanted to copy them all to a USB stick for the car… So here’s the bash one-liner I wrote to take an m3u playlist and copy all the files to a new directory:

#!/bin/bash
cat playlist.m3u | grep -v '#' | \
while read i; do cp "${i}" /path/to/destination/ ; done

Read the rest of this entry »