Wiki/Code/Bash/Bash-Basics.md
2023-12-20 16:30:40 +01:00

3.9 KiB

Bash scripting basics

Kommt immer zu begin eines neuen Scripts

#!/bin/bash

Berechtigung um ausgeführt zu werden

chmod +x FILENAME

Description Numeric Comparison String Comparison

Shell comparison example:   [ 100 -eq 50 ]; echo $? [ "GNU" = "UNIX" ]; echo $?
less than                         -lt                       <
greater than                      -gt                       >
equal                             -eq                       =
not equal                         -ne                       !=
less or equal                     -le                       N/A
greater or equal                  -ge                       N/A
boolean
true=0
false=1

Operator        Description
! EXPRESSION    The EXPRESSION is false.
-n STRING       The length of STRING is greater than zero.
-z STRING       The lengh of STRING is zero (ie it is empty).
[ -a FILE ]     True if FILE exists.
[ -b FILE ]     True if FILE exists and is a block-special file.
[ -c FILE ]     True if FILE exists and is a character-special file.
[ -d FILE ]     True if FILE exists and is a directory.
[ -e FILE ]     True if FILE exists.
[ -f FILE ]     True if FILE exists and is a regular file.
[ -g FILE ]     True if FILE exists and its SGID bit is set.
[ -h FILE ]     True if FILE exists and is a symbolic link.
[ -k FILE ]     True if FILE exists and its sticky bit is set.
[ -p FILE ]     True if FILE exists and is a named pipe (FIFO).
[ -r FILE ]     True if FILE exists and is readable.
[ -s FILE ]     True if FILE exists and has a size greater than zero.
[ -t FD   ]     True if file descriptor FD is open and refers to a terminal.
[ -u FILE ]     True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ]     True if FILE exists and is writable.
[ -x FILE ]     True if FILE exists and is executable.
[ -O FILE ]     True if FILE exists and is owned by the effective user ID.
[ -G FILE ]     True if FILE exists and is owned by the effective group ID.
[ -L FILE ]     True if FILE exists and is a symbolic link.
[ -N FILE ]     True if FILE exists and has been modified since it was last read.
[ -S FILE ]     True if FILE exists and is a socket.
STRING1       =    STRING2      STRING1 is equal to STRING2
STRING1      !=    STRING2      STRING1 is not equal to STRING2
INTEGER1    -eq    INTEGER2     INTEGER1 is numerically equal to INTEGER2
INTEGER1    -gt    INTEGER2     INTEGER1 is numerically greater than INTEGER2
INTEGER1    -lt    INTEGER2     INTEGER1 is numerically less than INTEGER2

Example if then else in bash

#!/bin/bash
num_a=400
num_b=200
if [ $num_a -lt $num_b ]; then
    echo "$num_a is less than $num_b!"
else
    echo "$num_a is greater than $num_b!"
fi

IF ELSE && CASE

if      Perform a set of commands if a test is true.
else    If the test is not true then perform a different set of commands.
elif    If the previous test returned false then try this one.
&&      Perform the and operation.
||      Perform the or operation.
case    Choose a set of commands to execute depending on a string matching a particular pattern.
fi      End of if

Variabeln

name='Someone'       -Nur Text kann man in einem String speichern.
username=$(whoami)   -funktionen werden mit $() angegeben
day=$(date +%A)      -parameter können ohne Probleme in die Klammer schreiben.

Command Exec

der befehl kommt ohne syntax einfach auf eine leere zeile z.b.

fortune
cowsay hallo

ich kann die beiden commands aber auch zusammensetzen

fortune | cowsay

stderr stdout

The difference between stdout and stderr output is an essential concept as it allows us to a threat, that is, to redirect each output separately. The > notation is used to redirect stdout to a file whereas 2> notation is used to redirect stderr and &> is used to redirect both stdout and stderr. The cat command is used to display a content of any given file