This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sw:bash:recipies:interpreter [2022/11/28 19:48] – created Frank Fegert | sw:bash:recipies:interpreter [2022/11/30 20:30] (current) – Frank Fegert | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Code interpretion ===== | ===== Code interpretion ===== | ||
- | Bash scripts are not compiled into executable binary files, but are rather fed into the '' | + | //Bash// scripts are not compiled into executable binary files, but are rather fed into the '' |
Example: | Example: | ||
Line 10: | Line 10: | ||
#!/bin/bash | #!/bin/bash | ||
echo "This line will be only printed once!" | echo "This line will be only printed once!" | ||
- | padded=$(printf ' | + | padded=$(printf ' |
- | echo " | + | echo " |
</ | </ | ||
Line 22: | Line 22: | ||
This line will be only printed once! | This line will be only printed once! | ||
[...] | [...] | ||
+ | </ | ||
+ | |||
+ | the above script rewrites itself, shifting the start of the script content to the position after the last previous content. Since the interpreter is currently at this position, it reads and executes the script from there again and again and again. | ||
+ | |||
+ | ===== Command groups ===== | ||
+ | |||
+ | //Bash// script code can use //command groups// to encapsulate any number of commands to be read and executed as a single unit. There are two seperate ways of //command groups//: | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code bash code_injection.sh> | ||
+ | #!/bin/bash | ||
+ | { | ||
+ | echo "This line will be only printed once!" | ||
+ | padded=$(printf ' | ||
+ | echo " | ||
+ | exit 0 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | When executed with: | ||
+ | |||
+ | <cli> | ||
+ | user@host: | ||
+ | This line will be only printed once! | ||
+ | user@host: | ||
</ | </ | ||
the above script rewrites itself, shifting the start of the script content to the position after the last previous content. | the above script rewrites itself, shifting the start of the script content to the position after the last previous content. | ||
- | ===== Code grouping | + | ===== Functions |
+ | |||
+ | Besides the previously mentioned //command groups//, //Bash// // | ||
+ | |||
+ | <code bash main.sh> | ||
+ | # | ||
+ | main() { | ||
+ | echo "This line will be only printed once!" | ||
+ | return 0 | ||
+ | } | ||
+ | |||
+ | { | ||
+ | main " | ||
+ | exit " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Functions in //Bash// do not use arguments like in other programming languages, but rather use positional parameters (e.g. '' | ||
+ | |||
+ | <code bash function_parameters.sh> | ||
+ | # | ||
+ | set -u | ||
+ | |||
+ | send_email() { | ||
+ | local address=" | ||
+ | local subject=" | ||
+ | local text=" | ||
+ | |||
+ | echo " | ||
+ | echo " | ||
+ | echo "and subject:" | ||
+ | echo " | ||
+ | echo "to recipient:" | ||
+ | echo " | ||
+ | return 0 | ||
+ | } | ||
+ | { | ||
+ | send_email " | ||
+ | exit " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Variables ===== | ||
+ | |||
+ | ==== Variable scope ==== | ||
+ | |||
+ | Variables in //Bash// are dynamically scoped, which means any variable *not* declared with '' | ||
+ | |||
+ | In //Bash// the use of undeclared variables is reported as an error, when the shell is started with the '' | ||
+ | |||
+ | ==== Return code masking ==== | ||
+ | |||
+ | When simultaniously declaring variables and assigning values to them special care must be taken, when the value to be assigned originates from a subshell (i.e. a command or a function) and the return code ('' | ||
+ | |||
+ | <code bash return_code_masking.sh> | ||
+ | # | ||
+ | set -u | ||
+ | |||
+ | file_contains() { | ||
+ | local file=" | ||
+ | local searchword=" | ||
+ | local result=$(grep -F " | ||
+ | if [[ $? != 0 ]]; then | ||
+ | # Nothing found | ||
+ | return 1 | ||
+ | fi | ||
+ | |||
+ | return 0 | ||
+ | } | ||
+ | { | ||
+ | file_contains "/ | ||
+ | exit " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | When executed with: | ||
+ | |||
+ | < | ||
+ | user@host: | ||
+ | 0 | ||
+ | </ | ||
+ | |||
+ | the script will return '' | ||
+ | |||
+ | <code bash return_code_masking.sh> | ||
+ | [...] | ||
+ | local result | ||
+ | result=$(grep -F " | ||
+ | [...] | ||
+ | </ | ||