From efa24e5a24986a89311c0c864d766e25ab2630cb Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Oct 2018 14:00:42 -0400 Subject: [PATCH 1/3] Added string tricks, fixed a few typos --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7f5f26a..8d1f027 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,11 @@ 2. [Basic Shell Programming](#2-basic-shell-programming) 2.1. [Variables](#21-variables) 2.2. [Array](#22-array) - 2.3. [String Substitution](#23-string-substitution) - 2.4. [Functions](#24-functions) - 2.5. [Conditionals](#25-conditionals) - 2.6. [Loops](#26-loops) + 2.3. [String Substitution](#23-string-substitution) + 2.4. [Other String Tricks](#24-string-tricks) + 2.5. [Functions](#25-functions) + 2.6. [Conditionals](#26-conditionals) + 2.7. [Loops](#27-loops) 3. [Tricks](#3-tricks) 4. [Debugging](#4-debugging) @@ -998,7 +999,43 @@ ${variable//pattern/string} # the longest match to pattern in variable is replac ${#varname} # returns the length of the value of the variable as a character string ``` -## 2.4. Functions +## 2.4. Other String Tricks + +Bash has multiple shorthand tricks for doing various things to strings. + +```bash +${variable,,} #this converts every letter in the variable to lowercase +${variable^^} #this converts every letter in the variable to uppercase + +${variable:2:8} #this returns a substring of a string, starting at the character at the 2 index(strings start at index 0, so this is the 3rd character), + #the substring will be 8 characters long, so this would return a string made of the 3rd to the 11th characters. +``` + +Here are some handy pattern matching tricks +```bash +if [[ "$variable" == *subString* ]] #this returns true if the provided substring is in the variable +if [[ "$variable" != *subString* ]] #this returns true if the provided substring is not in the variable +if [[ "$variable" == subString* ]] #this returns true if the variable starts with the given subString +if [[ "$variable" == *subString ]] #this returns true if the variable ends with the given subString +``` + + +The above can be shortened using a case statement and the IN keyword +```bash +case "$var" in + begin*) + #variable begins with "begin" + ;; + *subString*) + #subString is in variable + ;; + + *otherSubString*) + #otherSubString is in variable + ;; +esac + +## 2.5. Functions As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name. ```bash @@ -1023,7 +1060,7 @@ say "hello world!" When you run the above example the `hello` function will output "world!". The above two functions `hello` and `say` are identical. The main difference is function `say`. This function, prints the first argument it receives. Arguments, within functions, are treated in the same manner as arguments given to the script. -## 2.5. Conditionals +## 2.6. Conditionals The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is `if` expression `then` statement where statement is only executed if expression is true. @@ -1083,7 +1120,7 @@ file1 -ot file2 # file1 is older than file2 -ne # not equal ``` -## 2.6. Loops +## 2.7. Loops There are three types of loops in bash. `for`, `while` and `until`. From 807e598c659be5762bdde73bb8c0d2ec1dda493e Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Oct 2018 14:03:27 -0400 Subject: [PATCH 2/3] Added string tricks, fixed a few typos --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8d1f027..f07eea6 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ 2. [Basic Shell Programming](#2-basic-shell-programming) 2.1. [Variables](#21-variables) 2.2. [Array](#22-array) - 2.3. [String Substitution](#23-string-substitution) - 2.4. [Other String Tricks](#24-string-tricks) - 2.5. [Functions](#25-functions) + 2.3. [String Substitution](#23-string-substitution) + 2.4. [Other String Tricks](#24-other-string-tricks) + 2.5. [Functions](#25-functions) 2.6. [Conditionals](#26-conditionals) 2.7. [Loops](#27-loops) 3. [Tricks](#3-tricks) @@ -1034,6 +1034,7 @@ case "$var" in #otherSubString is in variable ;; esac +``` ## 2.5. Functions As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name. From a93a9bc285eee7bb355ddc8148841752fd07a064 Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Oct 2018 14:04:26 -0400 Subject: [PATCH 3/3] Fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f07eea6..554e28c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 2.2. [Array](#22-array) 2.3. [String Substitution](#23-string-substitution) 2.4. [Other String Tricks](#24-other-string-tricks) - 2.5. [Functions](#25-functions) + 2.5. [Functions](#25-functions) 2.6. [Conditionals](#26-conditionals) 2.7. [Loops](#27-loops) 3. [Tricks](#3-tricks)