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