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 1-4 of 4 total  RSS 

Morse decode using sed

This is a short Morse code decoder written as a shellscript using sed.

The Morse coded text should be in $text, and should be written with spaces between the letters.

echo $text\  | tr . 0 | sed -e {s/0----\ /1/g} -e {s/00---\ /2/g} -e {s/000--\ /3/g} -e {s/000-\ /4/g} -e {s/00000\ /5/g} -e {s/-0000\ /6/g} -e {s/--000\ /7/g} -e {s/---00\ /8/g} -e {s/----0\ /9/g} -e {s/-----\ /0/g} \
	| sed -e {s/-0-0\ /c/g} -e {s/-000\ /b/g} -e {s/00-0\ /f/g} -e {s/0000\ /h/g} -e {s/0---\ /j/g} -e {s/0-00\ /l/g} -e {s/0--0\ /p/g} -e {s/--0-\ /q/g} -e {s/000-\ /v/g} -e {s/-00-\ /x/g} -e {s/-0--\ /y/g} -e {s/--00\ /z/g} \
	| sed -e {s/0--\ /w/g} -e {s/-00\ /d/g} -e {s/--0\ /g/g} -e {s/-0-\ /k/g} -e {s/---\ /o/g} -e {s/0-0\ /r/g} -e {s/000\ /s/g} -e {s/00-\ /u/g} \
	| sed -e {s/0-\ /a/g} -e {s/00\ /i/g} -e {s/--\ /m/g} -e {s/-0\ /n/g} \
	| sed -e {s/0\ /e/g} -e {s/-\ /t/g}

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

Download coordinate from maproom

// description of your code here

IFS="
"
for C in Africa Antarctica Asia Australasia Europe North_America South_America
do
	mkdir -p GIS/$C
	wget -O jeter.html -q "http://www.maproom.psu.edu/cgi-bin/dcw/dcwarea.cgi?$C"
	for P in `egrep '^<OPTION' jeter.html | cut -d '>' -f2`
	do
		curl -d "area=$C" -d "country=$P" -o jeter2.html "http://www.maproom.psu.edu/cgi-bin/dcw/dcwcountry0.cgi"
		P2=`grep FORM jeter2.html | grep point10 | tr "<>" "\n\n" | grep count| cut -d ' ' -f4 | cut -d '=' -f2`
		#curl -d "country=$P2" -o jeter3.txt "http://www.maproom.psu.edu/cgi-bin/dcw/point10.cgi"
		wget -O jeter2.html "http://www.maproom.psu.edu/cgi-bin/dcw/point10.cgi?country=$P2"
		P3=`cat  jeter2.html | tr " >" "\n\n" | grep ftp`
		echo "##### $P2 $P3"	
		wget -O jeter3.txt "$P3"
		mv jeter3.txt > GIS/$C/${P2}.js
		sleep 3
	done
done

Quickly add your public key to an authorized keys file

This will add your public ssh key to an authorized keys file on a remote server for passwordless login.

Change id_dsa.pub to wherever you stored your public key from ssh-keygen.

ssh user@host 'mkdir ~/.ssh;echo '`cat ~/.ssh/id_dsa.pub`' >> ~/.ssh/authorized_keys'
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS