May 2009
19 posts
2 tags
Trim,ltrim & rtrim in #javascript
Simple class to trim,ltrim and rtrim as it’s common in other languages.
Below you will find usage examples.
trim = {
both : function(str,ch) { # Equivalent to trim
return trim.lead(trim.trail(str,ch),ch);
},
lead : function(str,ch) { # Equivalent to ltrim
if (!ch)
ch="\\s"
return str.replace(
new RegExp("^["+ch+"]+", "g"), ""
);
},
trail : function(str,ch) { # Equivalent...
Dump any object in #javascript and read their...
Simple function that makes it possible to read the content of any #js object, converting it to a string.
function dump (arr,level) {
var dumped_text = "";
if(!level) level = 0;
var level_padding = "";
for(var j=0;j \"" + value + "\"\n";
}
}
} else {
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
2 tags
Format #postgres output for scripting use
How to format a pg query output correctly so that it can be used from a shell script
query="SELECT * FROM sometable";
RESULTS=`psql -d table_name -c "$query" -q -F";" -A`;
for row in $RESULTS ;
do
# Put your action on each row here
# you can split it with awk,
# doing something like
first=`echo $row|awk'{split($0,a,";");print a[1]}'`;
# Remember that first row is field description
done;
2 tags
Find & Exec in #bash
How to perform an action on files returned by find command
# I will look for every file having '.log' extension
# and will run 'ls -la' on it for gaining more
# informations
find . -name "*.log" -type f -exec ls -la '{}' \;
1 tag
Find if a var is numeric in #javscript
How to be sure a variable is numeric with #js
function IsNumeric(sText) {
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for ( i = 0; i < sText.length && IsNumber == true; i++) {
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
IsNumber = false;
}
return IsNumber;
}
2 tags
Search which package(s) owns a file in #gentoo
Equery is a wonderfull tool part of the gentoolkit ebuild able to discover which ebuild(s) installed a certain file
# The command
equery belongs /usr/bin/vim
# The output
[ Searching for file(s) /usr/bin/vim in *... ]
app-editors/vim-7.2 (/usr/bin/vim)
1 tag
How to switch 2 $vars value in #Perl
How to simply switch two variables values in Perl.
#!/usr/bin/perl
# Assigning 2 vars
my $some = "some";
my $thing = "thing";
# Switching them
($some, $thing) = ($thing, $some);
# See what happened
print '$some is now "'.$some.'"';
print "\n";
print '$thing is now "'.$thing.'"';
1 tag
Dump variables in Perl
How to show content of unknown objects in perl using Dumper
use Data::Dumper;
print Dumper $my_unknown_object;
2 tags
Find & replace multiple files with Perl
How to search and replace a string within some files with perl, launched from a shell.
# I'm looking for 'some' in all my .js files
# and I want to replace it with 'foo'
perl -pi -w -e 's/some/foo/g;' *.js
2 tags
Adding a line to Crontab from bash script
How to insert a new line to existing crontab using a simple bash script
#!/bin/bash
# Set up the new line you want to insert
$newLine="* */5 * * * do something every 5 hours";
# Copy existing crontab to temp file
crontab -l > /tmp/cron
# Adding desired line at the end of the temp file
echo $newLine >> /tmp/tmpcron
# Installing new crontab
crontab -e /tmp/cron
# Removing temp...
2 tags
Creating a Twitter @reply from Perl
How to update your twitter account using Perl, specifying an ‘in reply to’ parameter.
#! /usr/bin/perl
# Configuration
my $TW_USER = "USERNAME";
my $TW_PASS = "PASSWORD";
my $INREPLYTO = "POST_ID"; # Retrieve this from Twitter
my $MESSAGE = "SOMETHING TO SAY";
# Import library and get an User Agent
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
# Set up the request
my $req = new...
2 tags
Htaccess redirects non-static non-existing files...
This is the fastest way to achieve a good url rewriting in Php, redirecting every non-existing file or directory to the index.php. The following htaccess also make chekcs to exclude automatically images,js,css from being redirected
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^.*\.png [nc]
RewriteCond %{REQUEST_FILENAME} !^.*\.css [nc]
RewriteCond %{REQUEST_FILENAME} !^.*\.jpg...
4 tags
Post to Tumblr via Bash using Curl
Quick example on how to use Tumblr API to update own account from the command line.
There are many other options as it is possible to post links, image etc…just change the type on what you need as described in Tumblr API.
#!/bin/bash
#
# Your Datas
#
TMB_USER='somefoo@somwhere.com';
TMB_PASS='somesecret';
#
# What to Write
#
TMB_TYPE='regular';
TMB_TITLE='The post...
2 tags
Print last position of a splitted string with AWK
I know this is quite easy but I admit I had to look up in google cause I didn’t know awk had lenght function.
#!/bin/bash
STRING="some,thing,is,splitting,here";
echo $STRING | awk '{split($0,a,","); print a[length(a)]}';
1 tag
Retrieve Options like Bash in Php : getopt()
Simply retrieve and use Options passed from command line to php
#!/usr/bin/php
<?php
/*
@ Options without ":" do not accept any argument
@ Options with a single ":" require an argument.
No argument input will cause an error
@ Options with a double "::" accept an argument.
No argument input won't cause any error
*/
$shortopts = "vhr:o::";
/*
Behave of longopts is exactly...
Split string to array in Bash without Awk!
This shows how to split a string into an array in Bash without using Awk or other programming languages.
#!/bin/bash
# Your string
string="one,two,three,four,five,end";
OLD_IFS="$IFS";
# Character used for splitting
IFS=",";
bar=( $string );
IFS="$OLD_IFS";
# Cycle for every splitted part
for (( i=0; i<${#bar[@]}; i++ )); do
# Some echo to understand what you got
echo "["$i"]...
Update Twitter via Wget or Curl
How to update twitter from command line using Wget and Curl.
Variables are just for better understanding, you can use this code writing directly usernames, password and messages.
-O / -o options are needed for downloading everything to /dev/null
-q / -s options are needed for not showing any output
#!/bin/bash
# Setting up User e Pass
TW_USER="twit_username";
TW_PASS="twit_mysecret";
#...
Quick file extension change in Bash
#!/bin/bash
EXTF="todo";
EXTT="done";
for i in *.$EXTF;
do
fname=$(basename $i .$EXTF);
mv -v $i $fname.$EXTT;
done
Easily count files per subdirectory in bash
#!/bin/sh
for file in *
do
if [ -d "$file" ]; then
echo $file ":" `ls $file | wc -l`;
continue
fi
done
exit 0