Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
shell:if-for-while-function-variable [2025/09/16 15:15] – [IF/ELSE] tmadeshell:if-for-while-function-variable [2026/10/19 16:38] (current) – [Function] tmade
Line 37: Line 37:
  
 Common parameters (file): Common parameters (file):
- +  # 
-  -c +  -e, -a        #File exist
-  e, -a        #File exist+
   -d            #File is a directory/ path exists   -d            #File is a directory/ path exists
   -f            #File is a regular file   -f            #File is a regular file
Line 163: 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 "xyz" + "." + string-content:
 +
 +Example string: xyz.njknvjkgwrjkbhwje...njkruiehv
 +
 +<code>
 +if [[ $STRING =~ ^xyz\..{107}$  ]]; then
 +    echo "String starts with \"xyz\" and has 107 signs"
 +else
 +echo "String doesn´t match"
 +fi 
 +</code>
 ==== Array ==== ==== Array ====
  
Line 287: Line 297:
 sed -i "s/^\(;\|[0-9]\)/#disabled#/g" $i sed -i "s/^\(;\|[0-9]\)/#disabled#/g" $i
 done done
 +</code>
 +
 +Running a for loop from 1 - 10 "in steps of two":
 +<code>
 +for i in {1..10..2};do
 + echo $i
 +done
 +
 +Output:
 +1
 +3
 +5
 +7
 +9
 </code> </code>
 ==== While ==== ==== While ====
Line 385: Line 409:
       sleep 10       sleep 10
 done done
 +</code>
  
 +<code>
 +while IFS= read -r CONFLINE; do
 +    case "$CONFLINE" in
 +        'logfile ""')
 +            echo 'logfile "/var/log/redis/redis-server.log"'
 +            ;;
 +        *)
 +            echo "$CONFLINE"
 +            ;;
 +    esac
 +done < /etc/redis/redis.conf > /tmp/redis.conf
 </code> </code>
  
 +Check, when a folder will be created:
  
 +<code>
 +#!/bin/bash
 +
 +# Path to folder
 +FOLDER="/path/to/folder"
 +
 +echo "Wait until: $FOLDER"
 +
 +# ! -d prüft, ob das Verzeichnis NOCH NICHT existiert
 +while [ ! -d "$FOLDER" ]; do
 +    sleep 1 # waiting....
 +done
 +
 +echo "Folder $FOLDER found! Loop terminated."
 +</code>
 ==== Function ==== ==== Function ====
  
Line 413: Line 465:
 fi fi
 </code> </code>
 +
 +<code>
 +#!/bin/bash
 +
 +function test {
 +echo $1
 +echo $2
 +}
 +
 +test first second
 +</code>
 +
 +Output:
 +  first
 +  second
 +
 +
 +
  
 Example: Example:
shell/if-for-while-function-variable.1758028503.txt.gz · Last modified: by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki