When to use shell scripts

With my new book “Oracle Shell Scripting” now on shelves you may be wondering what shell scripting can do for you. Below is an excerpt from the book that touches on when you may want to think about scripting. While the book is geared toward Oracle users much of the content would apply to shell scripting regardless of the use.

When to script

A shell script can be written to do anything you would do at the command line. So when do you want to write a shell script? When is it a bad idea? Well, here are a few guidelines I use.

Shell scripts can yield the biggest return on regular tasks that are performed more-or-less the same way each time. If every day you log into a system and remove some files out of a directory, backup a database or check a log file for a specific string of characters you should automate these tasks. Chances are you can make them run automatically and save yourself the hassle all together.

Shell scripts aren’t just for automation though. In some cases a script saves us time in a different way by allowing us to run a simple command instead of a very complicated one.

One of the most powerful features of the shell and shell scripting is the ability to affect several files and even multiple servers with a single script. Loops and commands like find make shell scripting ideal for managing large numbers of files in a single step.

In general there is little point in writing a shell script to do something once. The exception to this is occasionally something needs to be run at a time when you would rather not have to be around to run it yourself. In this case you may decide to bundle those commands into a shell script and schedule it to run without you involved. Be careful with this type of script and always be thinking “what if something goes wrong?” because sooner or later it will and if you’re not around to fix it you could get in some hot water.

Oracle Shell ScriptingFor more information like this check out my book Oracle Shell Scripting, only $34.95 from Rampant TechPress.

Buy it now!

shell script, unix, linux, oracle, bash, scripting

Oracle Shell Scripting available on Amazon.com

Oracle Shell ScriptingMy book Oracle Shell Scripting: Linux and UNIX Programming for Oracle has just become available on Amazon.com!

The book has been out for just a couple weeks and should be in book stores soon, but it can be had online right now! The best price on the book right now can be found at Rampant TechPress who is currently selling the book for $34.95. Amazon is currently selling the book at the cover price of $49.95.

unix, oracle, shell scripting, linux, book, database tuning, database administration, database security

Color ls output and permission denied

In my post from earlier this week on removing the color output from the ls command I mentioned that in some circumstances the color-coded output can cause permissions errors. Cloned Milkmen asked that I explain why this would be.

I was hoping someone would ask that. In Linux and UNIX if the execute flag is not set on a directory, as in the example below, the ls --color=tty command will return Permission denied.

$ ls -ld ls_test/
dr-------- 4 jonemmons pg222211 4096 Nov 12 20:07 ls_test/
$ ls ----color=tty ls_test/
ls: ls_test/test1: Permission denied
ls: ls_test/test2: Permission denied
ls: ls_test/test3: Permission denied
dir1 dir2
$ ls ls_test/
dir1 dir2 test1 test2 test3

The execute flag is funny on directories. The main effect is that it will inhibit a user’s ability to cd into a directory, but it will also keep you from being able to get information out of the file’s inode (the inode contains information about when a file was created, modified, accessed etc.) Since ls requires information from the inode to properly color-code files in a directory it will fail if the execute bit is not set. An ls -l will fail for the same reason.

So thanks to Cloned Milkmen for asking the question. If you’re looking for some eclectic and distracting reading, check out his blog The Synthetic Librarian.

unix, linux, directory, permission, security, sysadmin, system administration

Oracle Shell Scripting Now Available!

Oracle Shell ScriptingExactly 15 months after I first posted about it my book Oracle Shell Scripting: Linux and UNIX Programming for Oracle has finally been printed and is available!

The book offers an introduction to shell scripting, an in-depth look at many useful shell commands and tools and a bunch of example scripts to use as-is or as a basis for your own custom scripts. As a long-time database and system administrator I have compiled some of the best tools, tips and tricks I have found for administration, monitoring and automation of DBA tasks.

I know you’re just dying to go out and get it, but it will probably take a couple weeks for it to hit book stores and Amazon. The best way to buy the book is directly from the publisher. They have it in stock and ready to go.

I will be posting more about the book here in the near future. It really covers a lot of what I have learned in my professional career and I’m thrilled at the opportunity to share my experience in this form. If you have questions about the book please feel free to leave a comment. I don’t always get to my comments quickly, but I do read and reply to all of them.

unix, oracle, shell scripting, linux, book, database tuning, database administration, database security

Get rid of color ‘ls’ output in Linux

Color ls outputMany popular varieties of Linux use a “feature” which causes the ls command output to show files, directories, links, etc. all in different colors. I guess some people prefer this, but I find it at best annoying, and at worst illegible. Specifically the color-coding of symbolic links tend to show in such a light color that it is often impossible to read.

The color output is accomplished by adding the --color=tty or a similar option to the ls command. This is typically accomplished by creating an alias to ls in either the user’s profile or in one of the system-wide profiles.

alias ls='ls --color=tty'

My personal preference is to remove this line from any system-wide configuration files (such as /etc/profile) and allow users to set it in their own profile if preferred. If you don’t have the desire or ability to make this change universally than a user can easily disable the color output by using the unalias command:

unalias ls

This can either be added to the user’s configuration file (e.g. the .profile or .bash_profile in their home directory), or you can just type unalias ls anytime to disable color ls output for the rest of the current shell session. This can be especially useful to turn off the color output when you’re working on someone else’s system.

Color-coded ls output can cause permissions errors in some circumstances, so in my opinion it is best left off, but if you’re stuck with it then it’s nice to know how it can be disabled when necessary.

ls, sysadmin, system administration, linux, shell, bash, sh, UNIX