Brent Dodson

Web Programming Page

eightball Register
Unregistered users click here to register registered users can post their website here
eightball Science Humor:
Algebraic symbols are used when you do not know what you are talking about.
-- Philippe Schnoebelen
eightball Chuck Norris Humor:
The universe was created when Chuck Norris roundhouse kicked a star in the face for being too bright.
eightball Jobs:
Need a Job?
Check these out!!!!!
Jobs available
Execute a Unix Command with Node.js -
// http://nodejs.org/api.html#_child_processes
var sys = require('sys')
var exec = require('child_process').exec;
var child;

// executes `pwd`
child = exec("pwd", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});


// or more concisely
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("ls -la", puts);
Remove all .svn and dot-underscore files -

find . -name ".svn" -prune -o -name "._*" -exec rm -if {} \;
linux shell: making the shell alert or beep -



echo -en '\007'
Create a daily notes file using a Ruby shell script -
The following snippet launches a new gedit file with todays date when executed from a bash script.

gedit `ruby -e "puts %q(m/notes%s.txt) % Time.now.strftime('%d%m%y')"`

observed: gedit starts with a new file called notes050210.txt in the ~/m directory.
Install and run a seperate instance of opera including tsocks -

# download and unzip opera ...
% sh install.sh --prefix=/opt/opera-socks

# run opera with profile prefix and tsocks:
% tsocks /opt/opera-socks/bin/opera -pd socks
# opera does not support sockets directly
# remember that dns is not routed over socks (afaik)

% cat /etc/tsocks.conf
local = 10.0.0.0/255.255.255.0

server = 10.0.0.204
server_port = 1080
server_type = 5
BASH: Re-run previous command performing global substitution -
Re-run the previous command (accessed via !!) replacing 'foo' with 'bar' globally.


!!:gs/foo/bar/
Launch parallel shell process -
Bash in not specially well suited to run background jobs, but it can be accomplish. This is a simple parallel process launcher:


#!/bin/bash

# Echo to stderr
#
debug() {
echo "$@" >&2
}

# check_process PID
#
check_process() { kill -0 $1 2>/dev/null; }

# run_parallel NPROCESS FACTORY
#
# Run N parallel proceses, wait until EOF and all running processes are finished
#
#
# FACTORY is a process/function that returns (using stdout) the command to run
#
# The status code of FACTORY is interpreted like this:
#
# 0: There is more data
# != 0: There is no more data to process
#
run_parallel() {
NPROCESS=$1
shift

PIDS=()
EOF=0
while test $EOF = 0 -0 ${#PIDS[*]} -ne 0; do
if test $EOF = 0 -a ${#PIDS[*]} -lt $NPROCESS; then
if ! COMMAND=$("$@"); then
debug "EOF"
EOF=1
fi
if test "$COMMAND"; then
$COMMAND &
PID=$!
debug "starting process: $@ ($PID)"
PIDS[$PID]=$PID
fi
fi

debug "active processes: ${PIDS[*]}"
sleep 0.5

for PID in ${PIDS[*]}; do
if ! check_process $PID; then
wait $PID
unset PIDS[$PID]
debug "process finished: $PID"
fi
done
done
}


An example of usage:


process() {
read -t1 SEC &&{ debug "sleep $SEC"; return; }
test $? -eq 142 &&return || return 1
}

run_parallel 4 process
disk usage statics shell -
// description of your code here


dirs=`ls`
for dir in $dirs
do
if [ -d $dir ]
then
size=`cd $dir;du -sh`
echo "$dir $size"
fi

done
Simple files watch associated to commands -
The is the scenario: you want to run a command wherever a file (or files) is modified.

This is the most basic approach (you will need inotify):


#!/bin/bash
# Usage: onchange.sh "COMMAND" FILE1 ... [FILEN]
set -e
RUNFIRSTLOOP=1

COMMAND=$1
shift
test "$RUNFIRSTLOOP" &&bash -c "$COMMAND" || { echo "retcode: $?"; }

while inotifywait "$@"; do
bash -c "$COMMAND" || { echo "retcode: $?"; }
done
open gem with textmate -
open_gem is a rubygem. It adds a Gem Command to easily open a ruby gem with an editor. The Editor is specified with the -c option (or in $GEM_OPEN_EDITOR or $EDITOR). After Installation


$ sudo gem update --system
$ sudo gem install open_gem


this is the Syntax

$ gem open name_of_the_gem_to_open -c name_of_the_editor


and this Example opens 'open_gem' with 'textmate'

$ gem open open_gem -c mate

 Use OpenOffice.org        Spread Firefox Affiliate Button

For any questions or enquiries, i can be reached at my email
I look forward to hearing from you

Copyright © 2010 brentdodson.com