mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 13:52:24 +03:00
If and Decision making is done
This commit is contained in:
@@ -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 |
|
||||
|--------------------|
|
||||
|
||||
|
||||
49
filetest.sh
Normal file
49
filetest.sh
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
|
||||
#for provided file print the summary of what permissions users has granted
|
||||
#example: ./filetest.sh hello.txt
|
||||
#READ: YES
|
||||
#WRITE: NO
|
||||
#EXECUTABLE:NO
|
||||
|
||||
#argument check
|
||||
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Provide exactly one argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#variable assignment
|
||||
FILE=$1
|
||||
|
||||
#check if the file exists
|
||||
if [ -f $FILE ]; then
|
||||
|
||||
#default variables
|
||||
VAR_READ="NO"
|
||||
VAR_WRITE="NO"
|
||||
VAR_EXEC="NO"
|
||||
|
||||
#check if file is readable
|
||||
if [ -r $FILE ]; then
|
||||
VAR_READ="YES"
|
||||
fi
|
||||
|
||||
#check if file is writable
|
||||
if [ -w $FILE ]; then
|
||||
VAR_WRITE="YES"
|
||||
fi
|
||||
|
||||
#check if file is executable
|
||||
if [ -x $FILE ]; then
|
||||
VAR_EXEC="YES"
|
||||
fi
|
||||
|
||||
#write permissions summary to user
|
||||
echo "==FILE: $FILE=="
|
||||
echo "READ: $VAR_READ"
|
||||
echo "WRITE: $VAR_WRITE"
|
||||
echo "EXECUTABLE: $VAR_EXEC"
|
||||
else
|
||||
echo $FILE does not exists
|
||||
fi
|
||||
38
regex.sh
Normal file
38
regex.sh
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
#provide one word/sentence as an argument to the script.
|
||||
#If in that sentence will be ip addresss, find out, if that
|
||||
#ip address is reachable or not.
|
||||
|
||||
#argument check
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Provide exactly one argument e.g. $0 argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VAR1=$1
|
||||
#ip address regex
|
||||
REGEX="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[[:digit:]]{1,3}"
|
||||
|
||||
#regex check
|
||||
if ! [[ $VAR1 =~ $REGEX ]]; then
|
||||
echo "No IP address provided"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
IP=${BASH_REMATCH[0]}
|
||||
|
||||
#find if ip address is reachable or not
|
||||
ping -c4 $IP 1>/dev/null
|
||||
|
||||
#1>/dev/null
|
||||
#It will redirect standard output and prevent
|
||||
#any output to print on screen.
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
STATUS="ALIVE"
|
||||
else
|
||||
STATUS="DEAD"
|
||||
fi
|
||||
|
||||
echo "IP found: $IP ($STATUS)"
|
||||
12
secret.sh
Normal file
12
secret.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo -n Your age:
|
||||
read AGE
|
||||
|
||||
#Problem with output as () cause to execute command in subshell.
|
||||
#[ $AGE -lt 20 ] && (echo You are not allowed to see secret; exit 1) || echo Welcome
|
||||
|
||||
#command execute it same shell
|
||||
[ $AGE -lt 20 ] && { echo You are not allowed to see secret; exit 1;} || echo Welcome
|
||||
|
||||
echo Secret is that there is no secret
|
||||
Reference in New Issue
Block a user