Shell scripting has just become second nature to me and I am typical, just like any person whom redundantly uses a tool. Asa a result, I think everyone (including my mother, only kidding) knows how to use an IF statement with the TEST command when shell scripting. This is the rub, I just never comment my scripts because, well, you should know it right!! WRONG!!
With 2010 comes a new outlook and proper commenting of my code be it shell scripting, java, c# or ruby.
What sparked this new outlook, well this little piece of a script that someone asked me about below:
if [ -n "`grep 'Jan, 15 2010' month.log`" ]; then echo Batch Run for the night! fi |
They had never seen the -n conditional test. So it took me a little by surprise and my explanation was pitiful.
When using the if statement in conjunction with the TEST command the expressions allow status inspection and existence of files, directories and other interesting tips and bits.
A Unix program, if written correctly, responds with exit codes on successful execution or failure. This is returned as an integer value and the if statement in conjunction with the TEST command applies some interesting capabilities.
For example, when a properly written program exits successfully it returns a 0. Any other value indicates failure. As a result, the if statement can make use of this in its testing of a condition.
"NOTE: When writing scripts a good habit is exiting a script with an exit statement and proper return. On success exit with "exit 0" on failure exit with a number "exit 1" to better document the failure. "
Take a look at the TEST command by entering help test at the command line. The TEST command is a perfect complement to the if statement. The two forms of the test command are seen below:
FORM 1:
test expression
FORM 2:
[ expression ]
As you can see in the above code the second form has been used.
The TEST command allows a script to use a variety of expressions, which are listed at the end of this article: Basically, we can make some really good decisions, and allows us to be as certain as one can be. When using a series of programs step to step to accomplish a process and manage it to its end through the use of test.
-n is one of those expressions that makes scripting a little easier. The original if statement is :
if [ -n "`grep 'Jan, 15 2010' month.log`" ]; then
Expression | Description (NOTE Returns: Success=0 Failure=1) |
| -a file | True if file exists. |
| -b file | True if file is a block special |
| -c file | True if files is a character special |
-d file | True if file is a directory |
-e file | True if file exists. |
-f file | True if file exists and is a regular file. |
| -g file | True if if file is set-group-id |
| -h file | Tre if files is a symbolic link |
-L file | True if file is a symbolic link. |
| -k file | True if file has its `sticky' bit set |
| -p file | True if file is a named pipe |
-r file | True if file is a file readable by you. |
| -s file | True if file exists an is not empty |
| -S file | True if file is a socket |
| -t FD | True if FD is opened on a terminal |
| -u file | True if the file is set-user-id |
-w file | True if file is a file writable by you. |
-x file | True if file is a file executable by you. |
| -0 File | True if the file is effectively owned by you |
| -G File | True if the file is owned by your group |
| -N File | True if the file has been modified since it was last read |
file1 -nt file2 | True if file1 is newer than (according to modification time)file2 |
file1 -ot file2 | True if file1 is older than file2 |
-z string | True if string is empty. |
-n string | True if string is not empty. |
string1 = string2 | True if string1 equals string2. |
string1 != string2 | True if string1 does not equal string2. |
String operators:
Other operators:
Arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2.