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” ;;
esac
case “$bg” in
red) bg=”41m” ;;
green) bg=”42m” ;;
yellow) bg=”43m” ;;
blue) bg=”44m” ;;
white) bg=”47m” ;;
black) bg=”40m” ;;
*) bg=”40m” ;;
esac
echo -en “\033[${fg}\033[${bg}${text}\033[0m”
}
[/cc]

Unix: rename files to lowercase

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

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

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

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;
[/cc]
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;
[/cc]

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

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

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 ;
[/cc]