Tuesday, October 30, 2007

Html examples

How to put code into blog?
Use <PRE> tag. Sample here:

myClassPath=".;"
for i in `ls ./lib/*`; do
case $environment in
FIX) myClassPath="$myClassPath"":$i"
;;
*) myClassPath="$myClassPath"";$i"
;;
esac
done

Table examples

<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="60%" align="left" valign="top">TestMe</td>
<td width="40%" align="right" valign="top">&#160;</td>
</tr>
<tr colspan="2">
<td>COLSPAN</td>
</tr>
</table>


Scroll Box Sample

<div style="border: 1px solid rgb(170, 170, 170); overflow: auto; width: 350px; height: 100px; color: rgb(0, 0, 0);"><p>
your html code here.
</p>
</div>

Monday, October 29, 2007

Cygwin examples

Cygwin SSHD errors with RSA hostkey been changed:
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed. Click here for details.

Install cygwin cron
Get cron package from Admin group. Download cron from http://mirror.mcs.anl.gov and store it to your local drive. And then run /usr/bin/cron-config to setup cron daemon. Click here for details.


crontab -a filename Install filename as your crontab file.
On many systems, this command is
executed simply as crontab filename
(i.e., without the -a option).
crontab -e Edit your crontab file, or create one if it
doesn't already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file.
(This option is available on only a few systems.)
crontab -u user Used in conjunction with other options, this
option allows you to modify or view the crontab
file of user. When available, only administrators
can use this option.




Cygwin path to Windows path
$ cygpath -wp /home/oneuser/testdir
c:\cygwin\home\oneuser\testdir

Executing Java Program from Cygwin (bash shell script)
$ java -classpath `cygpath -wp $CLASSPATH` [classname] [arguments]

Uninstall Cygwin
source
# Restart your computer.
# Backup anything you may added to the c:\cygwin\ directory which will
not be replaced by the re-installation.
# Delete the c:/cygwin subdirectory (where Cygwin was installed).
# Remove registry entries: Any time you change the registry, do so with
great care..
* Under HKEY_CURRENT_USER, Software, delete "Cygnus Solutions"
* Under HKEY_LOCAL_MACHINE, Software, delete "Cygnus Solutions"
# Use Start, Run, cmd, chkdsk /F. This will run chkdsk, check and repair
any file system problems on the next bootup.
# Restart your computer.





Environment Examples

Table of Contents:

Cygwin examples
  • Cygwin SSHD errors with RSA hostkey been changed
  • Install cygwin cron
  • Cygwin path to Windows path
  • Executing Java Program from Cygwin (bash shell script)
  • Uninstall Cygwin
  • How to find IP Address

WinXP examples

Debian Linux examples

bash examples

Calulation in bash
bash$
bash$ echo "1.5 * 2" | bc -l
3.0
bash$ echo "1/3" | bc -l
.33333333333333333333
bash$
bash$ echo "scale=3; 10/3" | bc
3.333


Change Prompt
$
$ PS1="myPrompt> "
myPrompt>
myPrompt>

Get Create Table command from MySQL
$ sql "show create table my_table\G" | sed -e '1,2d;s/Create Table: //'
Note: sed -e '1,2d' removes the first 2 lines.

if statement examples

Use Regular Expression in if in bash script
if echo "4567" | grep -q "^4"; then
echo "4567 is started with a 4."
elif echo "4567" | grep -q "^A"; then
echo "4567 is started with a A."
else
echo "4567 is NOT started with a 4 or A."
fi

Float/Real Number Comparison in if statement
if [ $( echo "5 > 10" | bc ) -eq 1 ]; then
echo 5 is greater than 10.
else
echo 5 is NOT greater than 10.
fi

=> 5 is NOT greater than 10.



case statement examples
case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac

while statement examples
C-style while loop:
#!/bin/bash
# wh-loopc.sh: Count to 10 in a "while" loop.
LIMIT=10
a=1
while [ "$a" -le $LIMIT ]
do
echo -n "$a "
let "a+=1"
done # No surprises, so far.
echo; echo
# +=================================================================+

# Now, repeat with C-like syntax.

