Tag Archives: script

uzbl follow.js: link following with settable keys

EDIT: this script is no longer up-to-date and won’t work with the current version of uzbl.

This is a diff to current follow_Numbers.js. The new script, which I called follow.js, is more versatile, since the characters used for labels and key navigation can be set using the hintKeys variable.

2d1
<  * Its pretty stable, only using numbers to navigate.
12a12,14
> //This string should be 10 characters long. Play around!
> var hintKeys = 'asdfghjkl;';
> 
24c26
< //Make onlick-links "clickable"
---
> //Make onclick-links "clickable"
195a198,216
> //Map 0123456789 to hintKeys
> function mapNumToHintKeys(label) {
>     label = label + '';
>     for (var j = 0; j < label.length; j++) {
>     var pos = parseInt(label[j]);
>     label = label.replace(label[j], hintKeys[pos]);
>     }
>     return label;
> }
> //Map hintKeys to 0123456789
> function mapHintKeysToNum(label) {
>     label = label + '';
>     for (var j = 0; j < label.length; j++) {
>     var pos = hintKeys.search(label[j]);
>     if (pos < 0 || pos > 9) return;  // bad input, key not in hintKeys
>     label = label.replace(label[j], pos+'');
>     }
>     return parseInt(label);
> }
199c220
<     var linknr = parseInt(follow, 10);
---
>     var linknr = mapHintKeysToNum(follow);
216a238
>         label = mapNumToHintKeys(label);

Advertisements

Emacs with Tramp using zsh

First off, I recommend reading the relevant parts of the user manual, you might find the answer there.

Now, to the issue.

I installed tramp and added this to ~/.emacs:

(require 'tramp)
(setq tramp-default-method "scp")

Nothing special here, except it didn’t work. (The message was something like “waiting 60s for local shell”. Of course, I didn’t write it down, and it got lost in the browsing cache.)

So I turned on more verbose debugging, in ~/.emacs also:

(setq tramp-debug-buffer t)
(setq tramp-verbose 10)

The “logs”, mind you, are written into buffers: *debug/scp remotehost* are the messages and *tramp/scp remotehost* is the medium-rare (as compared to raw) data received.

If you open the latter and see characters like ^]]C4 or ^M, then it might be due to the shell prompt setting on the remote host. The manual advises wisely as to what should be considered.

One solution found floating on the internets is checking for the terminal type. Add this to the remote host’s .zshenv:


if [ "$TERM" = "dumb" ]
then
unsetopt zle
unsetopt prompt_cr
unsetopt prompt_subst
# unfunction precmd # these two are not
# unfunction preexec # working for me
PS1='$ '
fi

Also, make sure you’re not re-setting it somewhere else later, say .zshrc. A check should be enough even if you do:

if [ "$TERM" != "dumb" ]; then
# Set prompts
PROMPT='%m%# '
RPROMPT='[%C]'
fi

uzbl_tabbed.py: commands to FIFO

The list is on line 782 (ATM) of uzbl_tabbed.py. The FIFO location is determined by your settings (top of file for defaults, possibly overridden from config). It’s like this:

782     def parse_command(self, cmd):
783         '''Parse instructions from uzbl child processes.'''
784
785         # Commands ( [] = optional, {} = required )
786         # new [uri]
787         #   open new tab and head to optional uri.
788         # close [tab-num]
789         #   close current tab or close via tab id.
790         # next [n-tabs]
791         #   open next tab or n tabs down. Supports negative indexing.
...

So, to open a new tab in uzbl_tabbed from newsbeuter, you need a script like newtab.sh:

#!/bin/sh
echo "new $1" > `ls -1 /tmp/uzbltabbed_*`

Then call it from newsbeuter – put this in ~.newsbeuter/config:

browser "~/.local/share/uzbl/scripts/newtab.sh %u"

Lousy jailed-lighttpd scripts

Now, jailed has its own automated scripts to generate its own proper fully-contained jails (as far as “jails” on GNU/Linux go, that is; they’re chroots, actually).

The following scripts are leftovers from me trying to do the same, before finding out that the utilities are already there. They have been lying around for quite some time. I’m just copying them here so I can delete them from the home dir.

=====

jailed-lighttpd-install
—–

#!/bin/sh
# Create a jail for lighttpd

jail=/jail/lighttpd

mkdir -p $jail
cd $jail

mkdir tmp
chmod 1777 tmp

mkdir -p        var/log/lighttpd var/run/lighttpd home/http
chown http:http var/log/lighttpd var/run/lighttpd home/http
chmod a+rx      var/log/lighttpd var/run/lighttpd home/http

mkdir etc
cp -avr /etc/php etc/
cp -v /etc/hosts /etc/nsswitch.conf /etc/resolv.conf /etc/services /etc/localtime etc/
cp -v /etc/group /etc/host.conf /etc/passwd /etc/protocols etc/

mkdir -p usr/bin
cp /usr/bin/php /usr/bin/php-cgi usr/bin/

# read l2chroot before use
./l2chroot /usr/bin/php
./l2chroot /usr/bin/php-cgi

# just in case some permissions were forgotten...
chmod a+rx $jail

=====

jailed-lighttpd.rc
—–

#!/bin/bash

# general config
. /etc/rc.conf
. /etc/rc.d/functions

jailroot=/jail/lighttpd
function jailcmd () {
su http -c "$*"
}

PID=`pidof -o %PPID /usr/sbin/lighttpd`

case "$1" in
start)
stat_busy "Starting jailed lighttpd Daemon"
[ -z "$PID" ] && jk_chrootlaunch -j $jailroot -x /usr/sbin/lighttpd -- -f /etc/lighttpd/lighttpd.conf 2>&1
if [ $? -gt 0 ]; then
stat_fail
else
add_daemon lighttpd
stat_done
fi
;;
stop)
stat_busy "Stopping jailed lighttpd Daemon"
[ ! -z "$PID" ] && jailcmd kill $PID &>/dev/null
if [ $? -gt 0 ]; then
stat_fail
else
rm_daemon lighttpd
rm -f $jailroot/var/run/lighttpd/lighttpd.pid
stat_done
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac

=====

l2chroot
—–

#!/bin/bash
# Use this script to copy shared (libs) files to Apache/Lighttpd chrooted
# jail server.
# ----------------------------------------------------------------------------
# Written by nixCraft <http://www.cyberciti.biz/tips/>
# (c) 2006 nixCraft under GNU GPL v2.0+
# + Added ld-linux support
# + Added error checking support
# ------------------------------------------------------------------------------
# See url for usage:
# http://www.cyberciti.biz/tips/howto-setup-lighttpd-php-mysql-chrooted-jail.html
# -------------------------------------------------------------------------------
# Set CHROOT directory name
BASE="/jail/lighttpd"

