Linux Blog

Lite Reading : a Review of SQLite by Chris Newman

Filed under: General Linux — TheLinuxBlog.com at 4:45 pm on Monday, June 15, 2009

SQLite This review is sort of a long time coming. The book is simply called SQLite. SQLite (the book) was written by Chris Newman (0-672-32685-X) and is one of the books in the Developer’s Library from InformIT. Books in the Developer’s Library are designed for programmers as high quality references and tutorials on technical subjects. I believe that this is the first book that I have read and personally owned in the series. The book’s “tag-line” so to speak is:
A practical guide to using, administering, and programming the database bundled with PHP 5
and hits it right on the mark. While the book is small it packs a punch. Chris Newman makes it a point to go into detail where needed and skip the parts that are not necessary. One one of my gripes about programming books is that they contain too much programming and logic basics.

The book is split up into three logical parts, Part I consisting of General SQLite Usage, Part II Using SQLite Programming Interfaces and Part III SQLite Administration.

Part I has four chapters the first not being entirely necessary for the SQL guru, but I learned a few things that I would not have known otherwise, and it was a quick read if you skip the basic SQL stuff. The second through forth chapters are on actually working with SQLite such as the structures, syntax / usage and query optimization.

Part II is a chapter for each interface (PHP, C/C++, Perl, Tcl and Python.) Whether you use each of these technologies or not is irrelevant since it is invaluable as a reference if you wished to use your existing databases with these languages. Reading all of these chapters are not needed if you have no intention of using the language right away.

Part III discusses administration and the SQLite Virtual Database Engine. For me just wanting to learn SQLite the section on the VDBE was a little overkill, but interesting none the less. I like that it was included and think it will be useful in the future.

Overall I think SQLite has a good balance for SQL newbies and seasoned gurus just wanting to quickly get up to speed and implement SQLite. It has everything I’ve needed and then some for my tinkering with SQLite.

Shell Scripting 101

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:27 am on Sunday, August 5, 2007

In my last shell scripting article I made a script to manipulate images with imagemagick. The script was only five lines long and has saved me endless hours of manual work. The script can be adapted to do anything repetitive on any types of files.

Since this is my first official shell scripting post I am going to introduce some common basic shell scripting. This is intended for people that want to learn the basics of shell scripting, so those that have experience already may want to skip this post.

Basic applications used in this tutorial
In order to teach a little about shell scripting, I need to use a couple of programs other than bash its self. The ones that I will use in this post are:
echo
This program displays a line of text
cat
Cat likes to read files and print what it finds out.
grep
Search through files with an expression to find a line that matches the pattern. Sounds complicated but really is not.
less
A paging utility is used to display data. Its kind of like opening a file in notepad Windows except with most utilities more can be done.
less is probably my favorite pager. There are many of these including tail and more. Less is more is the tag line of this pager because it does more than less but is the same thing.

Basic standard output redirecting
Often abbreviated as stdout or stderr for standard out and standard error.
This is normally used to take the output of one application and direct it somewhere. You can overwrite or append to the stdout. The symbol to do this is > to overwrite and >> to append.
stdin example:

echo "This is the new text" > File_To_Be_Written
echo "This is appended" >> File_To_Be_Written

“This is the new text” (without quotes) is redirected to File_To_Be_Written and overwrites the file.

“This is appended” is appended to the end of the file that was just created. If the file did not exist before hand, the file would have been created.

Redirecting Errors
Redirecting errors is achieved the same way as redirecting stdout except it has a 2 in front of the greater than symbol.
stderror example:

echo "This is the new text" 2> File_To_Be_Written
echo "This is appended" 2>> File_To_Be_Written

To append to stderror just think “To redirect to stderror is to redirect with two” Meaning, to redirect stderror is just to redirect with two in front. Cheesy, but its easy to remember.

Pipes
Piping is a common technique I normally use in all of my scripts. The way this works is to send the stdout of one program to the stdin of another.
Example:

cat Big_File | grep search | less

This basically reads the Big_File and then grep’s the file for lines that have search in them. It then pipes that output into the paging utility less.

Lists
A list is just a sequence of pipes that are separated by control operators. Basically
what that means is you have operators that do specific things depending on return codes

&
This operator puts the command into the background, gives a return status of 1 and moves on to the next task.
Example:

owen@amd:~$ echo "tails" > heads
owen@amd:~$ cat heads & echo "done"
tails
[1] 15385
done
[1]   Done                    cat heads

;
The ; operator waits until the process is finished before moving onto the next task.
Example:

owen@amd:~$ cat heads; echo "done"
tails
done

&&
command1 && command2
command2 will only be executed if command1 returns an exit status of zero
Example:

owen@amd:~$ cat heads && echo "done"
tails
done

Note this works because cat has returned the correct exit status

owen@amd:~$ cat eads && echo "done"
cat: eads: No such file or directory

Notice that this does not work because cat did not return the correct exit status.

||
command1 || command2
command2 will be executed if command1 returns a non zero return code.
Example:

owen@amd:~$ cat heads || echo "Did not work"
tails
Did not work is not printed because cat exited properly
owen@amd:~$ cat eads || echo "Did not work"
cat: eads: No such file or directory
Did not work

Did not work was printed because the eads file does not exist.

Throwing it all together:

echo "This is whats in the output file" > output;
echo "Now that has done, lets add something to the end" >> output;
cat output > normal_output && echo "This is not the error log. Press Q to exit this pager and view the error log" >> normal_output;
cat put >> normal_output 2> error_output;
cat normal_output | less ; cat error_output  | less

This short example basically echos some text to the output file. It then adds some more to the end of the output file. Next it reads from the output file into the normal_output file. If that executes successfully it goes on to echo some instructions to the normal_output file. The script moves on to read the put file (which does not exist) and directs the error of that command to a error_output file. After that, it reads the normal output and sends it to the less pager. The first prompt has instructions for ending the pager, and once the pager has been stopped it reads the error log.

Variables
Variables are used to temporarily store data in memory. To read a variable you can just just the echo command. An example of setting and reading a bash variable is below:

owen@amd:~$ variable='This is my variable'
owen@amd:~$ echo $variable
This is my variable

Flow Control
There are ways of controlling how a script is going to work. This is the logic part of the script, the intelligence if you will. There are many different ways to control flow with bash. The following topics should get you started:
Bash If / Then / Else statements & Cases
Bash Loops For, While and Until
Creating menus in Bash with select

Where to from here?
If you followed this example and understand everything so far then you have got your feet wet in shell scripting. You should now know a little on how to read/write files, redirect output and use conditional operators.
If you would like more help there are plenty of resources on-line and probably on the computer you are using to try the examples. Check the man pages for more help on the utility and “man bash” for bash help. If your looking for more tutorials go to Google and type “bash shell scripting tutorial”.
If you have a specific question that needs answering leave a comment or contact me.