if [ test = 'asdf' ]; then echo test;fi


  if [ test='asdf' ]; then echo test;fi
  1. Ctrl + A Go to the beginning of the line you are currently typing on
  2. Ctrl + E Go to the end of the line you are currently typing on
  3. Ctrl + L Clears the Screen, similar to the clear command
  4. Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
  5. Ctrl + H Same as backspace
  6. Ctrl + R Let's you search through previously used commands
  7. Ctrl + C Kill whatever you are running
  8. Ctrl + D Exit the current shell
  9. Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
  10. Ctrl + W Delete the word before the cursor
  11. Ctrl + K Clear the line after the cursor
  12. Ctrl + T Swap the last two characters before the cursor
  13. Esc + T Swap the last two words before the cursor
  14. Alt + F Move cursor forward one word on the current line
  15. Alt + B Move cursor backward one word on the current line
  16. Tab Auto-complete files and folder names

Liste wichtiger Dateien, Verzeichnisse und Anwendungen:

 echo $a

We have shell and enviroment variables. With the command "export" you can declare a already defined shell variable to an enviroment variable. The command "env" returns all the exported variables. With "set" you get exported variables and the shell variables. With "export -n variable" you can annihilate the earlier export or makes an enviroment variable to a shell variable. With "unset" you can kill the variable.

The difference between login shells and shells which only execute a script is the start up process. The login shell processes first the "/etc/profile" file, then "~/.bash_profile" or "~/.bash_login" or "~/.profile" dependent which one was first founded. A normal shell first executes "/etc/bash.bashrc" and "~/.bashrc" if it exists. At the end of a shell it will be executed the file "~/.bash_logout"


 history -c
clear history
 history -w file
write history to file

1.109.2 Anpassen oder Schreiben einfacher Scripts (weight: 3)

Beschreibung: Die Kandidaten sollen in der Lage sein, existierende Scripts anzupassen oder einfache neue BASH-Scripts zu schreiben.

Wichtigste Wissensgebiete:

Liste wichtiger Dateien, Verzeichnisse und Anwendungen:

Under linux it is not possible to get root rights with the use of the sticky bit. You can set the bit but it will be futile.

if ... then ... elif ... else ... fi

 if test -f /etc/fstab;
   echo "asdf";
   echo "kkkkk";

Equivalent to "test -a /etc/fstab" is "[ -a /etc/fstab ]".

Test options: Keep in mind the spaces between stings!!! Unless the command test return always true.

 -d file
file a dir
 -f file
file exist and is regular file
 -r file 
file exist and is readable from user that called the script
 -w file
same as read but writing
same as read but executing
 -z string
empty string
 -n string 
string is not empty
 str = str2
 str != str2
 str -lt str2
 str -gt str2

for and while loops

 $for i in 1 2 3;do echo $i;done
 $while true;do echo "I was here";done
 $ for i in $(seq 1 1000);do echo $i;done

backticks and brackets

The backtick method is the older one. To replace a string with its result use `...`:

 now=`date +%F`

The newer method is

 now=$(date +%F)

and has the advantage that you can use nested forms.

With "seq" you can give a sequence of numbers:

 for i in $(seq 1 1000);do echo $i;done

What is happening when we execute the following script?

 cd ..

Response: nothing, because the shell will execute the script with a new shell, then it goes up a directory and ends its execution. Therefore it lands again at the start directory. Such a functionality can only be made by a shell function:

 up() { cd ..; }

Attend the space between "{" and the first command. It is not for well form related but syntactical.

With $1, $2 and so on you can touch the arguments to the script. $0 is the name of the script and $@ the whole set of arguments.

With $$ you can access the process id of the script.

 $ echo My id is $$
 My id is 5027

 $? is the errorcode of the last process.

Very useful is also the variable $! which is assigned with the value of the process id of the last started background process.

 $ find / -name "*.txt" -print &
 $ kill $!
kills the find process.
 $* all arguments in one string
 $@ all arguments in an array of strings
 $# number of arguments in the command line
 $_ last argument of the process which was called at last

Automatic variables:

 $RANDOM  radom number between 0 and 32767
 $REPLY   is set by command read if no variable is given
 $SECONDS number of seconds since last start or login of the current shell

Getting the options from command line

getopts delivers the options and even wrong parameters.

 while getopts abc:D: opt
   case $opt in
     a) echo "Option a wurde angegeben";;
     b) echo "Option b wurde angegeben";;
     c) echo "Option c wurde angegeben: $OPTARG";;
     D) echo "Option D wurde angegeben: $OPTARG";;

parameters beyond $9

 echo "the 20th argument: ${20}"

comparison of numbers

 [ "$var1" = "$var2" ]
 [ "$var1" != "$var2" ]
 [ -z "$var2" ]     # var empty?
 [ -n "$var3" ]     # var not empty?


 printf format arg1 arg2 ...
 %c char
 %s string
 i integer
 %u integer
 %f real
 E real exponetial 
 X integer hex
  escape char for %


 $cat results.txt
 Michael Foerster;45;20;17;20;17;-;-;74
 Frank Sebastian Haensch;2;17;20;15;20;-;-;72

 for data in $(cat results.txt)
   name=$(echo $data | cut -d\; -f1 )
   nr=$(echo $data | cut -d\; -f2)
   punkte=$(echo $data | cut -d\; -f9)
   printf "Fahrer %30s     Nr %3d Punkte %4d\n" $name $nr $punkte

 Fahrer               Michael_Foerster     Nr  45 Punkte   74
 Fahrer        Frank_Sebastian_Haensch     Nr   2 Punkte   72

adjust console view with tput

 $ echo "`tput bold` bold `tput sgr0` or `tput rev`invers`tput sgr0` or `tput smul`underline`tput sgr0`"
 bold  or invers or underline

 $ echo "`tput setf 2`green`tput setf 4`red`tput sgr0`"


 0 black
 1 blue
 2 green
 3 yellow
 4 red
 5 purple
 6 cyan
 7 grey

calculating with bourne shell

 expr 2048 \* 2

 let Z=2*2048
 echo $Z

random numbers with bash

 for (( I=1; i<1000 ; i++));do printf "%.2d.Zahl ist %10d\n" $i $RANDOM;done

Auswahlmenüs auswählen

 select auswahl in Punkt1 Punkt2 Punkt3 Punkt4
     echo "wahl war: $auswahl"

Wait for other processes

 wait 4711
waits for the process with PID 4711. wait returns 127 if the process no longer exists. Otherwise it is equal to the PID.
without any parameter wait waits on active child processes. Here the return value is always zero.

create user defined SIGNAL

 trap './script' SIGUSR1
 cat script
 kill -SIGUSR1 $$

job management

 fg %jobnr
 bg %jobnr

seq with formated string

 seq -f '%05g' 3
 seq --format='%05g' 3


With eval you can execute a command sequence that is stored in a string like:

 set -x
 LSCMD="ls -lt"
 eval $LSCMD

The set -x enables output where you can see what the shell makes out of the string.

dirname and basename

 dirname /home/user/asdf.txt
gives the directory part of the full-path. Here /home/user
 basename /home/user/asdf.txt
gives the file name. Here asdf.txt
 basename /home/user/asdf.txt .txt
gives asdf what is useful in case of renaming files or some kind of this.


With ulimit you can set the upper bound for resource use.

 ulimit -a
 ulimit -c 0
core dumps are forbidden
 ulimit -f 512000
no file size above 512mb
 ulimit -S -n 250
soft limit of 250 file descriptors
 ulimit -S -u 100
soft limit of 100 processes
 ulimit -S -v 50000
soft limit for maximal use of 50mb of memory