This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| shell:if-for-while-function-variable [2024/01/30 13:15] – tmade | shell:if-for-while-function-variable [2026/10/19 16:38] (current) – [Function] tmade | ||
|---|---|---|---|
| Line 37: | Line 37: | ||
| Common parameters (file): | Common parameters (file): | ||
| - | + | # | |
| - | e, -a | + | |
| + | -d #File is a directory/ path exists | ||
| -f #File is a regular file | -f #File is a regular file | ||
| - | -d #File is a directory | ||
| -s #File is non-zero size | -s #File is non-zero size | ||
| -h, -L #File is a symbolic link | -h, -L #File is a symbolic link | ||
| Line 73: | Line 73: | ||
| echo " | echo " | ||
| fi | fi | ||
| + | </ | ||
| + | |||
| + | ==Various conditions== | ||
| + | |||
| + | < | ||
| + | if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ] | ||
| + | | ||
| + | |||
| + | or like | ||
| + | |||
| + | if [[ -f $VAR1 && -f $VAR2 && -f $VAR3 ]] | ||
| + | | ||
| + | |||
| + | or even | ||
| + | |||
| + | if [ -f $VAR1 -a -f $VAR2 -a -f $VAR3 ] | ||
| + | | ||
| </ | </ | ||
| Line 97: | Line 114: | ||
| # In case none of the above occurs | # In case none of the above occurs | ||
| fi | fi | ||
| - | </ | ||
| - | |||
| - | Various conditions: | ||
| - | |||
| - | < | ||
| - | if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ] | ||
| - | | ||
| - | |||
| - | or like | ||
| - | |||
| - | if [[ -f $VAR1 && -f $VAR2 && -f $VAR3 ]] | ||
| - | | ||
| - | |||
| - | or even | ||
| - | |||
| - | if [ -f $VAR1 -a -f $VAR2 -a -f $VAR3 ] | ||
| - | | ||
| </ | </ | ||
| Line 153: | Line 153: | ||
| =, == #Is equal to | =, == #Is equal to | ||
| - | =~ # | + | =~ #advanced regular expression compare |
| != #Not equal to | != #Not equal to | ||
| > #Greater than | > #Greater than | ||
| Line 162: | Line 162: | ||
| Note: To check other parameters use "man test", which uses same syntax! | Note: To check other parameters use "man test", which uses same syntax! | ||
| + | Check string starts with " | ||
| + | |||
| + | Example string: xyz.njknvjkgwrjkbhwje...njkruiehv | ||
| + | |||
| + | < | ||
| + | if [[ $STRING =~ ^xyz\..{107}$ | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | </ | ||
| ==== Array ==== | ==== Array ==== | ||
| Line 182: | Line 193: | ||
| echo ${array[0]} | echo ${array[0]} | ||
| + | |||
| + | All entries: | ||
| + | |||
| + | echo ${array[*]} | ||
| | | ||
| Line 276: | Line 291: | ||
| for i in $(ls .pro*); do echo $i; done | for i in $(ls .pro*); do echo $i; done | ||
| + | < | ||
| + | test="/ | ||
| + | for i in ${test} ; do | ||
| + | ls $i | ||
| + | sed -i " | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | Running a for loop from 1 - 10 "in steps of two": | ||
| + | < | ||
| + | for i in {1..10..2}; | ||
| + | echo $i | ||
| + | done | ||
| + | |||
| + | Output: | ||
| + | 1 | ||
| + | 3 | ||
| + | 5 | ||
| + | 7 | ||
| + | 9 | ||
| + | </ | ||
| ==== While ==== | ==== While ==== | ||
| Infinite loop: | Infinite loop: | ||
| Line 373: | Line 409: | ||
| sleep 10 | sleep 10 | ||
| done | done | ||
| + | </ | ||
| + | < | ||
| + | while IFS= read -r CONFLINE; do | ||
| + | case " | ||
| + | ' | ||
| + | echo ' | ||
| + | ;; | ||
| + | *) | ||
| + | echo " | ||
| + | ;; | ||
| + | esac | ||
| + | done < / | ||
| </ | </ | ||
| + | Check, when a folder will be created: | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # Path to folder | ||
| + | FOLDER="/ | ||
| + | |||
| + | echo "Wait until: $FOLDER" | ||
| + | |||
| + | # ! -d prüft, ob das Verzeichnis NOCH NICHT existiert | ||
| + | while [ ! -d " | ||
| + | sleep 1 # waiting.... | ||
| + | done | ||
| + | |||
| + | echo " | ||
| + | </ | ||
| ==== Function ==== | ==== Function ==== | ||
| Line 401: | Line 465: | ||
| fi | fi | ||
| </ | </ | ||
| + | |||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | function test { | ||
| + | echo $1 | ||
| + | echo $2 | ||
| + | } | ||
| + | |||
| + | test first second | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | first | ||
| + | second | ||
| + | |||
| + | |||
| + | |||
| Example: | Example: | ||