# Double parentheses permit space when setting a variable, as in C.
((a = 1)) # a=1

# Double parentheses, and no "$" preceding variables.
while (( a <= LIMIT ))
do
echo -n "$a "
((a += 1))
# Double parentheses permit incrementing a variable with C-like syntax.
done
exit 0

statement examples

Add new examples here....

mysql examples

Create a temporary table:
CREATE TABLE TMP_TEST SELECT * FROM MY_TEST;

Populate a temporary table:
insert into sms_dailyrsi select * from tmp_dailyrsi;

MINUS alternative:
select *
from tmp_test left join my_test
on tmp_test.pk1 = my_test.pk1 and tmp_test.pk2 = my_test.pk2
where my_test.pk1 is NULL and my_test.pk2 is NULL;

sed examples

Get the last node from a path:
$ pwd
/cygdrive/c/cygwin/var/log

$ pwd | sed -e "s/.*\///g"
log

Display file without the blank lines and remove the first line:
$ cat bearish.htm | sed -e '/^ *$/d;1,1d'

Remove first 3 lines in a file:
$cat bearish.htm | sed -e '1,3d'

Remove HTML tags:
$cat bearish.htm | sed -e 's/<[^>]*>//g'

Unix Examples: bash/awk/sed/mysql

Unix Examples:

awk examples

Remove first 3 lines in a file:
$cat bearish.htm | awk 'NR>3'

Rounding Number:
Need to be able to round numbers in bash to the nearest whole number? You can use a single line of code in AWK to do the job. For example, the following code will make the number 29.498398 round up to 29.50:

$ echo 29.498398 | awk '{ rounded = sprintf("%.2f", $1); print rounded }'
29.50

Friday, October 26, 2007

Applet Examples

Table of Contents

Regular Expression Tips

JavaScript Tester: http://codespec.blogspot.com/2007/10/regular-expression-tips.html

foob.*r
: matchs strings like 'foobar', 'foobalkjdflkj9r' and 'foobr'
foob.+r : matchs strings like 'foobar', 'foobalkjdflkj9r' but not 'foobr'
foob.?r : matchs strings like 'foobar', 'foobbr' and 'foobr' but not 'foobalkj9r'
fooba{2}r : matchs the string 'foobaar'
fooba{2,}r : matchs strings like 'foobaar', 'foobaaar', 'foobaaaar' etc.
fooba{2,3}r : matchs strings like 'foobaar', or 'foobaaar' but not 'foobaaaar'

Metacharacters \1 through \9 are interpreted as backreferences. \ matches previously matched subexpression #.

Examples:

(.)\1+ matchs 'aaaa' and 'cc'.
(.+)\1+ also match 'abab' and '123123'
(['"]?)(\d+)\1 matchs '"13" (in double quotes), or '4' (in single quotes) or 77 (without quotes) etc

Metacharacters - line separators

^ start of line
$ end of line
\A start of text
\Z end of text
. any character in line

Examples:

^foobar matchs string 'foobar' only if it's at the beginning of line
foobar$ matchs string 'foobar' only if it's at the end of line
\^foobar$ matchs string 'foobar' only if it's the only string in line
\foob.r matchs strings like 'foobar', 'foobbr', 'foob1r' and so on


Metacharacters - predefined classes

\w an alphanumeric character (including "_")
\W a nonalphanumeric
\d a numeric character
\D a non-numeric
\s any space (same as [ \t\n\r\f])
\S a non space

You may use \w, \d and \s within custom character classes .

Examples:

\foob\dr matchs strings like 'foob1r', ''foob6r' and so on but not 'foobar', 'foobbr' and so on
\foob[\w\s]r matchs strings like 'foobar', 'foob r', 'foobbr' and so on but not 'foob1r', 'foob=r' and so on

Welcome to CodeSpec.

Here is where I'm going to build code examples so that it can be easily copied and pasted.

Table of Contents

Software Tools - How To:
  • Backup DVDs

Programming languages:

JavaScript Examples

Table of Contents

JavaScript RegExp Tester

Try this out:
http://www.regular-expressions.info/javascriptexample.html

Start Posting

Try it out......