2006/04/09

The SED beauty

Sed is one of the unix beauties. Simple, standard, absolutely
text/driven, and overall, powerful as The Brain. Look at the examples.

Simple replace:

# sed -e 's/en_US/es_ES/g' /etc/sysconfig/i18n

Replace only on determined lines:

# sed -e '/disable/s/yes/no/' /etc/xinetd.d/pop3s

Adding after a line:

# sed -e '/ListenAddress ::/a\AllowUsers root' /etc/ssh/sshd_config

Deleting a line:

# sed -e '/only_from/d' /etc/xinetd.d/swat

Making a backup, just with "-i"

# sed -i.bak -e '/\/AutoPPP/s/\#//' /etc/mgetty+sendfax/login.config

Replace spaces on start lines:

# sed 's/^ *//' file

More than 2 spaces with one:

# sed 's/[ ]\{2,\}/ /g' file

This is my precious, my ring: Once we have to replace this text...

old: {pdf=FILE text=TEXT}
new: {pdf=FILE title=FILE text=TEXT}

... on *THOUSANDS* of "page.txt" files in a BIIIG directory tree! Of
course, "FILE" and "TEXT" were different on each. With a little help
from my friends Sed and Find, took just a minute:

# find . -name page.txt -print -exec \
> sed -i -e \
> "s/{pdf=\(.*\?\) text=\(.*\?\)}/{pdf=\1 title=\1 text=\2}/g" "{}" \;

Note: ".*" is a GREEDY expression (RT regex FM). To avoid GREEDINESS,
you should use ".*?"

Beautiful.

2006/03/11

Running Tomcat!

How to install TOMCAT on Fedora:

a) Tomcat Itself
yum install tomcat5
yum install tomcat5-webapps
yum install tomcat5-admin-webapps

a2) install java from SUN (could be using Fedora Frog)

b) Change on /etc/tomcat5/tomcat5.conf

JAVA_HOME="/opt/jre1.5.0_06"
(or which suits)

c) In /etc/tomcat5/tomcat-users.xml, put manager/admin roles to tomcat
user:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="manager,admin"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>

d) Start service:

# service tomcat5 start

e) Browse http://localhost:8080/

;)

2006/02/22

Disabling Beagle

Very simple. But you must do it every time it updates.

# sed -i -e "/ENABLED/s/yes/no/g" /etc/beagle/crawl-applications
# sed -i -e "/ENABLED/s/yes/no/g" /etc/beagle/crawl-documentation

May The Source Be With You...

2006/01/18

Linux/PHP with Windows/MSSQLServer Howto

Made on Fedora Core 6. Suppose other distros work the same. Good luck!

- Install freetds, the unixODBC-kde gui and php-odbc, for making php work with odbc

[rodolfoap] /root # yum install freetds unixODBC-kde php-odbc

- Connectivity test, port 1433TCP. If you get "1>", ok. Elsewhere, its a firewall, ports, routing, etc. issue.

[rodolfoap] /root # tsql -S 192.168.1.20 -U sa -P sjasdad
locale is "en_US.UTF-8"
locale charset is "UTF-8"
1>

- Create an entry in /etc/freetds.conf:
[192.168.1.20]
host = 192.168.1.20
port = 1433
tds version = 8.0

- Add the entry in /etc/odbcinst.ini
[MSSQLServer]
Description = MSSQLServer
Driver = MSSQLServer
Servername = 192.168.1.20
Database = Bienes
UID = sa
PWD = sjasdad
Port = 1433

- And /etc/odbcinst.ini
[MSSQLServer]
Description =
Driver = /usr/lib/libtdsodbc.so.0
Driver64 =
Setup = /usr/lib/libtdsS.so.1
Setup64 =
UsageCount = 1
CPTimeout =
CPReuse =

- Now, you should get "SQL>"
[rodolfoap] /root # isql -v MSSQLServer sa sjasdad
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
(There you can perform SQL queries)

- The odbc php test page (for my database, of course):
[rodolfoap] /root # cat /var/www/html/odbc.php

<?
$connect = odbc_connect("MSSQLServer", "sa", "sjasdad");
odbc_exec($connect, "use Bienes");
$result = odbc_exec($connect, "SELECT * FROM Personas");
while(odbc_fetch_row($result)){
print(odbc_result($result, "CodPersona").' '.odbc_result($result, "Apellidos") . "<br>\n");
}
odbc_free_result($result);
odbc_close($connect);
?>

- If you want to use mssql functions, install php-mssql from http://remi.collet.free.fr/ , adding the repository and issueing "yum install php-mssq". And then,

[rodolfoap] /root # cat /var/www/html/mssql.php
<?
$connect = mssql_connect("192.168.1.20", "sa", "sjasdad");
mssql_select_db("Bienes", $connect);
$result = mssql_query("SELECT * FROM Personas");
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
while($row = mssql_fetch_array($result))
echo $row["IdPersona"] . " - " . $row["Apellidos"];
mssql_free_result($result);
mssql_close($connect);
?>

Yastá!