Bash modules: Difference between revisions
From wikinotes
Line 15: | Line 15: | ||
</blockquote><!-- shebang --> | </blockquote><!-- shebang --> | ||
= | = Set bash options = | ||
<blockquote> | <blockquote> | ||
See docs https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin | |||
< | |||
set -e | * <code>-${flag}</code> set option | ||
</ | * <code>+${flag}</code> unset option | ||
</blockquote> | |||
<syntaxhighlight lang="bash"> | |||
# some common bash script options | |||
set -e `# exit if any pipe/command fails` \ | |||
set -o pipefail `# pipe exitcode is last pipe in pipeline to exit w/ failure` \ | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash"> | |||
# some interactive options | |||
set -o vi | |||
</syntaxhighlight> | |||
</blockquote><!-- --> | |||
= Finding Path of Running Bash Script = | = Finding Path of Running Bash Script = |
Revision as of 12:58, 13 August 2021
shebang
Windows uses file-extensions to determine what program to use to open/run a program. Unix systems use mimetypes, which uses a combination of file-extensions, and signatures written within the file. This means that file-extensions are optional for bash-scripts.
Place this line at the top of your file to instruct your system that this file should be opened with an executable called bash. You can substitute this with something else, for example this might be a python, perl, or ruby script.
#!/usr/bin/env bash
Set bash options
See docs https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
-${flag}
set option+${flag}
unset option# some common bash script options set -e `# exit if any pipe/command fails` \ set -o pipefail `# pipe exitcode is last pipe in pipeline to exit w/ failure` \# some interactive options set -o vi
Finding Path of Running Bash Script
currentPath="`( cd \"$currentPath\" && pwd )`" #OR DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"## This works as long as you are not using a symbolic link. In the event that you ## are using a symbolic link: SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCE="$(readlink "$SOURCE")" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
Commandline Arguments
See bash arguments .