Reference: |
Main /
Bash
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" historyhistory -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 ... fiif 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 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
-x 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 bracketsThe 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 $! 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 $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 Getting the options from command linegetopts 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
parameters beyond $9 echo "the 20th argument: ${20}"
comparison of numbers[ "$var1" = "$var2" ] [ "$var1" != "$var2" ] [ -z "$var2" ] # var empty? [ -n "$var3" ] # var not empty? printfprintf 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 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`" Farben: 0 black 1 blue 2 green 3 yellow 4 red 5 purple 6 cyan 7 grey calculating with bourne shellexpr 2048 \* 2 4096 let Z=2*2048 echo $Z 4096 random numbers with bashfor (( 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
do
echo "wahl war: $auswahl"
done
Wait for other processeswait 4711 waits for the process with PID 4711. wait returns 127 if the process no longer exists. Otherwise it is equal to the PID.
wait without any parameter wait waits on active child processes. Here the return value is always zero.
create user defined SIGNALtrap './script' SIGUSR1 cat script ... kill -SIGUSR1 $$ job managementjobs fg %jobnr bg %jobnr seq with formated stringseq -f '%05g' 3 seq --format='%05g' 3 00001 00002 00003 evalWith 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 basenamedirname /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.
ulimitWith ulimit you can set the upper bound for resource use. ulimit -a overview
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
|