Labels

.net (1) *nix (1) administration (1) Android (2) Axis2 (2) best practice (5) big-data (1) business-analysis (1) code re-use (1) continuous-integration (1) Cordova-PhoneGap (1) database (2) defect (1) design (3) Eclipse (7) education (1) groovy (2) https (2) Hudson (4) Java (1) JAX-RS (2) Jersey (3) Jetty (1) localization (1) m2eclipse (2) MapForce (1) Maven (12) MySQL (1) Nexus (4) notes (4) OO (1) Oracle (4) performance (1) Perl (1) PL/SQL (1) podcast (1) PostgreSQL (1) requirement (1) scripting (1) serialization (1) shell (1) SoapUI (1) SQL (1) SSH (2) stored procedure (1) STS (2) Subclipse (1) Subversion (3) TOAD (3) Tomcat (4) UML (2) unit-testing (2) WAMP (1) WAS (3) Windows (3) WP8 (2) WTP (2) XML (4) XSLT (1)

Shell scripting for the experienced programmer

I've been programming for over 10 years, with significant Perl experience but never have had to develop with Unix shell scripts.  Sometimes this is the default tool, as in my case with Korn shell scripting.  Some quick tips and fixes:
  • If strange behavior is encountered running an existing (working) script, your default shell may differ.  At the top of the script, you may need to explicitly set your shell, e.g. change #!/bin/sh to #!/bin/bash
  • Whitespace is very important to the declaration of variables, that is:
  • numSections = 0;
    
    attempts to execute a command named numSections whereas
    numSections=0;
    
    declares a variable of name numSections 
  • If you have developed your script on a Windows machine and are testing it out for the first time on a Unix machine, you may not be able to get the script to execute at all.  This may be due to end-of-line differences between Windows and Unix (in the vi editor, you will see ^M at the end of every line).  These need to be removed in order for successful execution on Unix.  With Notepad++, you simply go Edit > EOL Conversion > UNIX format and save your script
  • Example of a numerical calculation (the length of a string is subtracted from the length of a longer string and one is subtracted from the result):  length_to_keep=$((${#APP_HOME_DIR}-${#APP_DIR_NAME}-1))   
  • Example of performing a substring operation (from beginning of string to a calculated value):  DEPLOYMENT_SCRIPT_DIR=`expr substr ${APP_HOME_DIR} 1 ${length_to_keep}`    
  • Best tip:  If possible, use Perl rather than shell scripting since perl is available on most Unix machines and its syntax and API is closer to higher-level programming languages.  Other advantages:  Can define subroutines after they are referenced in code, usage of whitespace does not change script logic 

No comments:

Post a Comment