if [ $# -eq 0 ]; then
echo "Syntax : $0 /path/to/executable"
echo "Example: $0 /usr/bin/php5-cgi"
exit 1
fi

[ ! $BASE ] && mkdir -p $BASE || :

# iggy ld-linux* file as it is not shared one
FILES="$(ldd $1 | awk '{ print $3 }' |egrep -v ^'\(')"

echo "Copying shared files/libs to $BASE..."
for i in $FILES
do
d="$(dirname $i)"
[ ! -d $BASE$d ] && mkdir -p $BASE$d || :
/bin/cp $i $BASE$d
done

# copy /lib/ld-linux* or /lib64/ld-linux* to $BASE/$sldlsubdir
# get ld-linux full file location
sldl="$(ldd $1 | grep 'ld-linux' | awk '{ print $1}')"
# now get sub-dir
sldlsubdir="$(dirname $sldl)"

if [ ! -f $BASE$sldl ];
then
echo "Copying $sldl $BASE$sldlsubdir..."
/bin/cp $sldl $BASE$sldlsubdir
else
:
fi

=====

There. Sorry for the tabbing, it went AWOL.

Simple script to see file sizes

#!/usr/bin/python
# directory size - show it

import os, sys

if len(sys.argv) < 2:
    os.system("du -sh *")
    print "-----"
    os.system("du -sh .")
else:
    for arg in sys.argv[1:]:
        cmd = "du -sh \"%s\"" % arg
        os.system(cmd)

Batch image resizing

My php script no longer works, it breaks on gd functions somehow.

So I use this Gimp script instead.

A PHP script to resize all images in a directory

This is a crude hack to resize images in a directory. The script is invoked as:

shrink <new_x> <new_y> <file> <file> <file> ...

You’ll need PHP with the GD library. That’s php5-gd on FreeBSD.


#!/usr/local/bin/php
# The output is JPEG, quality 88.

<?php



// Fuction to resize images, originally by D. Weimer.
// Code reformatting done, and '>' changed to '<'.
// http://www.dweimer.net/?page=help&subpage=imageresize
function resize($img,$nx,$ny)
{
    // $x and $y: original image's sizes.
    $x = imagesx($img);
    $y = imagesy($img);

    // Here, $nx and $ny are the minimal desired dimensions.
    if ( ($x/$nx) < ($y/$ny) )
    {
        $ny = round( $y * ($nx/$x) );
    }
    else
    {
        $nx = round( $x * ($ny/$y) );
    }

    $nimg = imagecreatetruecolor($nx,$ny);
    imagecopyresampled($nimg,$img,0,0,0,0,$nx,$ny,$x,$y);
    imagedestroy($img);
    return($nimg);
}



$arguments = $_SERVER['argv'];          // What the user wants to process.
array_shift($arguments);                // 1st element is the path to the script itself.
$newx = array_shift($arguments);        // 2nd is the new desired x dimension.
$newy = array_shift($arguments);        // 3rd is new y.
                                        // all the remaining args are images.

foreach ( $arguments as $arg )          // Go over user's shown items (single or "heaps").
{
    foreach (glob("$arg") as $filename) // Go over every file in a "heap".
    {
        $image = file_get_contents($filename);
        $image = imagecreatefromstring($image);

        $image = resize($image, $newx, $newy);

        imagejpeg( $image, $filename, 88 ); // WARNING!!! Writes JPEG by default!
    }
}


?>

I was too lazy to go searching for something pre-made.

Making a movie from image files using ffmpeg

There’s quite a lot of tutorials on the net, search for the title.

I had an issue, though: the images were taken with a web camera, and some of the images were garbled (wrong header, bad data, etc.). So ffmpeg choked on those silently, and the movie got cut off in the middle.

To find those, use something like

file * | grep

Also, to rename the images from the camera:

#!/bin/sh
#

n=1

for i in 122*; do
    echo $n
    cp $i processed/$n.jpg
    n=`expr $n + 1`
done

wpi-monitor: simple script to keep the wireless link up

I’m using a rum device in Host AP mode, so every time I try to send a large file or open many connections, the wireless link breaks down (gets “no carrier” instead of “associated”). To get it back up, I have to pull the interface on the station computer (wpi) down and then back up.

To automate the process, I’ve written this simple script. To be run with root/operator permissions.

#!/bin/sh
# Stop wpi from falling off.

if=wpi0 # interface name

while true
do
        # see if it has "no carrier" status, by looking for "no"
        status=`ifconfig $if | grep status | cut -d ":" -f 2 | cut -d " " -f 2`

        # bourne shell string comparison... you can has it
        if [ "$status" = "no" ] ; then
                ifconfig $if down
                ifconfig $if up
#               echo down
        else
#               echo up
        fi

        sleep 1
done

Copying music to USB flash from command line, fast

Thanks to this post.

EDIT: This doesn’t work well.

#!/bin/sh
# Script to copy music to media player (USB).

# remove the last character
var=`echo $*|sed s/.$//`

echo "Copying: $var"

# now copy
cp -R "$var" /mnt/player/ &