Sensible Open Source

Blog Status

  • 4 yrs 48 wks 5 days old
  • Updated: 11 Mar 2010
  • 455 entries
  • 174 comments
Total: 1,213,250
since: 5 Apr 2005

LinkBlogS

Creating Vidcasts

18 June 2007, Monday 7:05 P GMT-06
Tags:      

Man, Can't Microsoft Catch A Break?

16 March 2007, Friday 12:26 P GMT-06

Viacom, Google andYou Tube, Oh My!

16 March 2007, Friday 12:25 P GMT-06

Switch to digital TV to start in October

16 March 2007, Friday 7:46 A GMT-06

Is the Ice Ready? No, Its Still To Hot To Use...

16 March 2007, Friday 7:43 A GMT-06

MIT Entire Curriculum At disposal of e-learners

6 March 2007, Tuesday 11:52 A GMT-06

A cure for e-mail attention disorder?

2 March 2007, Friday 12:51 A GMT-06
Tags:  

Windows-on-Mac software gets virtualization update

1 March 2007, Thursday 5:08 A GMT-06

EnterpriseDB is/n't Open Source

1 March 2007, Thursday 3:37 A GMT-06

BitTorrent download portal debuts

27 February 2007, Tuesday 9:05 A GMT-06

$45b TXU buyout

27 February 2007, Tuesday 9:02 A GMT-06

iPhone Competitors Got The Touch

26 February 2007, Monday 3:43 A GMT-06
Tags:        

HTC - Smart Mobility

25 February 2007, Sunday 4:22 A GMT-06

Hard to find 1-800 numbers

23 February 2007, Friday 8:35 A GMT-06

Cuba Embraces Open-Source Software

21 February 2007, Wednesday 3:10 A GMT-06

Vista at the tipping point, Err Dipping Point?

11 February 2007, Sunday 11:11 A GMT-06
Tags:  

PostgreSQL Open Source And Persistence

3 February 2007, Saturday 10:32 P GMT-06

Blackboard Pledges No Patent Blocks

3 February 2007, Saturday 10:28 P GMT-06

UVU

5 January 2007, Friday 11:58 P GMT-06

Open-source IP PBX software appliance"

4 January 2007, Thursday 3:44 A GMT-06

Asterisk an under-appreciated Open Source Success Story

4 January 2007, Thursday 3:43 A GMT-06

Open Source AJAX Tooling

4 January 2007, Thursday 3:41 A GMT-06

Google MAIL API Secuirty Alert

1 January 2007, Monday 7:37 P GMT-06
Tags:    

United States Patent Application: 0060288329

26 December 2006, Tuesday 4:00 A GMT-06

LinkBlog Popular Tags

                                       







««Mar 2010»»
SMTWTFS
  12
3
456
789
10
111213
14151617181920
21222324252627
28293031

 

August 16, 2012

Time Left

2 years 5 months 4 days

                   

RSS Add-Me








Google Related Links

Shell Scripting The IF Statement and Test Command

posted 15 January 2010, Friday

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!! Embarassed 

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

Not the "-n" basically is a test for a string and returns true if the string is not empty.
 
The if statement basically looks for "Jan 15, 2010" in the file named month.log if it is found via the grep command the TEST expression will result in a true sucess and print out "Batch Run for the night!".  
 
There are several expressions for the TEST command and are listed below:  Also note the "[" and "]" these commands must be followed and preceded by a space or an error will occur
 
ERROR :
IF[-d dirName] then 
  echo DIR DirName exists!
fi
 
Proper SYNTAX:
IF[ -d dirName ] then 
  echo DIR DirName exists!
fi
 

Expression

Description (NOTE Returns: Success=0 Failure=1)

-a file  True if file exists. 
-b file  True if file is a block special
 -c fileTrue 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 fileTrue 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 fileTrue if file is a socket 
-t FDTrue if FD is opened on a terminal
-u fileTrue 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 FileTrue if the file is effectively owned by you
-G FileTrue if the file is owned by your group
-N FileTrue 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.

 
  • FILE1 -nt FILE2 True if FILE1 is newer than FILE2 based on modification date
  • FILE1 -ot FILE2 True if FILE1 is older than FILE2
  • FILE1 -ef FILE2 True if FILE1 is a hard link to FILE2

 

    String operators:

 

  • -z STRING      True if string is empty.
  • -n STRING      True if string is not empty.
  • STRING1 = STRING2  True if the strings are equal.
  • STRING1 != STRING2 True if the strings are not equal.
  • STRING1 < STRING2 True if STRING1 sorts before STRING2 lexicographically.
  • STRING1 > STRING2 True if STRING1 sorts after STRING2 lexicographically.

 

    Other operators:

 

  • -o OPTION      True if the shell option OPTION is enabled.
  • ! EXPR         True if expr is false.
  • EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
  • EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
  • arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne, -lt, -le, -gt, or -ge.

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.


links: digg this    del.icio.us    technorati    reddit