Contents |
|---|
Other Pages
- How to generate and use ssh keys .
- Setting up Java classpath using a shell script Classpath using shell.
- Bash Reference Manual
- bash
- Check Return Code
- Classpath using shell
- Command Line Arguments with Shell
- Cygwin
- Finding Deleted Files and Directories in Subversion
- Find Java in a shell script
- Indirect variable references in Bash
- Looping over values in shell
- Reading a file line by line with shell
- Reading Java-style Properties Files with Shell
- scp Speed Test
- Searching with find and grep
- Sed one liners
- Shell and MySQL
- Shell One Liners
- Ssh
- Start off with a shebang
- Useful Shell Functions
- Using the current date for file names
One liners
See Shell One Liners
Shell Programming
Start with a shebang!
Launching a nested shell from a shell script
Nested shells can be really useful for setting up environment variables, PATH, and LD_LIBRARY_PATH in a clean way. You could just source in a shell script that sets environment varaibles, etc, but in the nested shell it is much easier to return to the un-touched environment by typing exit.
Cygwin compatibility
If you are using Cygwin, you need to do a few extra things to 'path names' and other structures before invoking non-Cygwin commands. ANT is a good example because it is run under Java, which doesn't know about Cygwin paths.
Here is an example of setting ANT_HOME from a shell script:
Of course, this won't work on Linux, so it could be made more portable like this:
Note:
PATHis not converted. That's because Cygwin usesPATH.JAVA_HOMEandANT_HOMEare converted 'after'PATHis set becauseantdoesn't know about Cygwin's path system, but Cygwin bash does.
Script name and script path
Relative to absolute path names
To get the absolute path name from a relative path name:
Note that the 'cd' command inside the back-ticks does not affect the current directory of the running script.
To get the full path of the parent directory of the current script:
Here Documents
You can avoid creating temporary files in shell scripts by using 'here documents' (covered in the redirections section of the Bash reference manual).
A simple example:
Some common uses of here documents:
- Generating SQL scripts for MySQL, with shell variable interpolation, etc. See Shell and MySQL
Shell Gotchas
Common shell programming mistakes.
Boolean Logic Expressions - equality, AND, OR
Boolean logical expressions, in if and while statements for example, are a little different in shell. A Java programmer might be tempted to write:
This will normally produce a [: missing `]' error because && was used for a logical AND instead of -a. The correct code is:
Operation | Other Languages | Shell expression operator |
|---|---|---|
Logical AND | && (Java and C++), AND |
|
Logical OR | || (Java and C++) |
|
Equals | == (Java and C++) |
|
Test for Empty / Non-empty String
You can use -z and -n to test for empty string and non-empty string, respectively. There are two gotchas here:
- When testing the value of an environment variable, it must be enclosed in quotes.
- When using 'strict mode'
(set -u), environment variables that have not yet been set must be inside a conditional expression.
Surrounding the environment variable in quotes:
Using a conditional expression and also surrounding the variable in quotes: