Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 31-40 of 130 total

svn-add-all-new-files

alias svn-add-all-new-files='svn st|grep ^?|sed s/?//|xargs svn add $1'

Download all xkcd.com comics

This goes through all the first 329 (you might want to change this) pages, downloading the comic strips.

#!/bin/bash

for i in `seq 1 329`
do
	wget http://xkcd.com/$i/
	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
	rm index.html
done

Convert .rhtml templates to .html.erb

// run this shell command from within the dir you want to change. You will need to change the hg line to use which ever version control you are using, or if you're not use version control (naughty!) then leave out the hg letters altogether.

for f in $(find . -name '*.rhtml') ; do c=$(dirname $f)/$(basename $f .rhtml).html.erb ; hg mv $f $c ; done

Flac to mp3

Converts all *.flac files in the current dir to mp3.

#!/bin/bash

for file in *.flac
do
	echo Converting $file
	flac123 -q --wav=- "$file" | lame - "$file".mp3
done

Choose profile before launching GNOME Terminal emulator

This is a small sheel scripts that uses zenity to show a dialog that lets you choose the profile you want to use before launching a GNOME terminal window.

#!/bin/sh

#################################################
# Simple zenity script to display a dialog from #
# which to choose the profile you want for a    #
# GNOME terminal you want to run.               #
# (C) 2007 - Antonio Ognio <gnrfan@gnrfan.org>  #
# License: GPL                                  #
#################################################

PROFILES=`gconftool-2 --all-dirs /apps/gnome-terminal/profiles`
LIST=""
for p in $PROFILES; do
  p=`basename $p`
  LIST="$LIST FALSE $p "
done
choosen=`zenity  --title "GNOME Terminal" --window-icon /usr/share/icons/gnome/scalable/apps/gnome-terminal.svg --text "Choose one from available profiles:" --list  --radiolist  --column "" --column "Profiles" $LIST`
gnome-terminal --window-with-profile=$choosen &

simple backup PHP application (php files + mysql db)

// backup a folder contains all .php files and it's mysql database. a backup.ini file is required for storing mysql root login and what folder to be backed up.

#!/bin/sh

########################################
# simple sctipt for daily / hourly backup of PHP app + Mysql DB
# Copyleft (c) Sayid Munawar. chenull@yahoo.com
# LICENSE: anyone can copy / modify / distribute. whatever lah
#
# example of workhours backup (8 am - 5 pm. Mon - Fri)
# 0 8-17 * * 1-5 /home/backup/backup.sh
#
# example of simple dayly backup ( 6 pm. Mon - Fri)
# 0 18 * * 1-5 /home/backup/backup.sh
########################################

########################################
# example of backup.ini. REMOVE ALL leading '#'s
# file must be chmod 600
#[main]
#target = /home/backup/storage
#mysql_user = root   ; root can connect to any db
#mysql_pass = secret ; mysql root password
#
#[hukum]
#path = /home/hukum
#mysql_db = hukum
#
#[phpmyadmin]
#path = /home/phpmyadmin
#mysql_db = test
########################################

PATH=$PATH:/usr/bin:/bin:/usr/local/bin

inifile=`dirname $0`/backup.ini

test ! -r $inifile && echo "ERROR! backup.ini not found nor readable" && exit 1

#test apakah backup.ini bisa dibaca orang lain
echo -n `stat -c '%a' $inifile` | grep -q '[0-7]00' >/dev/null 2>&1
test $? -gt 0 && echo "ERROR! $inifile can be read by others" && exit 2
#test `stat -c '%U' $inifile` != 'root' && echo "ERROR! $inifile ownernya bukan root" && exit 3

