Unix: print in color

This simple procedure takes 3 arguments as the parameters:

  • Text
  • Foreground color
  • Background color

It prints the text to the terminal in selected colors.

[cc lang=”bash”]
function print_color {
local text=$1
local fg=$2
local bg=$3
case “$fg” in
red) fg=”31m” ;;
green) fg=”32m” ;;
yellow) fg=”33m” ;;
blue) fg=”34m” ;;
white) fg=”37m” ;;
black) fg=”30m” ;;
*) fg=”37m” ;;
case “$bg” in
red) bg=”41m” ;;
green) bg=”42m” ;;
yellow) bg=”43m” ;;
blue) bg=”44m” ;;
white) bg=”47m” ;;
black) bg=”40m” ;;
*) bg=”40m” ;;
echo -en “\033[${fg}\033[${bg}${text}\033[0m”

Unix: rename files to lowercase

It’s very simple to rename the files to lowercase/uppercase using awk:

[cc lang=”bash”]
ls -1rt | awk ‘{ printf(“mv %s %s\n”, $0, tolower($0)) | “sh” } END { close(“sh”) }’

[cc lang=”bash”]
ls -1rt | awk ‘{ printf(“mv %s %s\n”, $0, toupper($0)) | “sh” } END { close(“sh”) }’

Oracle: quotes and quote operator

There are several methods to put the quote into the string.

The first (and very traditional) one: use 2 quotes.
[cc lang=”oracle8″]
select ‘This ” is quote’ from dual;
If there are more than one quote, it’s difficult to read and write such strings

The second: use chr(39)
[cc lang=”oracle8″]
select ‘This ‘ || chr(39) || ‘ is quote’ from dual;

The third: put the quote into the variable
[cc lang=”oracle8″]
s_quote varchar2(1) := ”” ;
dbms_output.put_line(‘This ‘ || s_quote || ‘ is quote’ ) ;

And, finally, Oracle 10g has added new feature: quote operator.
[cc lang=”oracle8″]
select q'[This ‘ is quote]’ from dual ;

The general form is q’X string X’. Here X is just some character. If the brackets are used, Oracle expects the closing bracket for the end of the string.

Here are the additional examples:
[cc lang=”oracle8″]
select q'(This ‘ is quote)’ from dual ;
select q’|This ‘ is quote|’ from dual ;
select q’#This ‘ is quote#’ from dual ;
select q’#This ‘ is quote#’ from dual ;
select q’?This ‘ is quote?’ from dual ;
select q’TThis ‘ is quoteT’ from dual ;