if [ test = 'asdf' ]; then echo test;fi
NOT
if [ test='asdf' ]; then echo test;fi
Liste wichtiger Dateien, Verzeichnisse und Anwendungen:
variable=value a="aasdf" echo $a echo
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
history -w file
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 test -f /etc/fstab; then echo "asdf"; else echo "kkkkk"; fi
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
-f file
-r file
-w file
-x
-z string
-n string
str = str2 str != str2 str -lt str2 str -gt str2
$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
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?
#!/bin/bash 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 $!
$* 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 $LINENO $OLDPWD $OPTARG $PPID $PWD $REPLY is set by command read if no variable is given $SECONDS number of seconds since last start or login of the current shell
getopts delivers the options and even wrong parameters.
while getopts abc:D: opt do 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";; esac done
echo "the 20th argument: ${20}"
[ "$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 %
Example
$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) do 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 done Fahrer Michael_Foerster Nr 45 Punkte 74 Fahrer Frank_Sebastian_Haensch Nr 2 Punkte 72
$ 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`"
Farben:
0 black 1 blue 2 green 3 yellow 4 red 5 purple 6 cyan 7 grey
expr 2048 \* 2 4096 let Z=2*2048 echo $Z 4096
for (( I=1; i<1000 ; i++));do printf "%.2d.Zahl ist %10d\n" $i $RANDOM;done
select auswahl in Punkt1 Punkt2 Punkt3 Punkt4 do echo "wahl war: $auswahl" done
wait 4711
wait
trap './script' SIGUSR1 cat script ... kill -SIGUSR1 $$
jobs fg %jobnr bg %jobnr
seq -f '%05g' 3 seq --format='%05g' 3 00001 00002 00003
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 /home/user/asdf.txt
basename /home/user/asdf.txt
basename /home/user/asdf.txt .txt
With ulimit you can set the upper bound for resource use.
ulimit -a
ulimit -c 0
ulimit -f 512000
ulimit -S -n 250
ulimit -S -u 100
ulimit -S -v 50000