# function to parse ini file (backup.ini)
#
# $1 -> file.ini
# $2 -> section
_parse_ini () {
    if [ -z "$1" ] || [ -z "$2" ]; then return 0; fi
    eval `cat $1 | \
       sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
           -e 's/;.*$//' \
           -e 's/[[:space:]]*$//' \
           -e 's/^[[:space:]]*//' \
           -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" | \
       sed -n -e "/^\[$2\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}

_parse_ini $inifile main

# coba konek 
mysql -u $mysql_user  -p${mysql_pass} -e '' > /dev/null 2>&1
test $? -gt 0 && echo "ERROR! Failed to connect to mysql" && exit 4

# bikin target kalo blum ada
test ! -d $target && mkdir -p $target

hari=`date +%A`
jam=`date +%H`

# get pwd
cwd=`pwd`

# looping
for section in `cat $inifile | grep '^\[' | grep -v main | sed 's/\[//' | sed 's/\]//'`; do
  outdir=$target/$section/$hari/$jam
  echo Backing up ${section} to $outdir...
  # parse .ini
  _parse_ini $inifile $section
  # cek dulu
  test ! -d $path && echo "Warning: $path not found.  Backup process of $section skipped" && echo && continue
  mysql -u $mysql_user  -p${mysql_pass} -e '' $mysql_db > /dev/null 2>&1
  test $? -gt 0 && echo "Warning: Database $mysql_db not found.  Backup process of $section skipped" && echo && continue

  test ! -d $outdir && mkdir -p $outdir
  cd $path
  tar -czpf $outdir/$section.tar.gz .
  cd $cwd
  mysqldump -Q -u $mysql_user -p${mysql_pass} $mysql_db | gzip > $outdir/${mysql_db}.sql.gz
  ln -fs $outdir/$section.tar.gz $target/$section/latest.tar.gz
  ln -fs $outdir/${mysql_db}.sql.gz $target/$section/latest.sql.gz
done

gemdocs

Put this snippet into your ~/.bash_login (or alternatives) to browse your gem docs from the command line.


function gemdocs() { 

  if [[ -n "$(whereis gem_server)" ]]; then
     gem_server >/dev/null 2>&1 &
     sleep 3
     open http://127.0.0.1:8808/
  fi

}

bash : make tab completion of similarly named files not suck ass

// description of your code here

add this to your ~/.bashrc (auto-chooses the first completion and cycles thru them w/tab)
bind '"\t":menu-complete'


and / or add this to your ~/.inputrc (shows all completions right away)
set show-all-if-ambiguous on

Solaris 10 dotfiles...

Solaris 10: Failing to source .bashrc

Last login: Thu Aug  2 22:40:42 2007 from 192.168.1.20
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
-bash-3.00$ 
-bash-3.00$ 
-bash-3.00$ 
-bash-3.00$ 
-bash-3.00$ ls -lrt
total 40935
-rw-r--r-- 1 mcs other      136 Mar 18 17:51 local.cshrc
-rw-r--r-- 1 mcs other      157 Mar 18 17:51 local.login
-rw-r--r-- 1 mcs other      174 Mar 18 17:51 local.profile
drwxr-xr-x 2 mcs other      512 Mar 18 18:03 Documents
drwxr-xr-x 2 mcs other      512 Mar 18 18:03 Desktop
-rw-r--r-- 1 mcs other       98 Mar 25 22:19 local.bashrc
-rw------- 1 mcs other 41872384 Apr  1 19:50 firefox-2.0.0.3.en-US.solaris10-i386-pkg
-rw-r--r-- 1 mcs other      200 Apr  1 22:32 links
-bash-3.00$ ls -al
total 40976
drwxr-xr-x 17 mcs  other     1024 Aug  2 22:43 .
drwxr-xr-x  4 root root       512 Mar 18 17:51 ..
-rw-------  1 mcs  other     5182 Aug  2 22:42 .bash_history
-rw-r--r--  1 mcs  other      121 Aug  2 22:31 .bashrc
drwxr-xr-x  9 mcs  other      512 Jul  1 20:08 .dt
-rwxr-xr-x  1 mcs  other     5111 Mar 18 18:03 .dtprofile
-rw-------  1 mcs  other       16 Mar 18 22:57 .esd_auth
drwx------  4 mcs  other      512 Jul  1 20:08 .gconf
drwx------  2 mcs  other      512 Jul 30 19:52 .gconfd
drwx------  4 mcs  other      512 Mar 18 18:03 .gnome
drwx------  8 mcs  other      512 Jul  1 20:08 .gnome2
drwx------  2 mcs  other      512 Mar 18 18:03 .gnome2_private
-rw-r--r--  1 mcs  other       92 Mar 18 18:03 .gtkrc-1.2-gnome2
-rw-------  1 mcs  other     2123 Jul  1 20:08 .ICEauthority
drwxr-xr-x  3 mcs  other      512 Mar 18 18:03 .iiim
drwx------  3 mcs  other      512 Mar 18 18:03 .metacity
drwx------  4 mcs  other      512 Mar 18 18:04 .mozilla
drwxr-xr-x  3 mcs  other      512 Mar 18 18:03 .nautilus
-rw-r--r--  1 mcs  other      185 Mar 25 20:52 .profile
-rw-------  1 mcs  other      264 Apr  1 19:49 .recently-used
-rw-------  1 mcs  other       34 Aug  2 22:33 .sh_history
drwxr-xr-x  3 mcs  other      512 Mar 18 18:03 .softwareupdate
drwx------  2 mcs  other      512 Mar 19 19:29 .ssh
drwx------  3 mcs  other      512 Mar 19 19:28 .sunw
-rw-------  1 mcs  other     3638 Aug  2 22:31 .viminfo
-rw-r--r--  1 mcs  other       39 Jul 31 23:00 .vimrc
-rw-------  1 mcs  other      309 Aug  2 22:43 .Xauthority
drwxr-xr-x  2 mcs  other      512 Mar 18 18:03 Desktop
drwxr-xr-x  2 mcs  other      512 Mar 18 18:03 Documents
-rw-------  1 mcs  other 41872384 Apr  1 19:50 firefox-2.0.0.3.en-US.solaris10-i386-pkg
-rw-r--r--  1 mcs  other      200 Apr  1 22:32 links
-rw-r--r--  1 mcs  other       98 Mar 25 22:19 local.bashrc
-rw-r--r--  1 mcs  other      136 Mar 18 17:51 local.cshrc
-rw-r--r--  1 mcs  other      157 Mar 18 17:51 local.login
-rw-r--r--  1 mcs  other      174 Mar 18 17:51 local.profile
-bash-3.00$ cat ~/.bashrc
PS1="(\t)[\[\033[1;33m\]\u\[\033[0m\]@\[\033[0;32m\]\h\[\033[0m\]:\[\033[1;34m\]\W\[\033[0m\]]\$"

export PS1

set -o vi
-bash-3.00$ . !$
. ~/.bashrc
(22:43:36)[mcs@solaris:~]$

Rename all *.gif files in a directory (prefix)

// My mind always seems to draw a blank when I want to do this sort of thing, so here's a concise little self-reminder

for file in $(echo *.gif); do mv ${file} prefix.${file}; done
« Newer Snippets
Older Snippets »
Showing 31-40 of 130 total