Useful scripts

uncue

Today I practiced ruby and wrote v0.0.1 of uncue script

http://hg.bsdroot.lv/aldis/uncue

Right now it's pretty basic script. The purpose of uncue is to uncue. In otherword to split media (currently only audio) files with cue sheet.

I will keep developing this scipt further.
My biggest goal (to split audio files) is however almost achieved.

Some highlights:
  • Tries to check cue file
  • Tries to find files to uncue
  • Keeps metadata
  • Has bugs :)
I think for my first Ruby script it's pretty good. Especially that I've only spent one day, to write it :)

NOTE: This is so very alpha.... in case you try to uncue your media files, don't throw avay originals for now.

It should be relatively save as long as, cue sheet, has one FILE command and only AUDIO tracks...

Currently it accepts only 1 command line argument, and that argument must be cue filename.
Script depends on multimedia/ffmpeg and lang/ruby19
 
I was improving my desktop scripts and update agentsCtl.sh script
http://hg.bsdroot.lv/aldis/wmscripts/file/tip/agentCtl.sh

The main purpose of this script was to make sure, that gpg-agent and ssh-agend only run one instance.
I rewrote it pretty well (I think)

As side effect, if you add
Code:
eval `"$WMSCRIPTS_DIR/agentCtl.sh" start all`
to your ~/.xinitrc and ~/.shrc I can share agents across X11 and console. Also I can share them accross Logouts :)

In few minutes I will adopt this script to be able to work with csh & frieds
 
