meta data for this page
This is an old revision of the document!
include/source other scripts
Possible test scenario:
- call using from another directory full path /home/user/bin/myscripth.sh
- add /home/user/bin to PATH and then call from another directory by “myscript.sh”
- create symlink to script and call using symlink. Check for relative and absolute symlinks.
- it should work also if it is sourced “source” or “.”
The best is to create setup/installer script which hardcode path to one shared script.
Another solutions:
#!/bin/sh MY_DIR=$(dirname $(readlink -f $0)) $MY_DIR/other_script.sh
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "$SOURCE" )" && pwd )"
cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" cur_dir="$(dirname "${cur_file}")
`BASH_SOURCE'
An array variable whose members are the source filenames where the
corresponding shell function names in the `FUNCNAME' array
variable are defined. The shell function `${FUNCNAME[$i]}' is
defined in the file `${BASH_SOURCE[$i]}' and called from
`${BASH_SOURCE[$i+1]}'
double brackets [[ ]]
Bash double brackets Bash (extended test command) are safer and provides more features but they are not portable (and not sh compatible). Features:
- not filename expansion. Strings will be take literally (i.e. “name*”).
- operators like || instead of -o
- regex matching =~
[[ -e $file_name ]]
[ -e "$file_name" ]
Check if string contains another string
not compatible with sh
if [[ $string == *"word1 word2"* ]] ...
if [[ $string =~ .*word.* ]] ...