June 2009
15 posts
2 tags
String to upper or lower in #Bash
This shows how to convert a string to lower or upper using bash and tr utility.
STRING="myFAncyString";
# String to lower
echo $STRING | tr "[:upper:]" "[:lower:]";
# String to upper
echo $STRING | tr "[:lower:]" "[:upper:]";
Via spikesource
2 tags
Urlencode a string in #perl
Quick script able to urlencode a string without the needing of importing modules
$mystring = "String to encode";
$mystring =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
print $mystring;
# Returns String%20to%20encode
3 tags
Check if variable is an array in #perl
This is quite useful when parsing xml code and you don’t know if an object contains an array of tags or a single value.
eval {
$#{$isthisanarray}
};
if ($@){
print "Nuuu! It's not";
} else {
print "Yes it is";
}
3 tags
Get your system load
How to get the system load only using uptime ouptut, filtered using awk.
uptime | awk -F'load average:' '{ print "Load: "$2 }'
On twitter @markkolich suggested me a quicker way to obtain the same result
cat /proc/loadavg
3 tags
memtop: order top by %Mem using expect
Top is one of the commands I use the most while working on linux servers. It’s terribly useful and configurable, got a clean interface and is simple to modify its output using shortcuts.
A lot of times I need to order top by memory percentage instead of cpu percentage. This can be easily achieved by pressing ‘M’ while looking at top.
As I’m really (terribly) lazy and I...
2 tags
How to check if a string contains an other in...
Quick trick using #grep in #bash for checking if a string is contained in an other one
STRING="someUnknownString";
NAIL="know";
res=`echo $STRING | grep $NAIL`;
if [[ ${#res} > 0 ]];
then
echo "Yes I found it.";
else
echo "No way, there isn't."
fi
If you want you can make the whole thing case insensitive just using grep -i
STRING="someUnknownString";
NAIL="Unkn";
res=`echo $STRING | grep...
How to export to an array a MySQL query with 3...
codepuzzling:
By reading a good contribution on PHP site, I discovered this method for exporting to an array a query submitted to MySQL:
$result = mysql_query("SELECT * FROM table"); for($i = 0; $array[$i] = mysql_fetch_assoc($result); $i++) ; array_pop($array); // removes last empty array
It produces:
Array ( [0] => Array ( [id] => 1 [user] => myuser ...
tumblrMap - generate xml sitemaps for your tumblr...
Just a quick post to let every tumblr friend know that I created a simple web app which creates xml sitemaps for tumblr blog and auto-submit them too google. This should improve any tumblr blog seo a lot!
Take a look!
http://tumblrmap.net/
2 tags
Perl and Sqlite3 quick start example guide
1. Set up your sqlite database file
sqlite3 demo.db
SQLite version 3.6.13
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
create table datas (
id integer,
name text,
date text
);
2. Execute simple operations with perl
#!/usr/bin/perl
# Import needed Modules
use DBI;
use strict;
# Connection to DB file created before
my $dbh =...
3 tags
Optimize Tumblr title and meta tags #SEO
Simple way to improve SEO in tumblr blogs, by postponing general blog title to article titles in single post views, using the same title as meta description and putting tags as keywords.
<meta name="Description" CONTENT="{block:PostSummary}{PostSummary} - {/block:PostSummary}{Title}" />
<meta name="Keywords" content="{block:Tags}{Tag},{/block:Tags}"...
3 tags
Bash and Expect script to change user password
As passwd does not support —stdin in my linux system, I needed a different way to set an user password from a bash script. This seemed a good alternative to me.
Please note that expect might not be installed on your system. Be sure to install it before running the script. Note that using export HOSTIGNORE the passwd prompted won’t be stored in bash history...
2 tags
Relocate (switch) a svn repo without losing...
Simply relocate a subversion repository to a new url, without losing your local changes.
For more info do a svn help switch
svn switch --relocate svn://my.previous-location.com \
svn://my.new-location.com
2 tags
Create html for imgs in a folder using filename as...
This scripts generates the html code for all the images in a folder. It will use the filename, without extension and with some mods, as Alt attribute for the image.
#!/bin/bash
PATH_WWW="/img";
EXT="jpg"
for i in *.$EXT; do
fname=$(basename $i .jpg);
fname=${fname/_/ };
fname=${fname/-/ };
fname=${fname/./ };
echo "<img src="$PATH_WWW/$i" alt="$fname"...
2 tags
[PHP] How to retrive HTTP headers
codepuzzling:
Anyway response header returned by apache_response_headers() consists only of server signature in my case, but in firebug I can see a lot more of them.
$var = "REQUEST";
foreach (apache_request_headers() as $name => $value) {
$var.= "$name: $value ";
}
$var.= "RESPONSE";
foreach (apache_response_headers() as $name => $value) {
$var.= "$name: $value ";
}
echo...
3 tags
Encrypt & Decrypt strings in #Perl using Tea
Simple two way encrypt/decrypt script in Perl using Tea Algorithm. Light and fast as hell
#!/usr/bin/perl
use Crypt::Tea;
my $string = 'tobecrypted';
my $key = 'somekey';
# Print Original String
print $string."\n";
# Encrypt the string using my key
$user = encrypt($string,$key);
# Print Encrypted String
print $string."\n";
# Decrypt the string
$user = decrypt($string,$key);
# Print back...