============================================================================
============================================================================

----------------------------------------------------------------------------
Scripts
----------------------------------------------------------------------------

Few Bash scripts of mine. They're clumsy and potentially dangerous. Use (or modify) however you please.

doze :: telsu :: my2cents :: underline-files :: change-suffix :: nimip :: y :: cliching

doze
This script creates a playlist of all your audio files and plays them in random order. The script can also doze (sleep) for certain amount of time before launch so that it can be used as kind of an alarm clock or an egg timer.
#!/bin/bash

AUDIOPATH="$HOME"/audio/
PLAYLISTDIR="$HOME"/.playlists
PLAYLIST="$PLAYLISTDIR"/all
MPLAYER=$(which mplayer)

if [ ! -n $MPLAYER ]; then
    echo "Install mplayer and try again."
    exit 1
fi

make_playlist () {

    TEMPLIST=$(mktemp -p "$HOME" $(basename "$0").XXXXXXXX --suffix=.tmp)

    if [ -d "$PLAYLISTDIR" ]; then
        touch "$PLAYLIST"
    else
        mkdir -p "$PLAYLISTDIR"
        touch "$PLAYLIST"
    fi

    find $AUDIOPATH -print -type f > "$TEMPLIST"
    grep -i 'mp3$' "$TEMPLIST" > "$PLAYLIST"
    grep -i 'ogg$' "$TEMPLIST" >> "$PLAYLIST"
    grep -i 'm4a$' "$TEMPLIST" >> "$PLAYLIST"
    grep -i 'mid$' "$TEMPLIST" >> "$PLAYLIST"
    grep -i 'wma$' "$TEMPLIST" >> "$PLAYLIST"
    grep -i 'flac$' "$TEMPLIST" >> "$PLAYLIST"
    grep -i 'mp4$' "$TEMPLIST" >> "$PLAYLIST"

    rm "$TEMPLIST"

    echo Total files: $(wc -l "$PLAYLIST")
}

checkplay() {
    if [ ! -f "$PLAYLIST" ]; then
        make_playlist
        $MPLAYER -shuffle -playlist $PLAYLIST
    else
        $MPLAYER -shuffle -playlist $PLAYLIST
    fi
}

usage() {
    cat << EOF
Usage: $(basename "$0") [ARG]

  [digit]                 Doze for [digit] seconds and start playing
  [digit]m                Doze for [digit] minutes and start playing
  [digit]h                Doze for [digit] hours and start playing
  -c | --create           Create playlist w/o playing it
  -h | --help             Show this help
  -d | --dir [dir]        Play all audio files in [dir]
  -t | --track [path]     Play just one track
  [digit] path-to-track   "Alarm with a certain track." Doze for [digit]
                          and play specified track, continuing with
                          random play

                          w/o ARG playing starts immediately
EOF
}

# Show usage
if [ "$1" == "-h" -o "$1" == "--help" ]; then
    usage
    exit 0
fi

# Just create playlist w/o playing it
if [ "$1" == "-c" -o "$1" == "--create" ]; then
    make_playlist
    exit 0
fi

