July 2009
23 posts
1 tag
Max ID from Mysql - Speed tests
I know three methods able to retrieve the max ID (primary key with autoincrement) of a table and I wanted to know which was faster so I run some tests.
SELECT Auto_increment FROM information_schema.tables WHERE table_name="MYDB" AND table_schema="MYTABLE" LIMIT 1;
SELECT MAX(id) From MYDB.MYTABLE LIMIT 1;
SELECT id FROM MYDB.MYTABLE ORDER BY id DESC LIMIT 1
Before giving you the results...
1 tag
Htaccess rewrite : forward query string and add...
This shows a simple solution to make some redirs adding parameters to querystring. I used this to implement an API service.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.php* [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^([^.]+).([^?]+)$ index.php?method=$1&format=$2&%1
Example request:
/create.json?param=something&foo=smelse
Redirects to...
2 tags
Brace expansions examples in bash
A bunch of self-explaining examples using brace
# Just echo
echo {John,Jane,Baby,Precious}
#John Jane Baby Precious
# Combine
echo {John,Jane,Baby,Precious}Doe
#JohnDoe JaneDoe BabyDoe PreciousDoe
# Combine with with spaces
echo {"John ","Jane ","Baby ","Precious "}Doe
#John Doe Jane Doe Baby Doe Precious Doe
# Combine with with spaces and commas
echo {"John ","Jane ","Baby ","Precious...
2 tags
Send a message to all active users on system with...
Wall is a great utility for sysadm which allows them to send a message to all currently opened shells. Here’s a common use :
wall Rebooting server in 5 mins, please save works and get the hell out of here
Any currently connected user will receive a message like
Broadcast message from sysadm (pts/5) (Wed Jul 29 11:50:59 2009):
Rebooting server in 5 mins, please save works and get...
2 tags
Split big files in bash with ... split
Example on how to use split to phisically split large files in smaller ones.
ls -lh bigfile
# This is our initial situation: a large file we want to split
#-rw-r--r-- 1 foo foo 10M Jul 29 11:30 bigfile
# This will do the dirty job
# we're going to split the big file in many little ones, each of 2mb
split -b 2m bigfile part_
# Let's check what we got
ls -lh part_*
# Will output the list of new...
2 tags
Count multiple files lines with wc
Ok this is probably granted, but I didn’t know wc was able to read and count from multiple files at once.
# From standard input
cat *.pl | wc -l
# From list
wc -l file1.pl file2.pl
1 tag
Foreach key => val in Javascript
Simple example on how to obtain a key => val cycle for a Javascript Object
var object = { foo: 1, bar: 2, baz: 3 };
[ alert ( key + "=" + obj[val] ) for ( val in obj ) ];
3 tags
Reinstall all currently installed packages from...
This script, found on ubuntu forums, might help if you rm -rf some important dir and want to repair everything. Obviously it never happened to me.
for pkg in `dpkg --get-selections | awk '{print $1}' | egrep -v '(dpkg|apt|mysql|mythtv)'` ; do apt-get -y --force-yes install --reinstall $pkg ; done
2 tags
2 tags
Folder size in shell using du
Simple way to retrieve a folder size in human readable format using du utility.
du -sh folder_name
3 tags
Delete .svn folders from trunk with find
Quick code to remove all .svn files and folder from a trunk.
find . -name "*svn*" -exec rm -rf '{}' \;
2 tags
Update DynDns host with current IP with Perl
Updates any DynDns host with current IP address. Useful if put in crontab and executed every 15mins.
#!/usr/bin/perl
# Load Needed Modules
use strict;
use XML::Simple;
use LWP::UserAgent;
# Get login data
my $username="DynDns_user";
my $password="DynDns_user";
my $hostname="yourhost.example.com";
# Create a new useragent
my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/5.0 (X11; ; Linux...
4 tags
Create a Zenity progress bar using wget output
I found this simple script to redirect wget progress bar to a nicer zenity window.
Sed is used to parse the output and send to Zenity only the correct progress. An infinite bash cycle is used to check that zenity is still running (if the user closes the window probably you want wget to be killed)
# Start wget | zenity
# Note the & at the end of the pipe, this allows the script to continue...
1 tag
[PHP] How much memory does your app consume?
Reblogged codepuzzling:
Here it is a simple function to display how much memory your PHP script is consuming.
// debug function with time, memory consumption (MB) and optional custom message
public function getMemoryUsage($message="", $echo=1)
{
$mem_used = memory_get_usage(true)/ 1024 / 1024;
$mem_peak = memory_get_peak_usage(true) / 1024 / 1024;
...
1 tag
1 tag
3 tags
Use Zenity calendar to get dates in different...
Quick example on how to open a calendar with Zenity, get the selected date from a Bash script, and change the returned date format (timestamp f.e.).
#!/bin/bash
# Creates a Zenity calendar obj,
# Put the selected date on a variable.
DATE=`$(zenity --calendar --text "When to leave?" --title "Holidays"); echo $szDate`;
# If you want to return the date in a different format you can play with
#...
2 tags
Zenity dialog. Get the answer from Bash.
This shows how to create a Zenity dialog window and how to get the answer given by user into a Bash variable.
#!/bin/bash
# Creates a Zenity question dialog,
# similar to JS confirm().
# Put the answer code on a variable.
ANSWER=$(zenity --entry --text "What's your name?" ); echo $szAnswer
# Creates a Zenity popup (information dialog)
# and put there user previous answer
zenity --info --text...
2 tags
Zenity questions
Quick examples on how to launch Zenity questions from Bash and use the returned values.
#!/bin/bash
# Creates a Zenity question dialog,
# similar to JS confirm().
# Put the answer code on a variable.
ANSWER=`zenity --question --text "Do you like me?"; echo $?`;
# Check what the user pressed
if [[ $ANSWER -eq 1 ]]
then
# 1 => pressed cancel
echo "No, you don't";
else
# 0 => pressed...
2 tags
Cycle all folder's file and get each name in #bash
This is pretty useful if you need to do some operations on certain files in a folder. In the example I’m cycling all files in the apache log folder, selecting only the .log files and then get the basename (no path) of the log.
DIR="/var/log/apache2";
for FILE in ${DIR}/*.log
do
FILE=`basename $FILE`;
echo "Let's do some op on $FILE";
done
1 tag
[JS] How to test if a variable is defined
codepuzzling:
// returns 1 if defined, 0 otherwise.
function isDefined(anObj) {
switch(typeof(anObj)) {
case "string": if(anObj != "") return 1; else return 0;
case "undefined": return 0;
case "object": if(anObj != null) return 1; else return 0;
default: return 0;
}
}
2 tags
Using Sqlite3 in Bash
After my previous post about working with sqlite3 on #perl, here’s an other quick script to explore how to use this simple DB in #bash.
#!/bin/bash
# Defining my databse first table
STRUCTURE="CREATE TABLE data (id INTEGER PRIMARY KEY,name TEXT,value TEXT);";
# Creating an Empty db file and filling it with my structure
cat /dev/null > dbname.db
echo $STRUCTURE >...