Bash (Unix shell): Difference between revisions

 
(23 intermediate revisions by the same user not shown)
Line 20: Line 20:
[https://linuxize.com/post/bash-if-else-statement/ https://linuxize.com/post/bash-if-else-statement/]
[https://linuxize.com/post/bash-if-else-statement/ https://linuxize.com/post/bash-if-else-statement/]
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
if [[ $VAR -gt 10 ]] then
if [[ $VAR -gt 10 ]]; then
   echo "The variable is greater than 10."
   echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]] then
elif [[ $VAR -eq 10 ]]; then
   echo "The variable is equal to 10."
   echo "The variable is equal to 10."
else
else
   echo "The variable is less than 10."
   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
fi
</syntaxhighlight>
</syntaxhighlight>
Line 33: Line 54:
* <code>-gt</code> is <code>></code>
* <code>-gt</code> is <code>></code>
* <code>-ge</code> is <code>>=</code>
* <code>-ge</code> is <code>>=</code>
===Check if File/Folder Exists===
See [https://linuxize.com/post/bash-check-if-file-exists/#check-if-directory-exist https://linuxize.com/post/bash-check-if-file-exists/#check-if-directory-exist].


===Arrays===
===Arrays===
Line 48: Line 66:
   /media/veracrypt7
   /media/veracrypt7
   /media/veracrypt8
   /media/veracrypt8
  /media/veracrypt9
)
)
# Add to the array
DRIVES+=(/media/veracrypt9)


for i in "${DRIVES[@]}"
for i in "${DRIVES[@]}"
Line 70: Line 89:
</syntaxhighlight>
</syntaxhighlight>


==dirname==
===Redirects===
[https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself Stack overflow]
[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


How to get the source directory of the bash script
# Append stdout to file
ls >> file.txt


<syntaxhighlight lang="bash">
# Output stderr and stdout to file
#!/bin/bash
ls > file.txt 2>&1
ls &> file.txt # Only works in bash, not sh


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
</syntaxhighlight>
</syntaxhighlight>


==<code>.bashrc</code>==
===Trap===
Your <code>.bashrc</code> file will be loaded for each terminal.<br>
To make a bash script exit on ctrl c:
 
<syntaxhighlight lang="bash">
==Presentation of Shell Variable==
trap "exit" INT
Add the following to show your
</syntaxhighlight>
<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\]\$'
trap 'kill $(jobs -p)' EXIT
</syntaxhighlight>
</syntaxhighlight>


==Brace Expansion==
===Wait===
Add <code>wait</code> at the end of a script to wait for all subprocesses.
 
===Brace Expansion===
When you type the following:
When you type the following:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 105: Line 136:
echo "${APPLES[@]}"
echo "${APPLES[@]}"
</syntaxhighlight>
</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>==
Your <code>.bashrc</code> file will be loaded for each terminal.<br>
===Presentation of Shell Variable (PS1)===
Add the following to show your current working directory.
<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\]\n\$ '
</syntaxhighlight>
[[Category:Programming languages]]