Bash (Unix shell): Difference between revisions
No edit summary |
|||
(33 intermediate revisions by the same user not shown) | |||
Line 12: | Line 12: | ||
echo $myvariable $anothervar | echo $myvariable $anothervar | ||
</syntaxhighlight> | </syntaxhighlight> | ||
You can check your bash scripts using [https://www.shellcheck.net/ https://www.shellcheck.net/]. | |||
==Usage== | ==Usage== | ||
===Basic If Statements=== | |||
[https://linuxize.com/post/bash-if-else-statement/ https://linuxize.com/post/bash-if-else-statement/] | |||
<syntaxhighlight lang="bash"> | |||
if [[ $VAR -gt 10 ]]; then | |||
echo "The variable is greater than 10." | |||
elif [[ $VAR -eq 10 ]]; then | |||
echo "The variable is equal to 10." | |||
else | |||
echo "The variable is less than 10." | |||
fi | |||
</syntaxhighlight> | |||
[https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html bash if statements] | |||
;Some common expressions: | |||
<syntaxhighlight lang="bash"> | |||
# File exists and is a regular file. | |||
[ -f $FILE ] | |||
# File is non-zero size | |||
[ -s $FILE ] | |||
# String has length zero | |||
[ -z $STR ] | |||
# String has length non-zero | |||
[ -n $STR ] | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash"> | |||
# Check if user is root (https://stackoverflow.com/questions/18215973/how-to-check-if-running-as-root-in-a-bash-script) | |||
if [ "$EUID" -ne 0 ]; then | |||
echo "Please run as root" | |||
exit | |||
fi | |||
</syntaxhighlight> | |||
===Comparisons=== | |||
* <code>-eq</code> is <code>==</code> | |||
* <code>-gt</code> is <code>></code> | |||
* <code>-ge</code> is <code>>=</code> | |||
===Arrays=== | |||
<syntaxhighlight lang="bash"> | |||
DRIVES=( | |||
/media/veracrypt1 | |||
/media/veracrypt2 | |||
/media/veracrypt3 | |||
/media/veracrypt4 | |||
/media/veracrypt5 | |||
/media/veracrypt6 | |||
/media/veracrypt7 | |||
/media/veracrypt8 | |||
) | |||
# Add to the array | |||
DRIVES+=(/media/veracrypt9) | |||
for i in "${DRIVES[@]}" | |||
do | |||
ls $i | |||
done | |||
</syntaxhighlight> | |||
;Notes | |||
* <code>"${DRIVES[@]}"</code> means every element will be a new word | |||
* <code>"${DRIVES[*]}"</code> will expand the array into a single word | |||
===Functions=== | |||
<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
function say_hello { | |||
echo Hello $1 | |||
} | |||
say_hello World | |||
</syntaxhighlight> | |||
===Redirects=== | |||
[https://www.gnu.org/software/bash/manual/html_node/Redirections.html Redirections] | |||
<syntaxhighlight lang="bash"> | |||
# `ls` is an example command | |||
# Input to stdin | |||
ls < word | |||
# Output stdout to file | |||
ls > file.txt | |||
# Append stdout to file | |||
ls >> file.txt | |||
# Output stderr and stdout to file | |||
ls > file.txt 2>&1 | |||
ls &> file.txt # Only works in bash, not sh | |||
</syntaxhighlight> | |||
===Trap=== | |||
To make a bash script exit on ctrl c: | |||
<syntaxhighlight lang="bash"> | |||
trap "exit" INT | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="bash"> | |||
trap 'kill $(jobs -p)' EXIT | |||
</syntaxhighlight> | |||
===Wait=== | |||
Add <code>wait</code> at the end of a script to wait for all subprocesses. | |||
===Brace Expansion=== | |||
When you type the following: | |||
<syntaxhighlight lang="bash"> | |||
echo {red,green,blue}_apple | |||
</syntaxhighlight> | |||
bash will print out | |||
<pre> | |||
red_apple green_apple blue_apple | |||
</pre> | |||
If you want to save this into a variable, you can save it as an array: | |||
<syntaxhighlight lang="bash"> | |||
APPLES=({red,green,blue}_apple) | |||
echo "${APPLES[@]}" | |||
</syntaxhighlight> | |||
===<code>--</code>=== | |||
[https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean Reference] | |||
<code>--</code> explicitly ends non-positional arguments for many commands | |||
===alias=== | |||
You can add aliases to your <code>.bashrc</code> to make commmon commands shorter. | |||
<syntaxhighlight lang="bash"> | |||
alias kc=kubectl | |||
alias tb="tensorboard --logdir" | |||
</syntaxhighlight> | |||
==dirname== | |||
[https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself Stack overflow] | |||
How to get the source directory of the bash script | |||
<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |||
cd "${DIR}" || exit 1 | |||
</syntaxhighlight> | |||
==Check if running as sudo== | |||
[https://stackoverflow.com/questions/18215973/how-to-check-if-running-as-root-in-a-bash-script Reference] | |||
<syntaxhighlight lang="bash"> | |||
if [ $EUID -ne 0 ] | |||
then echo "Please run as root" | |||
exit | |||
fi | |||
</syntaxhighlight> | |||
==<code>.bashrc</code>== | ==<code>.bashrc</code>== | ||
Your <code>.bashrc</code> file will be loaded for each terminal.<br> | Your <code>.bashrc</code> file will be loaded for each terminal.<br> | ||
==Presentation of Shell Variable== | ===Presentation of Shell Variable (PS1)=== | ||
Add the following to show your | Add the following to show your current working directory. | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$' | PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\$ ' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Programming languages]] |
Latest revision as of 00:44, 26 December 2023
Bash Scripting
Getting Started
Here is an example bash script
#!/bin/bash
# A simple variable example
myvariable=Hello
anothervar=Fred
echo $myvariable $anothervar
You can check your bash scripts using https://www.shellcheck.net/.
Usage
Basic If Statements
https://linuxize.com/post/bash-if-else-statement/
if [[ $VAR -gt 10 ]]; then
echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]]; then
echo "The variable is equal to 10."
else
echo "The variable is less than 10."
fi
- Some common expressions
# File exists and is a regular file.
[ -f $FILE ]
# File is non-zero size
[ -s $FILE ]
# String has length zero
[ -z $STR ]
# String has length non-zero
[ -n $STR ]
# Check if user is root (https://stackoverflow.com/questions/18215973/how-to-check-if-running-as-root-in-a-bash-script)
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
Comparisons
-eq
is==
-gt
is>
-ge
is>=
Arrays
DRIVES=(
/media/veracrypt1
/media/veracrypt2
/media/veracrypt3
/media/veracrypt4
/media/veracrypt5
/media/veracrypt6
/media/veracrypt7
/media/veracrypt8
)
# Add to the array
DRIVES+=(/media/veracrypt9)
for i in "${DRIVES[@]}"
do
ls $i
done
- Notes
"${DRIVES[@]}"
means every element will be a new word"${DRIVES[*]}"
will expand the array into a single word
Functions
#!/bin/bash
function say_hello {
echo Hello $1
}
say_hello World
Redirects
# `ls` is an example command
# Input to stdin
ls < word
# Output stdout to file
ls > file.txt
# Append stdout to file
ls >> file.txt
# Output stderr and stdout to file
ls > file.txt 2>&1
ls &> file.txt # Only works in bash, not sh
Trap
To make a bash script exit on ctrl c:
trap "exit" INT
trap 'kill $(jobs -p)' EXIT
Wait
Add wait
at the end of a script to wait for all subprocesses.
Brace Expansion
When you type the following:
echo {red,green,blue}_apple
bash will print out
red_apple green_apple blue_apple
If you want to save this into a variable, you can save it as an array:
APPLES=({red,green,blue}_apple)
echo "${APPLES[@]}"
--
--
explicitly ends non-positional arguments for many commands
alias
You can add aliases to your .bashrc
to make commmon commands shorter.
alias kc=kubectl
alias tb="tensorboard --logdir"
dirname
How to get the source directory of the bash script
#!/bin/bash
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "${DIR}" || exit 1
Check if running as sudo
if [ $EUID -ne 0 ]
then echo "Please run as root"
exit
fi
.bashrc
Your .bashrc
file will be loaded for each terminal.
Presentation of Shell Variable (PS1)
Add the following to show your current working directory.
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\$ '