# Play directory
if [ "$1" == "-d" -o "$1" == "--dir" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        if [[ "$2" != */ ]]; then
            DIR="$2"/
        else
            DIR="$2"
        fi
        $MPLAYER "$DIR"*
        exit 0
    fi
fi

# Play a track
if [ "$1" == "-t" -o "$1" == "--track" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        $MPLAYER "$2"
        exit 0
    fi
fi

# Start playing immediately
if [ "$#" -eq 0 ]; then
    checkplay
    exit 0
fi

# Doze first
if [ "$#" -eq 1 ]; then
    sleep "$1"
    checkplay
    exit 0
fi

# Doze and play "alarm"
if [ "$#" -eq 2 ]; then
    sleep "$1"
    $MPLAYER "$2"
    checkplay
    exit 0
fi


telsu
This one basically shows what's on Finnish TV at the moment. The channels are fetched from Telkku.com.
#!/bin/bash

# Name: telsu
# Usage: telsu [OFFSET]

TERMWIDTH=$(tput cols)

bigline() {
    for (( i=0; i<$TERMWIDTH; i++ )); do
        echo -n "="
    done
}

smallline() {
    for (( i=0; i<$TERMWIDTH; i++ )); do
        echo -n "-"
    done
}

if [ "$#" -eq 1 ]; then
    OFFSET="$1"
else
    OFFSET=7
fi

bigline

w3m -dump http://www.telkku.com | grep -A"$OFFSET" Haku | sed -e \
    "s/.*Haku.*//g"

smallline

w3m -dump http://www.telkku.com?ks=1 | grep -A"$OFFSET" Haku | sed -e \
    "s/.*Haku.*//g"
# FIXME
#| cut -c 1-56,92-

bigline

echo
echo "Time now: $(date +%H.%M)"

exit 0

my2cents
A Python (!) script that converts just ratios to cents. If that sounds like gibberish, check out Just Intonation Explained.
#!/usr/bin/env python

"""
my2cents 0.1 by kasmra 2007
--------------------------
This program converts just ratios
to cents. 'N' is the nominator and 
'P' is the denominator of given just
ratio.
"""

from __future__ import division
import math

print '--- my2cents 0.1 ---'
print '  (Type 0 to quit)'

cont = True

while cont:

    n = int(input('N: '))
    if n == 0:
        cont = False
        break

    p = int(input('P: '))
    if p == 0:
        cont = False
        break

    answer = math.log10(n/p) * 1200/math.log10(2)

    print str(n)+'/'+str(p)+' is '+str(answer)+' cents.'

underline-files
Get rid of whitespace in file names at current directory. Optionally according to file suffix.
#!/bin/bash

# Do *FILE_SUFFIX
# $ underline-files [FILE_SUFFIX]

# OR do all files in a current directory:
# $ underline-files

if [ "$#" -eq 1 ]; then
    for file in *"$1"
    do
        target=`echo "$file" | sed -e 's/ /_/g'`
        mv "$file" "$target"
    done
    exit 0
fi

for file in *
do
    target=`echo "$file" | sed -e 's/ /_/g'`
    mv "$file" "$target"
done
exit 0
change-suffix
Get rid of UPPERCASE file suffixes or change them to something completely different.
#!/bin/bash

# Change according to erm... file suffix desired
isuffix='.JPG'
osuffix='.jpg'

for i in *"$isuffix"
do 
    mv "$i" $(basename "$i" "$isuffix")"$osuffix"

    # Comment out below for non-verbose behaviour
    echo Moved "$i" to $(basename "$i" "$isuffix")"$osuffix"
done

# Ditto
echo Done.
nimip
See whose name day it is according to Finnish calendar. One can search by date (xx.x. as in 12.2., e.g. no leading zeroes) or by name (Xxxx as in Anna). If no parameter is given, current date is used.
#!/bin/bash

dump() {
    w3m -dump http://koti.mbnet.fi/~stinger/namedays.php?$VAR |\
    grep $RESULT | sed -e "s/.*ehdot.*//;s/.*JUHA.*//;\
    s/^[ \t]*//;s/[ \t]*$//"
}

is_int() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}

if [ "$#" -eq 0 ]; then
    DATE=$(date "+%-d.%-m.")
    VAR="pvm=$DATE"
    RESULT=$DATE
elif is_int ${1:0:1} ; then
    DATE="$1"
    VAR="pvm=$DATE"
    RESULT=$DATE
else
    NAME="$1"
    VAR="nimi=$NAME"
    RESULT=$NAME
fi

dump

exit 0

y
Enable YubNub search from your terminal emulator. The search results open in your favourite browser.
#!/bin/bash

# Usage:
#
# For example search at YouTube:
# $ y yt death may be your santa claus
#
# Or list all Debian related yubnub commands:
# $ y ls debian
#
# Etc. etc.

# Change as you please
CLIBROWSER='w3m'
GUIBROWSER='iceweasel'

# Change accordingly
BROWSER="$CLIBROWSER"

XTERMCMD='xterm -bg black -fg white -ls -j -s -sl 10000 +sb -fa \
LucidaTypewriter -fs 9 -e'
SEARCHSTRING="$@"

case "$BROWSER" in
    "$CLIBROWSER")    
        # Uncomment below to spawn results into a new xterm window
        #$XTERMCMD \
        $BROWSER http://yubnub.org/parser/parse?command="$SEARCHSTRING"
        ;;
    "$GUIBROWSER")
        $BROWSER http://yubnub.org/parser/parse?command="$SEARCHSTRING"
        ;;
esac
cliching
This Python script randomizes I Ching hexagrams (figure names included) for you. By default it uses the "marble method". Also the more traditional coin tossing method is supported.

(Get it or take a look at it from the link above.)

Copyleft by kasmra 2008-2010