Here's script that will exec and then refresh www/uzbl every time some subdirectory is modified.
Useful for web development (Ide from http://railscasts.com/episodes/264-guard), It by no means is a substitute. I was just interested in writing something similar

Code:
#!/bin/sh

DIR_REFRESH_TIMEOUT=300

LOCK_FILE=".web-dev-uzbl.tmp"
DEFAULT_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/uzbl/config"

updated_dirlist() {
	find . -type d | grep -v '/\.' > "$LOCK_FILE"
}

last_updated() {
	cat "$LOCK_FILE" | xargs -n 1 ls -d -D %s -l | awk '{print $6}' | sort | tail -n 1
}


if [ -f "$LOCK_FILE" ]; then
	rm -f "$LOCK_FILE"
	sleep 3
fi

updated_dirlist > "$LOCK_FILE"

{
	[ -f "$DEFAULT_CONFIG" ] && cat "$DEFAULT_CONFIG"
	echo "uri $1"
	
	LAST_UPDATED=0
	i=0
	while [ -f "$LOCK_FILE" ]; do
		sleep 1
		LAST_UPDATED_NOW=`last_updated`
		if [ $LAST_UPDATED -lt $LAST_UPDATED_NOW ]; then
			echo reload_ign_cache
			LAST_UPDATED=$LAST_UPDATED_NOW
		fi

		i=$(($i+1))
		if [ $i -gt $DIR_REFRESH_TIMEOUT ]; then
			updated_dirlist
			i=0
		fi

	done

	exit

} | uzbl-browser -c -

# vim: set ts=4 sw=4 :
 
Instant event handler will be very significantly (thousands of times) more responsive than a time out and compare based mechanism. Maybe not so significant in human noticeable units but it will be literally many times more :D.
 
admin script...

I made this script only for myself and couple of friend first, but soon it became much bigger project than I planned so why not share it with everyone. It script for doing much of admin/maintenance stuff within one simple command.

You can find it here: http://kpn.kahvipannu.fi/admin

All opinions and ideas etc. are welcome, but since I'm not on this forum on regular basis it would be nice to get feedback with email.
 
useful MDCONFIG wrapper

Usage:

Code:
% [color="Blue"]dd < /dev/zero > FILE bs=1m count=100[/color]
100+0 records in
100+0 records out
104857600 bytes transferred in 2.234466 secs (46927368 bytes/sec)

% [color="blue"]ls -lh FILE[/color]
-rw-r--r--  1 vermaden  vermaden   100M 2011.10.04 15:00 FILE

% [color="blue"]mdconfig.sh -c FILE[/color]
IN: created vnode at /dev/md0

% [color="blue"]mdconfig.sh -l[/color]
md0     vnode     100M  /usr/home/vermaden/FILE

% [color="blue"]mdconfig.sh -d 0[/color]
IN: deleted vnode at /dev/md0

Code:
Code:
#! /bin/sh

__usage() {
  NAME="$( basename ${0} )"
  echo "usage: $( basename ${0} ) OPTION FILE|DEVICE"
  echo
  echo "options:    -l --list   - list existing vnode(s)"
  echo "            -c --create - create new vnode"
  echo "            -d --delete - delete existing vnode"
  echo
  echo "examples: ${NAME} -c ~/FILE"
  echo "          ${NAME} -d 0"
  echo "          ${NAME} -l"
  echo
  exit 1
}

[ ${#} -eq 2 -o ${#} -eq 1 ] || __usage

case ${1} in
  (-l|--list)
    sudo mdconfig -l -v
    ;;

  (-c|--create)
    [ -f ${2} ] || {
      echo "ER: file '${2}' does not exist"
      echo
      __usage
    }
    echo -n "IN: created vnode at /dev/"
    sudo mdconfig -a -t vnode -f ${2}
    ;;

  (-d|--delete)
    [ -c /dev/md${2} ] || {
      echo "ER: device '/dev/md${2}' does not exist"
      echo
      __usage
    }
    sudo mdconfig -d -u ${2}
    echo "IN: deleted vnode at /dev/md${2}"
    ;;

  (*)
    __usage

esac
 
I had enough of updating vim plugins and help tags manually, so I wrote 2 scripts:

This script updates my plugins by downloading tar.gz from github:
update_plugins.sh modified and renamed: https://github.com/graudeejs/dot.vim/blob/master/github_plugin_update.sh

And this script updates all help tags for all doc directories in ~/.vim/
https://github.com/graudeejs/dot.vim/blob/master/update_tags.sh


EDIT, I updated scripts.... so now there's also Makefile:
https://github.com/graudeejs/dot.vim/blob/master/Makefile

enjoy!
 
Code:
#!/bin/sh

TMPZIP=$( /usr/bin/mktemp /tmp/hostszip.XXXXXX )
TMPHOSTS=$( /usr/bin/mktemp /tmp/hosts.XXXXXX )

if $( ! /bin/test -e /etc/hosts.original ); then
    echo "  ~  0  ~   Backing up original hosts..."
    /bin/cp /etc/hosts /etc/hosts.original
fi

echo "  ~  1  ~   Fetching..."
/usr/bin/fetch -o $TMPZIP http://hostsfile.mine.nu.nyud.net/hosts.zip

echo "  ~  2  ~   Extracting..."
/usr/bin/tar Oxf $TMPZIP | tr -d '\r' > $TMPHOSTS

echo "  ~  3  ~   Respawning..."
/bin/cat /etc/hosts.original $TMPHOSTS > /etc/hosts
/bin/rm -f $TMPZIP $TMPHOSTS

This script will download a (regularly updated) list of more than 95,000 hostnames used for advertising/etc. and append it to your /etc/hosts file. In subsequent runs, /etc/hosts will be respawned from a copy of your original hosts file (saved to /etc/hosts.original) and the fetched list.

There are several versions of this script online, but all for Linux. So I adapted one for FreeBSD: changed shells/bash to sh(1), and removed its dependence on ftp/wget, archivers/unzip, and converters/unix2dos.

Comments are welcome: I am new to shell scripting.

-edit-

Please note that the list is quite comprehensive. It includes, e.g.:
Code:
127.0.0.1       clients2.google.com
127.0.0.1       clients3.google.com
127.0.0.1       clients4.google.com
127.0.0.1       clients5.google.com
These hosts are used to install extensions for Chromium. So uncomment these if necessary.
 
SNK
named(8) - Internet domain name server.
DNS server, that came to substitute hosts file those days, when the quantity of hosts began to grow.
 
I've been reading up on scripting with the bourne shell... I mashed face agains the wall for a few hours putting together a script to check latency on FreeBSD cvsup servers for fetching/updating ports.

Code:
#!/bin/sh

#start with cvsup1.freebsd.org
server=1

#intended to error check at the end, haven't implemented yet
fastest=-1 

#some absurd latency to compare to initially
seed=2000.0

echo "Pinging cvsup servers..."
while [ $server -le 18 ]
do
	speed=`ping -nqt 4 cvsup$server.freebsd.org | \
                     grep 'round-trip*' | awk '{n=split($4,array,"/"); print array[2];}'`
	if [ "$speed" != "" ]
		then
		result=`echo "$speed < $seed" | bc -l`
		if [ "$result" -eq 1 ] 
		then
			seed=$speed
			fastest=$server
		fi
	fi
	server=`expr $server + 1`
done;
echo "cvsup$fastest.freebsd.org"

Obviously, this only works for US servers at this point...

Edit: Adding to this, I may have a question for clarification. I came across a few comments on random forums searching for answers, and while looking for the answer to floating point comparison in sh(), I noticed a comment talking about how bc() returns 1 for true and 0 for false and how it was not "bashlike"... This confused me, but agreed with a statement I read elsewhere, how bash and other GNU implementations of software have catered to poor coding and standards, enforcing worse habits. I guess, I immediately thought of C/C++ that treat 1 or any other non zero value as true and 0 as false. This would make sense as to why other programs such as bc(), return 1 for true and 0 for false... Right? Why would bash() be any different?
 
zeroseven said:
I've been reading up on scripting with the bourne shell... I mashed face agains the wall for a few hours putting together a script to check latency on FreeBSD cvsup servers for fetching/updating ports.

Remember that lowest latency is not necessarily the fastest. Might want to compare with sysutils/fastest_cvsup.
 
wblock@ said:
Remember that lowest latency is not necessarily the fastest. Might want to compare with sysutils/fastest_cvsup.

Haha.. I knew there was probably a tool for this, I just didn't put in the legwork to try and find it... Really, I was just looking for an excuse to get my hands dirty with a shell script.. I know some C/C++ and and have a little experience with some other languages. I'm just now starting to read up on things like grep, sed, awk and sh scripting.
 
Here's userscript to switch youtube to html5=1 automatically

Code:
// ==UserScript==
// @include http://youtube.com/*
// @include http://*.youtube.com/*
// ==/UserScript==

var l = window.location.href;
var a = l.split('?');
var base_address = a[0];
var params = a[1].split('&');

var html5 = false;
for (var i = 0; i < params.length; i++) {
  if (params[i].match(/^html5=/)) {
    html5 = true;
    break;
  }
}

if (html5 == false) {
  params.push('html5=1');
  var address = [base_address, params.join('&')].join('?');
  window.location.href = address;
}
 
# I find union , dis-union reduce more complicated
# scripting. The below is "huge list" able, small.

Code:
#!/bin/sh
set -e
if [ -z "$1" ] || [ -z "$2" ] ; then
cat << EOF >> /dev/stderr
usage: [dis-]union file1 file2
descr: files are sets, lines in are elements (newline separated list)
limit: f1 f2 same as f2 f1, result is sort(1)ed; not in sets order
       very fast w/large file support, not bad for a shell script
       re-ordering result to orig. order is a linear operation
EOF
exit ; fi
[ -f "$1" ]
[ -f "$2" ]
cat "$1" | sort | uniq > s1.$$ ;  cat "$2" | sort | uniq > s2.$$
cat s1.$$ s2.$$ | sort | uniq -d
rm s1.$$ s2.$$

Code:
#!/bin/sh
set -e
if [ -z "$1" ] || [ -z "$2" ] ; then
cat << EOF >> /dev/stderr
usage: [dis-]union file1 file2
descr: files are sets, lines in are elements (newline separated list)
limit: f1 f2 same as f2 f1, result is sort(1)ed; not in sets order
       very fast w/large file support, not bad for a shell script
       re-ordering result to orig. order is a linear operation
EOF
exit ; fi
[ -f "$1" ]
[ -f "$2" ]
cat "$1" | sort | uniq > s1.$$ ;  cat "$2" | sort | uniq > s2.$$
cat s1.$$ s2.$$ | sort | uniq -u
rm s1.$$ s2.$$

# X login script(s):

# http://www.sourceforge.net/projects/xdm-options

# dependancy sorting related program and scripts:

# http://www.sourceforge.net/projects/dep-trace
 
GNOME - Use the FreeBSD logo as main icon

Yesterday, I installed 4 FreeBSD workstations in a public space. The dialog after X launch:
- "Hey, why do we have the GNOME logo instead the distro one?"
- "Well... As I said before, FreeBSD isn't a distro but a full operating system, different than Linux [etc.]"
- "Yes, yes, yes, an OS if you want, but what about the icon?"

I so prepared a small script to fetch the FreeBSD logo, crop and resize it, and replace the places/start-here.png files.

Code:
#!/bin/sh
#Fetches FreeBSD full logo, crops it and deploys it as Gnome application icon.
#Requires ImageMagick (graphics/ImageMagick port)

GNOME_ICON_PATH=/usr/local/share/icons/gnome

#Fetches logo and crops it
cd /tmp
fetch http://www.freebsd.org/logo/logo-full.png
convert logo-full.png -gravity West -crop 175x175+0+0 logo.png

#Deploys it to gnome icons
convert logo.png -resize 48x48 ${GNOME_ICON_PATH}/48x48/places/start-here.png
convert logo.png -resize 32x32 ${GNOME_ICON_PATH}/32x32/places/start-here.png
convert logo.png -resize 24x24 ${GNOME_ICON_PATH}/24x24/places/start-here.png
convert logo.png -resize 22x22 ${GNOME_ICON_PATH}/22x22/places/start-here.png
convert logo.png -resize 16x16 ${GNOME_ICON_PATH}/16x16/places/start-here.png

#Deletes logo and icons cache
rm logo.png logo-full.png ${GNOME_ICON_PATH}/icon-theme.cache

#User help
echo "FreeBSD logo icon has been installed. To enable it, you need to refresh your GNOME theme."
echo "Menu System > Preferences > Appearance > Button Customize..."
echo "Tab Icons > double-click on your current theme to load new logo icon."
 
kpn43 said:
I made this script only for myself and couple of friend first, but soon it became much bigger project than I planned so why not share it with everyone. It script for doing much of admin/maintenance stuff within one simple command.

You can find it here: http://kpn.kahvipannu.fi/admin

All opinions and ideas etc. are welcome, but since I'm not on this forum on regular basis it would be nice to get feedback with email.

Cool project. But I must ask you: why did you write it in bash? Seems a little weird for a program that's made for FreeBSD.
 
Print $PATH Line by Line

Sometimes it's difficult to locate a particular directory in a long $PATH variable. I wrote a quick Perl script to list each $PATH directory line by line. I named it ppath- Perlpath.

Script:
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $path;
my @paths;

$path = $ENV{'PATH'};
@paths = split(/:/,$path);
foreach (@paths) {
        print $_ . "\n";
}

Output:
Code:
/sbin
/bin
/usr/sbin
/usr/bin
/usr/games
/usr/local/sbin
/usr/local/bin
/home/rob/bin
 
rob34 said:
Sometimes it's difficult to locate a particular directory in a long $PATH variable. I wrote a quick Perl script to list each $PATH directory line by line. I named it ppath- Perlpath.

Script:
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $path;
my @paths;

$path = $ENV{'PATH'};
@paths = split(/:/,$path);
foreach (@paths) {
        print $_ . "\n";
}

Output:
Code:
/sbin
/bin
/usr/sbin
/usr/bin
/usr/games
/usr/local/sbin
/usr/local/bin
/home/rob/bin

$ echo $PATH | tr ':' ' ' | xargs -n 1 echo
You can probably write this even shorter
 
Back
Top