Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Friday, June 24, 2011

Convert text to html codes

I was surprised there was no simple script out here to do this conversion. I took the an table of html ascii and wrote this bash script.

I know there are functions to convert text to ascii. I didn't want to go through the hassle of formatting it for HTML.

This script accepts as input the text you want to convert and also a html formatted table of ascii codes.



#!/bin/sh
# $1 input text
# $2 ascii table



to_process=$1
res=""
for ((i=0;i<${#to_process};i++))
{
letter=${to_process:i:1}

if [[ $letter == "." ]]
then
res="${res} &#46;"
else
line_no=`grep -n "<td>$letter</td>" $2 | cut -f 1 -d ':'`
((line_no++))

if [[ ${#line_no} -gt 0 ]]
then

part=`sed -n "${line_no}p" $2 | sed -e 's/<td>//' -e 's/<\/td>//'`
res=${res}${part}

fi

fi
}
echo $res | sed -e 's/amp\;//g' -e 's/ //g'


Here is the table I used:


<table cellspacing="0" border="1" width="100%" class="reference">
<tbody><tr>
<th align="left">ASCII Character</th>
<th align="left">HTML Entity Code</th>
<th align="left">Description</th>
</tr>
<tr>
<td> </td>
<td>&#32;</td>
<td>space</td>
</tr>
<tr>
<td>!</td>
<td>&#33;</td>
<td>exclamation mark</td>
</tr>
<tr>
<td>"</td>
<td>&#34;</td>
<td>quotation mark</td>
</tr>
<tr>
<td>#</td>
<td>&#35;</td>
<td>number sign</td>
</tr>
<tr>
<td>$</td>
<td>&#36;</td>
<td>dollar sign</td>
</tr>
<tr>
<td>%</td>
<td>&#37;</td>
<td>percent sign</td>
</tr>
<tr>
<td>&</td>
<td>&#38;</td>
<td>ampersand</td>
</tr>
<tr>
<td>'</td>
<td>&#39;</td>
<td>apostrophe</td>
</tr>
<tr>
<td>(</td>
<td>&#40;</td>
<td>left parenthesis</td>
</tr>
<tr>
<td>)</td>
<td>&#41;</td>
<td>right parenthesis</td>
</tr>
<tr>
<td>*</td>
<td>&#42;</td>
<td>asterisk</td>
</tr>
<tr>
<td>+</td>
<td>&#43;</td>
<td>plus sign</td>
</tr>
<tr>
<td>,</td>
<td>&#44;</td>
<td>comma</td>
</tr>
<tr>
<td>-</td>
<td>&#45;</td>
<td>hyphen</td>
</tr>
<tr>
<td>.</td>
<td>&#46;</td>
<td>period</td>
</tr>
<tr>
<td>/</td>
<td>&#47;</td>
<td>slash</td>
</tr>
<tr>
<td>0</td>
<td>&#48;</td>
<td>digit 0</td>
</tr>
<tr>
<td>1</td>
<td>&#49;</td>
<td>digit 1</td>
</tr>
<tr>
<td>2</td>
<td>&#50;</td>
<td>digit 2</td>
</tr>
<tr>
<td>3</td>
<td>&#51;</td>
<td>digit 3</td>
</tr>
<tr>
<td>4</td>
<td>&#52;</td>
<td>digit 4</td>
</tr>
<tr>
<td>5</td>
<td>&#53;</td>
<td>digit 5</td>
</tr>
<tr>
<td>6</td>
<td>&#54;</td>
<td>digit 6</td>
</tr>
<tr>
<td>7</td>
<td>&#55;</td>
<td>digit 7</td>
</tr>
<tr>
<td>8</td>
<td>&#56;</td>
<td>digit 8</td>
</tr>
<tr>
<td>9</td>
<td>&#57;</td>
<td>digit 9</td>
</tr>
<tr>
<td>:</td>
<td>&#58;</td>
<td>colon</td>
</tr>
<tr>
<td>;</td>
<td>&#59;</td>
<td>semicolon</td>
</tr>
<tr>
<td><</td>
<td>&#60;</td>
<td>less-than</td>
</tr>
<tr>
<td>=</td>
<td>&#61;</td>
<td>equals-to</td>
</tr>
<tr>
<td>></td>
<td>&#62;</td>
<td>greater-than</td>
</tr>
<tr>
<td>?</td>
<td>&#63;</td>
<td>question mark</td>
</tr>
<tr>
<td>@</td>
<td>&#64;</td>
<td>at sign</td>
</tr>
<tr>
<td>A</td>
<td>&#65;</td>
<td>uppercase A</td>
</tr>
<tr>
<td>B</td>
<td>&#66;</td>
<td>uppercase B</td>
</tr>
<tr>
<td>C</td>
<td>&#67;</td>
<td>uppercase C</td>
</tr>
<tr>
<td>D</td>
<td>&#68;</td>
<td>uppercase D</td>
</tr>
<tr>
<td>E</td>
<td>&#69;</td>
<td>uppercase E</td>
</tr>
<tr>
<td>F</td>
<td>&#70;</td>
<td>uppercase F</td>
</tr>
<tr>
<td>G</td>
<td>&#71;</td>
<td>uppercase G</td>
</tr>
<tr>
<td>H</td>
<td>&#72;</td>
<td>uppercase H</td>
</tr>
<tr>
<td>I</td>
<td>&#73;</td>
<td>uppercase I</td>
</tr>
<tr>
<td>J</td>
<td>&#74;</td>
<td>uppercase J</td>
</tr>
<tr>
<td>K</td>
<td>&#75;</td>
<td>uppercase K</td>
</tr>
<tr>
<td>L</td>
<td>&#76;</td>
<td>uppercase L</td>
</tr>
<tr>
<td>M</td>
<td>&#77;</td>
<td>uppercase M</td>
</tr>
<tr>
<td>N</td>
<td>&#78;</td>
<td>uppercase N</td>
</tr>
<tr>
<td>O</td>
<td>&#79;</td>
<td>uppercase O</td>
</tr>
<tr>
<td>P</td>
<td>&#80;</td>
<td>uppercase P</td>
</tr>
<tr>
<td>Q</td>
<td>&#81;</td>
<td>uppercase Q</td>
</tr>
<tr>
<td>R</td>
<td>&#82;</td>
<td>uppercase R</td>
</tr>
<tr>
<td>S</td>
<td>&#83;</td>
<td>uppercase S</td>
</tr>
<tr>
<td>T</td>
<td>&#84;</td>
<td>uppercase T</td>
</tr>
<tr>
<td>U</td>
<td>&#85;</td>
<td>uppercase U</td>
</tr>
<tr>
<td>V</td>
<td>&#86;</td>
<td>uppercase V</td>
</tr>
<tr>
<td>W</td>
<td>&#87;</td>
<td>uppercase W</td>
</tr>
<tr>
<td>X</td>
<td>&#88;</td>
<td>uppercase X</td>
</tr>
<tr>
<td>Y</td>
<td>&#89;</td>
<td>uppercase Y</td>
</tr>
<tr>
<td>Z</td>
<td>&#90;</td>
<td>uppercase Z</td>
</tr>
<tr>
<td>[</td>
<td>&#91;</td>
<td>left square bracket</td>
</tr>
<tr>
<td>\</td>
<td>&#92;</td>
<td>backslash</td>
</tr>
<tr>
<td>]</td>
<td>&#93;</td>
<td>right square bracket</td>
</tr>
<tr>
<td>^</td>
<td>&#94;</td>
<td>caret</td>
</tr>
<tr>
<td>_</td>
<td>&#95;</td>
<td>underscore</td>
</tr>
<tr>
<td>`</td>
<td>&#96;</td>
<td>grave accent</td>
</tr>
<tr>
<td>a</td>
<td>&#97;</td>
<td>lowercase a</td>
</tr>
<tr>
<td>b</td>
<td>&#98;</td>
<td>lowercase b</td>
</tr>
<tr>
<td>c</td>
<td>&#99;</td>
<td>lowercase c</td>
</tr>
<tr>
<td>d</td>
<td>&#100;</td>
<td>lowercase d</td>
</tr>
<tr>
<td>e</td>
<td>&#101;</td>
<td>lowercase e</td>
</tr>
<tr>
<td>f</td>
<td>&#102;</td>
<td>lowercase f</td>
</tr>
<tr>
<td>g</td>
<td>&#103;</td>
<td>lowercase g</td>
</tr>
<tr>
<td>h</td>
<td>&#104;</td>
<td>lowercase h</td>
</tr>
<tr>
<td>i</td>
<td>&#105;</td>
<td>lowercase i</td>
</tr>
<tr>
<td>j</td>
<td>&#106;</td>
<td>lowercase j</td>
</tr>
<tr>
<td>k</td>
<td>&#107;</td>
<td>lowercase k</td>
</tr>
<tr>
<td>l</td>
<td>&#108;</td>
<td>lowercase l</td>
</tr>
<tr>
<td>m</td>
<td>&#109;</td>
<td>lowercase m</td>
</tr>
<tr>
<td>n</td>
<td>&#110;</td>
<td>lowercase n</td>
</tr>
<tr>
<td>o</td>
<td>&#111;</td>
<td>lowercase o</td>
</tr>
<tr>
<td>p</td>
<td>&#112;</td>
<td>lowercase p</td>
</tr>
<tr>
<td>q</td>
<td>&#113;</td>
<td>lowercase q</td>
</tr>
<tr>
<td>r</td>
<td>&#114;</td>
<td>lowercase r</td>
</tr>
<tr>
<td>s</td>
<td>&#115;</td>
<td>lowercase s</td>
</tr>
<tr>
<td>t</td>
<td>&#116;</td>
<td>lowercase t</td>
</tr>
<tr>
<td>u</td>
<td>&#117;</td>
<td>lowercase u</td>
</tr>
<tr>
<td>v</td>
<td>&#118;</td>
<td>lowercase v</td>
</tr>
<tr>
<td>w</td>
<td>&#119;</td>
<td>lowercase w</td>
</tr>
<tr>
<td>x</td>
<td>&#120;</td>
<td>lowercase x</td>
</tr>
<tr>
<td>y</td>
<td>&#121;</td>
<td>lowercase y</td>
</tr>
<tr>
<td>z</td>
<td>&#122;</td>
<td>lowercase z</td>
</tr>
<tr>
<td>{</td>
<td>&#123;</td>
<td>left curly brace</td>
</tr>
<tr>
<td>|</td>
<td>&#124;</td>
<td>vertical bar</td>
</tr>
<tr>
<td>}</td>
<td>&#125;</td>
<td>right curly brace</td>
</tr>
<tr>
<td>~</td>
<td>&#126;</td>
<td>tilde</td>
</tr>
</tbody></table>

Friday, May 14, 2010

Tool to manage power for ppc Linux

ACPI is n/a for ppc. Same for apm. Those don't work with the ppc hardware. Pbbuttonsd controls sleep and hibernate. And for PPC Linux, you must have an ATI graphics card for sleep to work. Nvidia and anything else will not work.



$ sudo pbbcmd hibernate

or

$ sudo pbbcmd sleep

or

$ sudo pbbuttonsd sleep

or

$ sudo pbbuttonsd hibernate

Tuesday, March 30, 2010

Linux - wireless commands

wireless commands

iwlist wlan0 scan
wevent &
iwconfig wlan0 key open xxxxx
iwconfig wlan0 essid xxxx

Wednesday, March 17, 2010

Set program output to environment variable in batch file

FOR /F "tokens=*" %A IN ('prog.exe') DO SET ENVVAR=%A

gcc, gdb and make commands

Macro definition
gcc -D DUMMY_DEF -c test_str.c

Makefile arguments
test:
./program $(PROG_ARGS)

make test PROG_ARGS='testfile1.txt'

gdb commands
run args - runs program
frame - examines current frame
up - examines previous frame

Wednesday, February 24, 2010

Mount smb from command line

mount -t smbfs //username@ip/share mountpt

Sunday, January 24, 2010

Mortgage calculator - bash script

This simply outputs the monthly mortgage payment.

Takes five arguments : principle years interest points fees

For instance,

bash mortgage.sh 100000 5 .075 0 0

Outputs

Principle + fees = 100000 , Years = 5 , Months = 60 , interest .006250000000
Points = 0 , 0
2003.794859563881




#!/bin/sh

if [[ $# -ne 5 ]]
then
echo "enter 5 args : principle years interest points fees"
exit
fi

prin=$1
((mon=$2 * 12 ))
inter=`echo "scale=12;$3/12" | bc`
pts=`echo "scale=12; $prin * $4" | bc`
fee=$5
prin=`echo "scale=12;$prin + $fee" | bc`


echo "Principle + fees = $prin , Years = $2 , Months = $mon , interest $inter"
echo "Points = $4 , $pts"

prin=`echo "scale=12; $prin - $pts" | bc`


echo `echo "scale=12;($prin*$inter)*((1+$inter)^$mon)/(((1+$inter)^$mon)-1)"|bc`
echo "the monthly payment : $new_prin"



Monday, January 11, 2010

Bash - Gaussian Elimination Op count example



#!/bin/sh

if [[ $# -ne 2 ]]
then
exit
fi

echo " m $1 by n $2 matrix"

n=$2
m=$1
mult=0
add=0
c=0

for ((i=1;i < m;i++))
do
echo "row = i = $i"
for ((j=i;j < m;j++))
do
((mult=mult + (n-i+1)))
((add=add + (n-i+1)))
done
((c=c+(m-i)*(n-i+1)))
done
echo "additions = $add"
echo "mults = $mult"
echo "summation = $c"



Saturday, September 5, 2009

Removing unwanted characters from a text file with grep

# This worked pretty good when I was getting strange characters from uploading a google text file to my linux box
#I couldn't import into mysql
#So I did this:

cat $1 | grep -o "[a-zA-Z,;()0-9_ ][a-zA-Z,;()0-9_ *@'.]*" > output.sql

#this command greps for only letters,commas, numbers, underscores, parenthesis, semicolons, astericks, and at signs.

Monday, August 31, 2009

Linux mail basics

# Start interactive mail
mail

# Print current message
p

# Delete current message
d

#Exit mail program
q

Friday, August 28, 2009

Leopard user template

sudo mv /System/Library/User\ Template/English.lproj /System/Library/User\ Template/English.lproj.old

This will give you copy of the original called "English.lproj.old" in the same folder. Then you may copy your new template file by executing:

sudo cp -R /Users/new-user-short-name /System/Library/User\ Template/English.lproj

If you prefer to remove (delete) the original try the command without the /* at the end: sudo rm -r /System/Library/User\ Template/English.lproj

Get mac address from xml and generate computer names for each mac

Parsing xml is pretty easy using grep. The benefit of sed/grep is that they are right there -- if you have cygwin, Linux or Mac.

Here is an example format:



<macs>

<string>00-00-00-00-00-01</string>
<string>00-00-00-00-00-02</string>
</macs>


This will parse the xml file formatted like above and generate a computer name for each mac address. A good application of sed/grep/bash. If you are installing a lot of new computers and need to associate new names with mac addresses, this is a good start.


grep All\ Computers.plist | sed -e 's/<\/*string>//g' | while read line; do ((i++)); echo "pc-$i $line"; done > macs.txt

Friday, August 21, 2009

Set the default printer from the registry

rem Using reg.exe you can set the default printer

@echo off

rem Here we are doing a split of the hostname on the hypen, so room1-01, becomes room1

for /F "tokens=1 delims=-" %%a in ('hostname') do set name=%%a


if %name% == room1 ( reg add "hkcu\software\microsoft\windows nt\currentversion\windows" /t REG_SZ /d "ROOM1PRINTER,winspool,NE04:" /v "Device" /f
)

Thursday, August 20, 2009

Installing a folder tree of fonts on XP

rem XP doesn't let you select a folder to install fonts from; it will not recurse to find fonts within a folder unless they are all under one folder

rem This command will go through all folders in the current directory and copy the files to all_fonts

rem The /y for copy will force an overwrite if a duplicate file shows up

for /F "tokens=1 delims=," %a in ('dir /s /b') do copy /y "%a" all_fonts

Tuesday, August 4, 2009

mysql -- attaching to a host

#This is a way checking your connection to a mysql db server

mysql --user=username -h hostname -p

Wednesday, June 24, 2009

Replacing folders on a network with psexec and xcopy

rem We can run a process remotely with psexec
rem
rem cmd /C allows us to run a process in a cmd window and it automatically terminates

for /l %a in (1,1,50) do psexec \\station-%a cmd /C "move c:\path\to\folder c:\path\to\backup"

rem Now lets replace that folder with a new one using xcopy
rem
rem We use our local copy of the folder c:\path\to\folder and copy it to each station
rem
rem /S /E /H /I switches for xcopy ensure that we get hidden files, sub directories, and empty directories. /I assumes that the destination is a folder.

for /l %a in (1,1,50) do start cmd /k "xcopy /S /E /H /I c:\path\to\folder \\station-%a\c$\path\to\folder"

Wednesday, June 17, 2009

Get mac serial no from terminal

system_profiler SPHardwareDataType | grep "Serial Number" | sed 's/Serial Number://'

Friday, June 12, 2009

Silent install of msp patch

msiexec /p mspfile.msp /qn

Thursday, June 11, 2009

Checking versions if Office 2004 is at 11.5.5 and Acrobat is at 9.1.2

This is a trick to check version of some software on a group of computers


Here we're checking that Office 2004 11.5.5 update is installed


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; else ssh station-${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; fi; done

Here we're checking that Acrobat reader and acrobat are at 9.1.2


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local "system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; else ssh station-${i}.local " system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; fi; done

Tuesday, June 9, 2009

How to start another X session

You already have X running and want to run another one.

1. Login into another virtual console
2. Open the terminal
3. startx -- :1