If and Decision making is done

This commit is contained in:
Hizenberg469
2024-11-29 13:25:36 +05:30
parent 5759f3f807
commit 1ede16f3c4
4 changed files with 225 additions and 1 deletions

View File

@@ -335,4 +335,129 @@
* [[ $STRING == pattern_with_wildcards ]]
* [[ $STRING == file[0-9].txt ]]
* [[ $STRING == rich* ]]
* [[ $STRING == rich* ]]
-> Regular Expressions
|-----------------------------------------------------------|
| Symbol | Description | Example | Example matches |
|-----------------------------------------------------------|
| . | any single | bo.k | book,bo1k, ... |
| | character | | |
|-----------------------------------------------------------|
| * | preceeding | co*l | cl,col,cool, ...|
| | item must | | |
| | match zero | | |
| | or more | | |
| | times | | |
|-----------------------------------------------------------|
| ^ | Start of | ^hello | line starting |
| | the line | | with hello |
| | marker | | |
|-----------------------------------------------------------|
| $ | End of the | hello$ | line ending |
| | line marker | | with hello |
|-----------------------------------------------------------|
| [] | Any one of | coo[kl] | cook or cool |
| | of character| | |
| | enclosed in[]| | |
|-----------------------------------------------------------|
| [-] | Any character| file[1-3]| file1,file2,file3|
| | within the | | |
| | range | | |
|-----------------------------------------------------------|
| [^] | Any character| file[^0 | file8,file9 |
| | except those| 1234567]| |
| | enclosed in | | |
| | brackets | | |
|-----------------------------------------------------------|
| ? | preceeding | colou?r | color,colour |
| | item must | | NOT colouur |
| | one or zero | | |
| | times | | |
|-----------------------------------------------------------|
| + | preceeding | file1+ | file1,file11, |
| | item must | | file111 |
| | match one or | | NOT file |
| | more times | | |
|-----------------------------------------------------------|
| {n} | preceeding | [0-9]{3}| Any three digit:|
| | item must | | 097,256,787,... |
| | match n times | | |
|-----------------------------------------------------------|
| {n,} | Minimum number| [0-9]{3,}| Any three digit |
| | of times the | | 111,097,256,787.|
| | preceeding | | |
| | item should | | |
| | match | | |
|-----------------------------------------------------------|
| {n,m} | Minimum and | [0-9]{1,3}| 1,158,26, ... |
| | maximum number| | NOT 1258,1111,..|
| | of times the | | |
| | preceeding | | |
| | item should | | |
| | match | | |
|-----------------------------------------------------------|
| \ | Escape | hell\.o | hell.o |
| | character - | | NOT:helllo, |
| | escape any of | | hell1o... |
| | the special | | |
| | character, | | |
| | ignore the | | |
| | meaning of | | |
| | them | | |
|-----------------------------------------------------------|
-> Regex in Bash
* [[ $STRING =~ $REGEX ]]
* REGEX="http://.*\.jpg"
* ${BASH_REMATCH[0]} -> part of the STRING which match
REGEX (e.g. http://images.jpg)
* ${BASH_REMATCH[1]} -> part of the REGEX which is
enclosed in first parentheses,
e.g.
REGEX="http://(.*)\.jpg" -> which
would represents as "images"
-> Filesystem Related Tests
|-----------------------------------------------------------------------|
| File Test operators | Description, what is returned |
|-----------------------------------------------------------------------|
| [ -e $VAR ] | True if variable hold existing file (file |
| | or directory) |
|-----------------------------------------------------------------------|
| [ -f $VAR ] | True if variable holds an existing regular |
| | file |
|-----------------------------------------------------------------------|
| [ -d $VAR ] | True if variable holds an existing directory|
|-----------------------------------------------------------------------|
| [ -x $VAR ] | True if variable is an executable file |
|-----------------------------------------------------------------------|
| [ -L $VAR ] | True if variable holds th path of a symlink |
|-----------------------------------------------------------------------|
| [ -r $VAR ] | True if variable holds file that is readable|
|-----------------------------------------------------------------------|
| [ -w $VAR ] | True if variable holds file that is writable|
|-----------------------------------------------------------------------|
* Example:
if [ -f $FILE ]; then
echo File exists
else
echo File does not exists
fi
-> && and ||
*examples
[ $? -eq 0 ] && echo command OK || echo something went wrong
|-----------------| |
if true execute this |
if NOT true |
|--------------------|