From 539552d5069ee1a7651412fd08b607798a709113 Mon Sep 17 00:00:00 2001 From: samirm00 Date: Fri, 15 Mar 2024 19:06:17 +0100 Subject: [PATCH] add 02-variables-and-shell-expansions --- 00-build-bash-script/0-examples/00.sh | 7 +- 00-build-bash-script/0-examples/01.sh | 12 +-- 00-build-bash-script/0-examples/02.md | 4 +- 00-build-bash-script/0-examples/02.sh | 11 ++- 00-build-bash-script/0-examples/03.sh | 11 +-- 00-build-bash-script/0-examples/04.sh | 16 ++-- 00-build-bash-script/0-examples/05.sh | 14 ++-- 00-build-bash-script/0-examples/06.sh | 9 +-- 00-build-bash-script/1-blanks/00.sh | 12 +-- 00-build-bash-script/1-blanks/01.sh | 19 +---- 00-build-bash-script/1-blanks/02.sh | 12 +-- 00-build-bash-script/1-blanks/03.sh | 17 ++-- 00-build-bash-script/1-blanks/04.sh | 14 ++-- 00-build-bash-script/1-blanks/05.sh | 11 +-- 00-build-bash-script/1-blanks/06.sh | 11 +-- 00-build-bash-script/2-bugs/00.sh | 8 +- 00-build-bash-script/2-bugs/01.sh | 15 +--- 00-build-bash-script/2-bugs/02.sh | 12 +-- 00-build-bash-script/2-bugs/03.sh | 16 +--- 00-build-bash-script/2-bugs/04.sh | 15 ++-- 00-build-bash-script/2-bugs/05.sh | 16 ++-- 00-build-bash-script/2-bugs/06.sh | 11 +-- 00-build-bash-script/3-refactor/00.sh | 10 +-- 00-build-bash-script/3-refactor/01.sh | 21 +---- 00-build-bash-script/3-refactor/02.sh | 15 +--- 00-build-bash-script/3-refactor/03.sh | 14 +--- 00-build-bash-script/3-refactor/04.sh | 13 +--- 00-build-bash-script/3-refactor/05.sh | 16 ++-- 00-build-bash-script/3-refactor/06.sh | 13 +--- 00-build-bash-script/4-modify/00.sh | 14 +--- 00-build-bash-script/4-modify/01.sh | 17 +--- 00-build-bash-script/4-modify/02.sh | 18 ++--- 00-build-bash-script/4-modify/03.sh | 16 +--- 00-build-bash-script/4-modify/04.sh | 12 +-- 00-build-bash-script/4-modify/05.sh | 13 +--- 00-build-bash-script/4-modify/06.sh | 10 +-- 00-build-bash-script/5-chunks/00.sh | 10 +-- 00-build-bash-script/5-chunks/01.sh | 16 +--- 00-build-bash-script/5-chunks/02.sh | 11 +-- 00-build-bash-script/5-chunks/03.sh | 12 +-- 00-build-bash-script/5-chunks/04.sh | 10 +-- 00-build-bash-script/5-chunks/05.sh | 13 +--- 00-build-bash-script/5-chunks/06.sh | 9 +-- 00-build-bash-script/6-blank-pages/00.sh | 14 +++- 00-build-bash-script/6-blank-pages/01.sh | 12 ++- 00-build-bash-script/6-blank-pages/02.sh | 13 +++- 00-build-bash-script/6-blank-pages/03.sh | 12 ++- 00-build-bash-script/6-blank-pages/04.sh | 14 +++- 00-build-bash-script/6-blank-pages/05.sh | 13 +++- 00-build-bash-script/6-blank-pages/06.sh | 13 +++- 01-debug-your-script/0-examples/00.md | 23 ++++++ 01-debug-your-script/0-examples/01.sh | 4 +- 01-debug-your-script/0-examples/02.sh | 4 +- 01-debug-your-script/0-examples/03.sh | 4 +- 01-debug-your-script/0-examples/04.sh | 4 +- 01-debug-your-script/0-examples/05.sh | 4 +- 01-debug-your-script/1-blanks/01.sh | 4 +- 01-debug-your-script/1-blanks/02.sh | 2 +- 01-debug-your-script/1-blanks/03.sh | 2 +- 01-debug-your-script/1-blanks/04.sh | 2 +- 01-debug-your-script/1-blanks/05.sh | 2 +- 01-debug-your-script/2-bugs/01.sh | 2 +- 01-debug-your-script/2-bugs/02.sh | 2 +- 01-debug-your-script/2-bugs/03.sh | 2 +- 01-debug-your-script/2-bugs/04.sh | 2 +- 01-debug-your-script/2-bugs/05.sh | 3 +- 01-debug-your-script/3-refactor/01.sh | 2 +- 01-debug-your-script/3-refactor/02.sh | 2 +- 01-debug-your-script/3-refactor/03.sh | 2 +- 01-debug-your-script/3-refactor/04.sh | 2 +- 01-debug-your-script/3-refactor/05.sh | 2 +- 01-debug-your-script/4-modify/01.sh | 7 +- 01-debug-your-script/4-modify/02.sh | 2 +- 01-debug-your-script/4-modify/03.sh | 2 +- 01-debug-your-script/4-modify/04.sh | 2 +- 01-debug-your-script/4-modify/05.sh | 2 +- 01-debug-your-script/5-chunks/01.sh | 4 +- 01-debug-your-script/5-chunks/02.sh | 4 +- 01-debug-your-script/5-chunks/03.sh | 4 +- 01-debug-your-script/5-chunks/04.sh | 4 +- 01-debug-your-script/5-chunks/05.sh | 4 +- 01-debug-your-script/6-blank-pages/01.sh | 12 +-- 01-debug-your-script/6-blank-pages/02.sh | 12 +-- 01-debug-your-script/6-blank-pages/03.sh | 4 +- 01-debug-your-script/6-blank-pages/04.sh | 2 +- 01-debug-your-script/6-blank-pages/05.sh | 4 +- .../0-examples/01.md | 77 +++++++++++++++++++ .../0-examples/01.sh | 41 ++++++++++ .../{ => 0-examples}/02.sh | 16 ++-- .../{ => 0-examples}/03.sh | 10 +-- .../0-examples/04.sh | 22 ++++++ .../0-examples/05.sh | 28 +++++++ .../0-examples/06.md | 71 +++++++++++++++++ .../{04.sh => 0-examples/06.sh} | 12 ++- .../{05.sh => 0-examples/07.sh} | 14 ++-- .../0-examples/08.sh | 37 +++++++++ .../{08.sh => 0-examples/09.sh} | 12 +-- .../{09.sh => 0-examples/10.sh} | 17 ++-- .../{10.sh => 0-examples/11.sh} | 9 ++- 02-variables-and-shell-expansions/01.sh | 20 ----- 02-variables-and-shell-expansions/06.sh | 32 -------- 02-variables-and-shell-expansions/07.sh | 37 --------- .../1-blanks/01.sh | 41 ++++++++++ .../1-blanks/02.sh | 37 +++++++++ .../1-blanks/03.sh | 56 ++++++++++++++ .../1-blanks/04.sh | 22 ++++++ .../1-blanks/05.sh | 28 +++++++ .../1-blanks/06.sh | 44 +++++++++++ .../1-blanks/07.sh | 34 ++++++++ .../1-blanks/08.sh | 37 +++++++++ .../1-blanks/09.sh | 26 +++++++ .../1-blanks/10.sh | 28 +++++++ .../1-blanks/11.sh | 40 ++++++++++ .../2-bugs/01.sh | 41 ++++++++++ .../2-bugs/02.sh | 37 +++++++++ .../2-bugs/03.sh | 56 ++++++++++++++ .../2-bugs/04.sh | 22 ++++++ .../2-bugs/05.sh | 28 +++++++ .../2-bugs/06.sh | 44 +++++++++++ .../2-bugs/07.sh | 34 ++++++++ .../2-bugs/08.sh | 37 +++++++++ .../2-bugs/09.sh | 26 +++++++ .../2-bugs/10.sh | 28 +++++++ .../2-bugs/11.sh | 40 ++++++++++ .../3-refactor/01.sh | 41 ++++++++++ .../3-refactor/02.sh | 37 +++++++++ .../3-refactor/03.sh | 56 ++++++++++++++ .../3-refactor/04.sh | 22 ++++++ .../3-refactor/05.sh | 28 +++++++ .../3-refactor/06.sh | 44 +++++++++++ .../3-refactor/07.sh | 34 ++++++++ .../3-refactor/08.sh | 37 +++++++++ .../3-refactor/09.sh | 26 +++++++ .../3-refactor/10.sh | 28 +++++++ .../3-refactor/11.sh | 40 ++++++++++ .../4-modify/01.sh | 41 ++++++++++ .../4-modify/02.sh | 37 +++++++++ .../4-modify/03.sh | 56 ++++++++++++++ .../4-modify/04.sh | 22 ++++++ .../4-modify/05.sh | 28 +++++++ .../4-modify/06.sh | 44 +++++++++++ .../4-modify/07.sh | 34 ++++++++ .../4-modify/08.sh | 37 +++++++++ .../4-modify/09.sh | 26 +++++++ .../4-modify/10.sh | 28 +++++++ .../4-modify/11.sh | 40 ++++++++++ .../5-chunks/01.sh | 41 ++++++++++ .../5-chunks/02.sh | 37 +++++++++ .../5-chunks/03.sh | 56 ++++++++++++++ .../5-chunks/04.sh | 22 ++++++ .../5-chunks/05.sh | 28 +++++++ .../5-chunks/06.sh | 44 +++++++++++ .../5-chunks/07.sh | 34 ++++++++ .../5-chunks/08.sh | 37 +++++++++ .../5-chunks/09.sh | 26 +++++++ .../5-chunks/10.sh | 28 +++++++ .../5-chunks/11.sh | 40 ++++++++++ .../6-blank-pages/01.sh | 41 ++++++++++ .../6-blank-pages/02.sh | 37 +++++++++ .../6-blank-pages/03.sh | 56 ++++++++++++++ .../6-blank-pages/04.sh | 22 ++++++ .../6-blank-pages/05.sh | 28 +++++++ .../6-blank-pages/06.sh | 44 +++++++++++ .../6-blank-pages/07.sh | 34 ++++++++ .../6-blank-pages/08.sh | 37 +++++++++ .../6-blank-pages/09.sh | 26 +++++++ .../6-blank-pages/10.sh | 28 +++++++ .../6-blank-pages/11.sh | 40 ++++++++++ README.md | 4 +- 169 files changed, 3008 insertions(+), 601 deletions(-) create mode 100644 02-variables-and-shell-expansions/0-examples/01.md create mode 100755 02-variables-and-shell-expansions/0-examples/01.sh rename 02-variables-and-shell-expansions/{ => 0-examples}/02.sh (69%) mode change 100755 => 100644 rename 02-variables-and-shell-expansions/{ => 0-examples}/03.sh (88%) mode change 100755 => 100644 create mode 100755 02-variables-and-shell-expansions/0-examples/04.sh create mode 100755 02-variables-and-shell-expansions/0-examples/05.sh create mode 100644 02-variables-and-shell-expansions/0-examples/06.md rename 02-variables-and-shell-expansions/{04.sh => 0-examples/06.sh} (83%) rename 02-variables-and-shell-expansions/{05.sh => 0-examples/07.sh} (74%) create mode 100755 02-variables-and-shell-expansions/0-examples/08.sh rename 02-variables-and-shell-expansions/{08.sh => 0-examples/09.sh} (60%) rename 02-variables-and-shell-expansions/{09.sh => 0-examples/10.sh} (62%) mode change 100755 => 100644 rename 02-variables-and-shell-expansions/{10.sh => 0-examples/11.sh} (80%) mode change 100755 => 100644 delete mode 100755 02-variables-and-shell-expansions/01.sh delete mode 100755 02-variables-and-shell-expansions/06.sh delete mode 100755 02-variables-and-shell-expansions/07.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/01.sh create mode 100644 02-variables-and-shell-expansions/1-blanks/02.sh create mode 100644 02-variables-and-shell-expansions/1-blanks/03.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/04.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/05.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/06.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/07.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/08.sh create mode 100755 02-variables-and-shell-expansions/1-blanks/09.sh create mode 100644 02-variables-and-shell-expansions/1-blanks/10.sh create mode 100644 02-variables-and-shell-expansions/1-blanks/11.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/01.sh create mode 100644 02-variables-and-shell-expansions/2-bugs/02.sh create mode 100644 02-variables-and-shell-expansions/2-bugs/03.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/04.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/05.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/06.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/07.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/08.sh create mode 100755 02-variables-and-shell-expansions/2-bugs/09.sh create mode 100644 02-variables-and-shell-expansions/2-bugs/10.sh create mode 100644 02-variables-and-shell-expansions/2-bugs/11.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/01.sh create mode 100644 02-variables-and-shell-expansions/3-refactor/02.sh create mode 100644 02-variables-and-shell-expansions/3-refactor/03.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/04.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/05.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/06.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/07.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/08.sh create mode 100755 02-variables-and-shell-expansions/3-refactor/09.sh create mode 100644 02-variables-and-shell-expansions/3-refactor/10.sh create mode 100644 02-variables-and-shell-expansions/3-refactor/11.sh create mode 100755 02-variables-and-shell-expansions/4-modify/01.sh create mode 100644 02-variables-and-shell-expansions/4-modify/02.sh create mode 100644 02-variables-and-shell-expansions/4-modify/03.sh create mode 100755 02-variables-and-shell-expansions/4-modify/04.sh create mode 100755 02-variables-and-shell-expansions/4-modify/05.sh create mode 100755 02-variables-and-shell-expansions/4-modify/06.sh create mode 100755 02-variables-and-shell-expansions/4-modify/07.sh create mode 100755 02-variables-and-shell-expansions/4-modify/08.sh create mode 100755 02-variables-and-shell-expansions/4-modify/09.sh create mode 100644 02-variables-and-shell-expansions/4-modify/10.sh create mode 100644 02-variables-and-shell-expansions/4-modify/11.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/01.sh create mode 100644 02-variables-and-shell-expansions/5-chunks/02.sh create mode 100644 02-variables-and-shell-expansions/5-chunks/03.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/04.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/05.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/06.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/07.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/08.sh create mode 100755 02-variables-and-shell-expansions/5-chunks/09.sh create mode 100644 02-variables-and-shell-expansions/5-chunks/10.sh create mode 100644 02-variables-and-shell-expansions/5-chunks/11.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/01.sh create mode 100644 02-variables-and-shell-expansions/6-blank-pages/02.sh create mode 100644 02-variables-and-shell-expansions/6-blank-pages/03.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/04.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/05.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/06.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/07.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/08.sh create mode 100755 02-variables-and-shell-expansions/6-blank-pages/09.sh create mode 100644 02-variables-and-shell-expansions/6-blank-pages/10.sh create mode 100644 02-variables-and-shell-expansions/6-blank-pages/11.sh diff --git a/00-build-bash-script/0-examples/00.sh b/00-build-bash-script/0-examples/00.sh index 418ffb7..f6a6bfa 100755 --- a/00-build-bash-script/0-examples/00.sh +++ b/00-build-bash-script/0-examples/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version : 1.0 # Description: The main components of a shell script # Usage: ./00.sh @@ -16,9 +16,10 @@ # 3. Exit status will be at the end of the script -exit 0 #-------------------------------------------------------------# +exit 0 + # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` diff --git a/00-build-bash-script/0-examples/01.sh b/00-build-bash-script/0-examples/01.sh index dd83ef8..1b1e045 100755 --- a/00-build-bash-script/0-examples/01.sh +++ b/00-build-bash-script/0-examples/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version : 1.0 # Description: echo command # Usage: ./01.sh @@ -18,16 +18,10 @@ echo -e "Hello,\tWorld! third example" # What is the difference between the three echo commands above? -exit 0 - #-------------------------------------------------------------# +exit 0 # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` # 3. `./` - - - - - diff --git a/00-build-bash-script/0-examples/02.md b/00-build-bash-script/0-examples/02.md index c0f4000..93ea903 100755 --- a/00-build-bash-script/0-examples/02.md +++ b/00-build-bash-script/0-examples/02.md @@ -2,7 +2,7 @@ ## Introduction -The `$SHELL` variable in Unix-like operating systems is an environment variable that holds the path to the user's preferred shell. +The `$SHELL` variable in Unix-based and Unix-like operating systems is an environment variable that holds the path to the user's default shell. ## Usage @@ -38,5 +38,5 @@ This command sets the default shell to Zsh, for example. When working with the `$SHELL` variable: - Ensure compatibility by using the appropriate syntax for the current shell. -- Use the `$SHELL` variable dynamically in scripts to adapt to users' preferences. +- Use the `$SHELL` variable dynamically in scripts to adapt to user's preferences. - Be cautious when changing the default shell, considering compatibility with existing scripts and configurations. diff --git a/00-build-bash-script/0-examples/02.sh b/00-build-bash-script/0-examples/02.sh index 416a1d6..e883db0 100755 --- a/00-build-bash-script/0-examples/02.sh +++ b/00-build-bash-script/0-examples/02.sh @@ -1,14 +1,14 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The $SHELL varibale # Usage: ./02.sh #-------------------------------------------------------------# -# $SHELL is a shell variable that contains the path to the current shell +# $SHELL is a shell variable that contains the path to the default shell echo "$SHELL" @@ -19,11 +19,10 @@ echo "Your default shell is {$SHELL}" # Can I change the default shell? How? # Are there any other shell variables? -exit 0 - #-------------------------------------------------------------# +exit 0 # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +# 3. `./` diff --git a/00-build-bash-script/0-examples/03.sh b/00-build-bash-script/0-examples/03.sh index 03ed4f4..5b84568 100755 --- a/00-build-bash-script/0-examples/03.sh +++ b/00-build-bash-script/0-examples/03.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Comments in bash script. # Usage: ./02.sh @@ -12,21 +12,18 @@ # Single line comment. - # Multi-line comment. # Multi-line comment. # Multi-line comment. echo "Comments in bash script." # Inline comment. -# Why the shebang is not a comment? - -exit 0 +# is the shebang a comment? #-------------------------------------------------------------# +exit 0 # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` # 3. `./` - diff --git a/00-build-bash-script/0-examples/04.sh b/00-build-bash-script/0-examples/04.sh index 95f3583..33c800d 100644 --- a/00-build-bash-script/0-examples/04.sh +++ b/00-build-bash-script/0-examples/04.sh @@ -2,7 +2,7 @@ # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Make your script more professional. # Usage: ./04.sh @@ -10,16 +10,16 @@ # To make your script more professional, you can use 5 pecies of information -# 1. Author : the name of the person who created the script -# 2. Date of creation : the date when the script was created -# 3. Modified date : the date when the script was last modified -# 4. Description : what the script does -# 5. Usage : how to use the script +# 1. Author : the name of the person who created the script +# 2. Date : the date when the script was created +# 3. Version : the version of the script +# 4. Description : a brief description of the script +# 5. Usage : how to use the script +#-------------------------------------------------------------# exit 0 -#-------------------------------------------------------------# # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +# 3. `./` diff --git a/00-build-bash-script/0-examples/05.sh b/00-build-bash-script/0-examples/05.sh index a229ae9..c8b1d63 100755 --- a/00-build-bash-script/0-examples/05.sh +++ b/00-build-bash-script/0-examples/05.sh @@ -1,34 +1,32 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Add and remove scripts to/from the PATH # Usage: ./05.sh #-------------------------------------------------------------# - # To see the PATH variable, you can use the following command echo "{$PATH}" # Add scripts to the PATH allows you to run the script from any directory without specifying the full path ## To add 0-examples to the PATH, you can use the following command (replace the path with your own) -export PATH=$PATH:$HOME/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples +export PATH=$PATH:$HOME/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples echo "{$PATH}" ## To remove 0-examples from the PATH, you can use the following command (replace the path with your own) -export PATH=${PATH%:"$HOME"/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples} +export PATH=${PATH%:"$HOME"/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples} echo "{$PATH}" -exit 0 - #-------------------------------------------------------------# +exit 0 # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +# 3. `./` diff --git a/00-build-bash-script/0-examples/06.sh b/00-build-bash-script/0-examples/06.sh index 98e2cb6..830c79a 100755 --- a/00-build-bash-script/0-examples/06.sh +++ b/00-build-bash-script/0-examples/06.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Set up secure script permissions # Usage: ./06.sh @@ -29,11 +29,10 @@ ls -l 06.sh # chmod 744 .sh # 744 means that the owner has read, write and execute permission, and the group and public have only read permission -exit 0 - #-------------------------------------------------------------# +exit 0 # How to run the script: # 1. Open the terminal # 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +# 3. `./` diff --git a/00-build-bash-script/1-blanks/00.sh b/00-build-bash-script/1-blanks/00.sh index 434e4b6..d25d888 100755 --- a/00-build-bash-script/1-blanks/00.sh +++ b/00-build-bash-script/1-blanks/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The main components of a shell script # Usage: _/__.sh @@ -21,11 +21,5 @@ # The exit status is a ________ value that a script returns upon completion. # Typically, ___ indicates success and non _____ indicates failure. -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` - +exit 0 diff --git a/00-build-bash-script/1-blanks/01.sh b/00-build-bash-script/1-blanks/01.sh index 07d25e2..baff6f2 100755 --- a/00-build-bash-script/1-blanks/01.sh +++ b/00-build-bash-script/1-blanks/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # _______: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: echo command # ______: ./01.sh @@ -10,7 +10,7 @@ echo "__________ first example" # Hello, World! first example -echo -e "__________ second example" +echo -e "__________ second example" # Hello, # World! second example @@ -18,16 +18,5 @@ echo -e "__________ third example" # Hello, World! third example # What is the difference between the three echo commands above? -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` - - - - - - +exit 0 diff --git a/00-build-bash-script/1-blanks/02.sh b/00-build-bash-script/1-blanks/02.sh index c23d94c..f84cb70 100755 --- a/00-build-bash-script/1-blanks/02.sh +++ b/00-build-bash-script/1-blanks/02.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: __________ # Date: __________ -# Modified: __________ +# Version: __________ # Description: The $SHELL variable # Usage: ./___.sh @@ -19,11 +19,5 @@ echo "Your default shell is {________}" # Can I change the default shell? How? # Are there any other shell variables? -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` +exit 0 diff --git a/00-build-bash-script/1-blanks/03.sh b/00-build-bash-script/1-blanks/03.sh index e2a688d..f8b8078 100755 --- a/00-build-bash-script/1-blanks/03.sh +++ b/00-build-bash-script/1-blanks/03.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: __________ # Date: __________ -# Modified: __________ +# Version: __________ # Description: Comments in bash script. # Usage: ./___.sh @@ -21,15 +21,8 @@ echo "Comments in bash script." # Inline comment. # Inline comments start with the ________ symbol and extend to the end of the line. -# Why the shebang is not a comment? -# The shebang is not a comment because it is ________ by the shell to determine the ________ to be used for executing the script. +# Is shebang a comment? +# The shebang is a special comment because it is ________ by the shell to determine the ________ to be used for executing the script. -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` - - +exit 0 diff --git a/00-build-bash-script/1-blanks/04.sh b/00-build-bash-script/1-blanks/04.sh index 556fe49..8a750bc 100644 --- a/00-build-bash-script/1-blanks/04.sh +++ b/00-build-bash-script/1-blanks/04.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: __________ # Date: __________ -# Modified: __________ +# Version: __________ # Description: Make your script more professional. # Usage: ./___.sh @@ -12,13 +12,9 @@ # 1. __________ : the name of the person who created the script # 2. __________ : the date when the script was created -# 3. __________ : the date when the script was last modified +# 3. __________ : the version of the script # 4. __________ : what the script does -# 5. __________ : how to use the script +# 5. __________ : how to use the script -exit 0 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` +exit 0 diff --git a/00-build-bash-script/1-blanks/05.sh b/00-build-bash-script/1-blanks/05.sh index 57df926..38dc45c 100755 --- a/00-build-bash-script/1-blanks/05.sh +++ b/00-build-bash-script/1-blanks/05.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: __________ # Date: __________ -# Modified: __________ +# Version: __________ # Description: Add and remove scripts to/from the PATH # Usage: ./___.sh @@ -23,10 +23,5 @@ export PATH=${PATH%:"________"} echo "{$PATH}" -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` +exit 0 diff --git a/00-build-bash-script/1-blanks/06.sh b/00-build-bash-script/1-blanks/06.sh index 836b222..078d42b 100755 --- a/00-build-bash-script/1-blanks/06.sh +++ b/00-build-bash-script/1-blanks/06.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: __________ # Date: __________ -# Modified: __________ +# Version: __________ # Description: Set up secure script permissions # Usage: ./___.sh @@ -29,10 +29,5 @@ ls -l ___.sh # chmod ________ .sh # ________ means that the owner has read, write and execute permission, and the group and public have only read permission -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the ________ -# 2. `chmod ___ ` -# 3. `./` +exit 0 diff --git a/00-build-bash-script/2-bugs/00.sh b/00-build-bash-script/2-bugs/00.sh index 579d0bb..4a95c68 100755 --- a/00-build-bash-script/2-bugs/00.sh +++ b/00-build-bash-script/2-bugs/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The main components of a shell script # Usage: ./00.sh @@ -11,7 +11,5 @@ # echo the $SHELL variable echo "{SHELL}" -exit 1 - #-------------------------------------------------------------# - +exit 1 diff --git a/00-build-bash-script/2-bugs/01.sh b/00-build-bash-script/2-bugs/01.sh index c56c026..8bace0c 100755 --- a/00-build-bash-script/2-bugs/01.sh +++ b/00-build-bash-script/2-bugs/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: echo command # Usage: ./01.sh @@ -12,15 +12,8 @@ echo "Hello, World! first example" -echo "Hello,\nWorld! second example" +echo "Hello,\nWorld! second example" -echo "Hello,\tWorld! third example" +echo "Hello,\tWorld! third example" #-------------------------------------------------------------# - - - - - - - diff --git a/00-build-bash-script/2-bugs/02.sh b/00-build-bash-script/2-bugs/02.sh index 20c6d7b..e38d0d9 100755 --- a/00-build-bash-script/2-bugs/02.sh +++ b/00-build-bash-script/2-bugs/02.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The $SHELL varibale # Usage: ./02.sh @@ -17,11 +17,5 @@ echo "Your default shell is {SHELL}" # Can I change the default shell? How? # Are there any other shell variables? -exit 10 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 10 diff --git a/00-build-bash-script/2-bugs/03.sh b/00-build-bash-script/2-bugs/03.sh index 0421f06..58c6686 100755 --- a/00-build-bash-script/2-bugs/03.sh +++ b/00-build-bash-script/2-bugs/03.sh @@ -1,8 +1,8 @@ -#/bin/bash +#/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Comments in bash script. # Usage: ./02.sh @@ -10,21 +10,13 @@ # Single line comment. - # Multi-line comment. # Multi-line comment. # Multi-line comment. echo "Comments in bash script." # Inline comment. -# Why the shebang is not a comment? - -exit 23 +# Is shebang a comment? #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - +exit 23 diff --git a/00-build-bash-script/2-bugs/04.sh b/00-build-bash-script/2-bugs/04.sh index cf13f6d..7244210 100644 --- a/00-build-bash-script/2-bugs/04.sh +++ b/00-build-bash-script/2-bugs/04.sh @@ -1,8 +1,8 @@ -#/bin/bash +#/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Make your script more professional. # Usage: ./04.sh @@ -12,14 +12,9 @@ # 1. Author : the name of the person who created the script # 2. Date of creation : the date when the script was created -# 3. Modified date : the date when the script was last modified +# 3. version: the date when the script was last modified # 4. Description : what the script does -# 5. Usage : how to use the script - -exit 0 +# 5. Usage : how to use the script #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/2-bugs/05.sh b/00-build-bash-script/2-bugs/05.sh index ea01495..f096de3 100755 --- a/00-build-bash-script/2-bugs/05.sh +++ b/00-build-bash-script/2-bugs/05.sh @@ -1,33 +1,27 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Add and remove scripts to/from the PATH # Usage: ./05.sh #-------------------------------------------------------------# - # To see the PATH variable, you can use the following command echo "{PATH}" # Add scripts to the PATH allows you to run the script from any directory without specifying the full path ## To add 0-examples to the PATH, you can use the following command (replace the path with your own) -export PATH=$PATH:$HOME/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples +export PATH=$PATH:$HOME/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples echo "{PATH}" ## To remove 0-examples from the PATH, you can use the following command (replace the path with your own) -export PATH=${PATH%:"$HOME"/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples} +export PATH=${PATH%:"$HOME"/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples} echo "{PATH}" -exit 2 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 2 diff --git a/00-build-bash-script/2-bugs/06.sh b/00-build-bash-script/2-bugs/06.sh index a2d20a2..7b776f4 100755 --- a/00-build-bash-script/2-bugs/06.sh +++ b/00-build-bash-script/2-bugs/06.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Set up secure script permissions # Usage: ./06.sh @@ -29,10 +29,5 @@ ls -m 06.sh # chmod 777 .sh # 777 means that the owner has read, write and execute permission, and the group and public have only read permission -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/3-refactor/00.sh b/00-build-bash-script/3-refactor/00.sh index 5ebb6a0..74a22f4 100755 --- a/00-build-bash-script/3-refactor/00.sh +++ b/00-build-bash-script/3-refactor/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: This script demonstrates the basics of a shell script. # Usage: Just run the script and witness the magic! @@ -16,15 +16,11 @@ # Exit status, the grand finale of our script. Exiting with a status of 0, because why not? -exit 0 - #-------------------------------------------------------------# - +exit 0 # How to run the script: # 1. Open the terminal # 2. Find the script file buried in your filesystem # 3. Try to remember the chmod command and what those numbers mean # 4. Execute the script with the precise combination of dots and slashes # 5. Wonder why you bothered running it in the first place - - diff --git a/00-build-bash-script/3-refactor/01.sh b/00-build-bash-script/3-refactor/01.sh index 132a4c2..069af6f 100755 --- a/00-build-bash-script/3-refactor/01.sh +++ b/00-build-bash-script/3-refactor/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Demonstrating the echo command with various examples. # Usage: Just run the script and be amazed by the echo magic! @@ -18,20 +18,5 @@ echo -e "Hello,\tWorld! This is the third example." # Tabbing into the world wit # Now, let's ponder the deep philosophical question: What is the difference between these echo commands? -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. Find the script file nestled somewhere in your directories -# 3. Try to recall the chmod command and its mysterious numbers -# 4. Execute the script with the perfect combination of dots and slashes -# 5. Marvel at the wonders of echo and question why you're still running this script - - - - - - - +exit 0 diff --git a/00-build-bash-script/3-refactor/02.sh b/00-build-bash-script/3-refactor/02.sh index 9d4a656..2739913 100755 --- a/00-build-bash-script/3-refactor/02.sh +++ b/00-build-bash-script/3-refactor/02.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Demonstrating the usage of the $SHELL variable. # Usage: Execute the script and marvel at the magic of shell variables! @@ -22,15 +22,6 @@ echo "Your default shell is {$SHELL}" # But wait, do we really need those curly # And hey, are there any other shell variables? Maybe. But that's a mystery for another day. -exit 0 - #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. Locate the script file hidden somewhere in your directories -# 3. Try to recall the chmod command syntax and those cryptic numbers -# 4. Execute the script with the usual combination of dots and slashes -# 5. Reflect on the mysteries of shell variables while wondering why you ran this script in the first place - - +exit 0 diff --git a/00-build-bash-script/3-refactor/03.sh b/00-build-bash-script/3-refactor/03.sh index ea1c435..2d49f62 100755 --- a/00-build-bash-script/3-refactor/03.sh +++ b/00-build-bash-script/3-refactor/03.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: A masterpiece of comments in a bash script. # Usage: Execute the script and witness the wonder of comments! @@ -22,13 +22,5 @@ echo "Comments in bash script." # Ah, an inline comment. Because why not mix thi # But seriously, why isn't the shebang a comment? Let's ponder that existential question. -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Summon the terminal from the depths of your operating system. -# 2. Perform the ancient ritual of chmod to grant execute permissions to the script. -# 3. Utter the sacred incantation "./" to invoke the script. -# 4. Observe the output with a sense of wonder and bewilderment. - +exit 0 diff --git a/00-build-bash-script/3-refactor/04.sh b/00-build-bash-script/3-refactor/04.sh index 8483c07..9f0f77d 100644 --- a/00-build-bash-script/3-refactor/04.sh +++ b/00-build-bash-script/3-refactor/04.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Making your script look more professional, because why not? # Usage: Execute the script and witness the illusion of professionalism! @@ -20,12 +20,5 @@ # 5. Usage: Instructions on how to wield this powerful tool. Handle with care! -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Summon the terminal from the depths of your operating system. -# 2. Channel your inner wizard and cast the spell of chmod to grant execute permissions to the script. -# 3. Speak the sacred words "./" to invoke the magic within the script. -# 4. Behold the wonders of professionalism as the script executes its mysterious tasks. +exit 0 diff --git a/00-build-bash-script/3-refactor/05.sh b/00-build-bash-script/3-refactor/05.sh index 0a8f7b6..1e2705f 100755 --- a/00-build-bash-script/3-refactor/05.sh +++ b/00-build-bash-script/3-refactor/05.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Adding and removing scripts from the PATH variable. # Usage: Execute the script and witness the magic of PATH manipulation! @@ -18,22 +18,16 @@ echo "{$PATH}" # But wait, why are we enclosing PATH in curly braces? The world ## Behold, the incantation to add 0-examples to the PATH. Replace the path with your own! -export PATH=$PATH:$HOME/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples +export PATH=$PATH:$HOME/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples echo "{$PATH}" # Let's admire the transformed PATH. Isn't it mesmerizing? ## And now, the sacred ritual to remove 0-examples from the PATH. Replace the path with your own! -export PATH=${PATH%:"$HOME"/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples} +export PATH=${PATH%:"$HOME"/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples} echo "{$PATH}" # Witness the PATH once more, now stripped of its former glory. -exit 0 - #-------------------------------------------------------------# -# How to run the script: -# 1. Summon the terminal from the depths of your operating system. -# 2. Channel your inner wizard and cast the spell of chmod to grant execute permissions to the script. -# 3. Utter the sacred words "./" to invoke the magic within the script. -# 4. Marvel at the manipulation of the PATH variable and wonder why you didn't just set it permanently instead. +exit 0 diff --git a/00-build-bash-script/3-refactor/06.sh b/00-build-bash-script/3-refactor/06.sh index fd62fb9..4290376 100755 --- a/00-build-bash-script/3-refactor/06.sh +++ b/00-build-bash-script/3-refactor/06.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Achieving script security through proper permissions. # Usage: Execute the script and witness the wonders of secure scripting! @@ -26,12 +26,5 @@ ls -l 06.sh # Behold the cryptic output that reveals the secrets of file permiss # chmod 744 .sh - This incantation bestows upon the owner full control and grants mere mortals read-only access. -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Summon the terminal from the depths of your operating system. -# 2. Channel your inner wizard and cast the spell of chmod to grant execute permissions to the script. -# 3. Utter the sacred words "./" to invoke the magic within the script. -# 4. Marvel at the wonders of file permissions and contemplate the complexities of script security. +exit 0 diff --git a/00-build-bash-script/4-modify/00.sh b/00-build-bash-script/4-modify/00.sh index 275cb5d..dbebb36 100755 --- a/00-build-bash-script/4-modify/00.sh +++ b/00-build-bash-script/4-modify/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The main components of a shell script # Usage: ./00.sh @@ -14,13 +14,7 @@ # 2. Commands -# 3. Exit status will be at the beginning of the script +# 3. Exit status will be at the beginning of the script -exit 1 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - +exit 1 diff --git a/00-build-bash-script/4-modify/01.sh b/00-build-bash-script/4-modify/01.sh index 7933809..9fe2f37 100755 --- a/00-build-bash-script/4-modify/01.sh +++ b/00-build-bash-script/4-modify/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: echo command # Usage: ./01.sh @@ -19,16 +19,5 @@ echo -e "Hello,\tWorld! third example" # What is the difference between the three echo commands above? -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - - - - - - +exit 0 diff --git a/00-build-bash-script/4-modify/02.sh b/00-build-bash-script/4-modify/02.sh index 32f76c8..7ce7645 100755 --- a/00-build-bash-script/4-modify/02.sh +++ b/00-build-bash-script/4-modify/02.sh @@ -1,30 +1,24 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The $SHELL varibale # Usage: ./02.sh #-------------------------------------------------------------# # $SHELL is a shell variable that contains the path to the current shell -# However, it actually stores the version number of the current shell +# However, it actually stores the version number of the current shell echo "SHELL" echo "Your default shell is {SHELL}" -# Do we need double quotes at line 13 ? -# Why $SHELL variable is all uppercase? +# Do we need double quotes at line 13 ? +# Why $SHELL variable is all uppercase? # Can I change the default shell? How? # Correct question # Are there any other shell variables? # Correct question -exit 0 - #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` +exit 0 diff --git a/00-build-bash-script/4-modify/03.sh b/00-build-bash-script/4-modify/03.sh index 1fe5a1d..b781ad4 100755 --- a/00-build-bash-script/4-modify/03.sh +++ b/00-build-bash-script/4-modify/03.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Comments in bash script. # Usage: ./02.sh @@ -12,21 +12,13 @@ # Single line comment. - # Multi-line comment. # Multi-line comment. # Multi-line comment. echo "Comments in bash $script." # Inline comment. -# Why the shebang is not a comment? +# Why the shebang is not a comment? -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - - +exit 0 diff --git a/00-build-bash-script/4-modify/04.sh b/00-build-bash-script/4-modify/04.sh index da1ec33..7ae72f0 100644 --- a/00-build-bash-script/4-modify/04.sh +++ b/00-build-bash-script/4-modify/04.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 -# Modified: 2024-03-02 +# Version: 1.0 # Description: Make your script more professional. # Usage: ./04.sh @@ -14,11 +14,7 @@ # 2. Date of creation : the date when the script was created # 3. Modified date : the date when the script was last modified # 4. Description : what the script does -# 5. Usage : how to use the script +# 5. Usage : how to use the script -exit 25 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 25 diff --git a/00-build-bash-script/4-modify/05.sh b/00-build-bash-script/4-modify/05.sh index f732c3e..d55a253 100755 --- a/00-build-bash-script/4-modify/05.sh +++ b/00-build-bash-script/4-modify/05.sh @@ -1,14 +1,12 @@ -#!/bin/bash - +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Add and remove scripts to/from the PATH # Usage: ./05.sh #-------------------------------------------------------------# - # To see the PATH variable, you can use the following command echo "{$PATH}" @@ -24,10 +22,5 @@ export PATH=${PATH%:"$PATH"/Desktop/work/master-classes/bash-scripting/00-build- echo "{$PATH}" -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/4-modify/06.sh b/00-build-bash-script/4-modify/06.sh index 50a0cc9..ed0c6e7 100755 --- a/00-build-bash-script/4-modify/06.sh +++ b/00-build-bash-script/4-modify/06.sh @@ -2,7 +2,7 @@ # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: Set up secure script permissions # Usage: ./06.sh @@ -27,12 +27,8 @@ ls -a 09.sh # To make your script more secure, you can use the following permissions # chmod 744 .sh -# 744 means that the owner has read, write and execute permission, and the group and public have only read permission +# 744 means that the owner has read, write and execute permission, and the group and public have only write permission -exit 0 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/5-chunks/00.sh b/00-build-bash-script/5-chunks/00.sh index 8391e3a..7cf88a5 100755 --- a/00-build-bash-script/5-chunks/00.sh +++ b/00-build-bash-script/5-chunks/00.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The main components of a shell script # Usage: ./00.sh @@ -16,10 +16,6 @@ # 3. __________________________________________ -exit 0 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. ________________________ -# 3. `./` +exit 0 diff --git a/00-build-bash-script/5-chunks/01.sh b/00-build-bash-script/5-chunks/01.sh index 4eafd10..fc38786 100755 --- a/00-build-bash-script/5-chunks/01.sh +++ b/00-build-bash-script/5-chunks/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: echo command # Usage: ./01.sh @@ -16,15 +16,5 @@ echo ____ "Hello,\nWorld! second example" echo _____ "Hello,\tWorld! third example" -___________ #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - - - - - +___________ diff --git a/00-build-bash-script/5-chunks/02.sh b/00-build-bash-script/5-chunks/02.sh index 8a87c02..2565eb3 100755 --- a/00-build-bash-script/5-chunks/02.sh +++ b/00-build-bash-script/5-chunks/02.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 -# Modified: 2024-03-12 +# Version: 1.0 # Description: The $SHELL varibale # Usage: ./02.sh @@ -19,10 +19,5 @@ echo "Your default shell is __________" # Can I change the default shell? How? # Are there any other shell variables? -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. ________________________ -# 3. ________________________ \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/5-chunks/03.sh b/00-build-bash-script/5-chunks/03.sh index e1ff519..770914e 100755 --- a/00-build-bash-script/5-chunks/03.sh +++ b/00-build-bash-script/5-chunks/03.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 @@ -12,20 +12,14 @@ # __________ comment. - # __________ comment. # __________ comment. # __________ comment. echo "Comments in bash script." # Inline comment. -# Why the shebang is not a comment? +# Is the shebang a comment? -exit 0 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` - +exit 0 diff --git a/00-build-bash-script/5-chunks/04.sh b/00-build-bash-script/5-chunks/04.sh index 826203e..5ecc116 100644 --- a/00-build-bash-script/5-chunks/04.sh +++ b/00-build-bash-script/5-chunks/04.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-02 @@ -14,11 +14,7 @@ # 2. ____________ : the date when the script was created # 3. Modified date : the date when the script was last modified # 4. _____________ : what the script does -# 5. Usage : how to use the script +# 5. Usage : how to use the script -exit 0 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/5-chunks/05.sh b/00-build-bash-script/5-chunks/05.sh index cf5d253..c30eb7f 100755 --- a/00-build-bash-script/5-chunks/05.sh +++ b/00-build-bash-script/5-chunks/05.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 @@ -8,26 +8,21 @@ #-------------------------------------------------------------# - # To see the PATH variable, you can use the following command echo "{$PATH}" # Add scripts to the PATH allows you to run the script from any directory without specifying the full path ## To add 0-examples to the PATH, you can use the following command (replace the path with your own) -export PATH=____________/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples +export PATH=____________/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples echo "{$PATH}" ## To remove 0-examples from the PATH, you can use the following command (replace the path with your own) -export PATH=${__________/Desktop/work/master-classes/bash-scripting/00-build-bash-script/0-examples} +export PATH=${__________/Desktop/denepo/bash-scripting/00-build-bash-script/0-examples} echo "{$PATH}" -exit 23 #-------------------------------------------------------------# -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 23 diff --git a/00-build-bash-script/5-chunks/06.sh b/00-build-bash-script/5-chunks/06.sh index 86a4ec1..af32ccd 100755 --- a/00-build-bash-script/5-chunks/06.sh +++ b/00-build-bash-script/5-chunks/06.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-12 @@ -29,10 +29,5 @@ ls -l 06.sh # chmod 744 .sh # 744 means that the owner has ________________ permission, and the group and public have __________ permission -exit 0 #-------------------------------------------------------------# - -# How to run the script: -# 1. Open the terminal -# 2. `chmod 744 ` -# 3. `./` \ No newline at end of file +exit 0 diff --git a/00-build-bash-script/6-blank-pages/00.sh b/00-build-bash-script/6-blank-pages/00.sh index cc1f786..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/00.sh +++ b/00-build-bash-script/6-blank-pages/00.sh @@ -1 +1,13 @@ -#!/bin/bash \ No newline at end of file +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: + +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/00-build-bash-script/6-blank-pages/01.sh b/00-build-bash-script/6-blank-pages/01.sh index 9234836..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/01.sh +++ b/00-build-bash-script/6-blank-pages/01.sh @@ -1,7 +1,13 @@ -#!/bin/bash - - +#!/opt/homebrew/bin/bash +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: +#---------------------------------------------------------------------# +#---------------------------------------------------------------------# +exit 0 diff --git a/00-build-bash-script/6-blank-pages/02.sh b/00-build-bash-script/6-blank-pages/02.sh index 05a7907..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/02.sh +++ b/00-build-bash-script/6-blank-pages/02.sh @@ -1,2 +1,13 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: + +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/00-build-bash-script/6-blank-pages/03.sh b/00-build-bash-script/6-blank-pages/03.sh index 7a693aa..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/03.sh +++ b/00-build-bash-script/6-blank-pages/03.sh @@ -1,3 +1,13 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/00-build-bash-script/6-blank-pages/04.sh b/00-build-bash-script/6-blank-pages/04.sh index a9bf588..8e440a1 100644 --- a/00-build-bash-script/6-blank-pages/04.sh +++ b/00-build-bash-script/6-blank-pages/04.sh @@ -1 +1,13 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: + +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/00-build-bash-script/6-blank-pages/05.sh b/00-build-bash-script/6-blank-pages/05.sh index 05a7907..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/05.sh +++ b/00-build-bash-script/6-blank-pages/05.sh @@ -1,2 +1,13 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: + +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/00-build-bash-script/6-blank-pages/06.sh b/00-build-bash-script/6-blank-pages/06.sh index 05a7907..8e440a1 100755 --- a/00-build-bash-script/6-blank-pages/06.sh +++ b/00-build-bash-script/6-blank-pages/06.sh @@ -1,2 +1,13 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash +# Author: Samir M. +# Date: +# Version: 1.0 +# Description: +# Usage: + +#---------------------------------------------------------------------# + +#---------------------------------------------------------------------# + +exit 0 diff --git a/01-debug-your-script/0-examples/00.md b/01-debug-your-script/0-examples/00.md index f889e48..e466be0 100644 --- a/01-debug-your-script/0-examples/00.md +++ b/01-debug-your-script/0-examples/00.md @@ -22,3 +22,26 @@ shellcheck 1.sh - It cannot detect issues with the environment that the script is running in. - It cannot detect how the permissions are set on the file. - It won't be able to tell you if specific packages are required to run the script. + +### Using Bash Debug + +- Install `Bash Debug` in VS code. +- go to debug in vs code and create`launch.json` +- add the following to your file and debug as any other file in VS code. + +```json +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "bashdb", + "request": "launch", + "name": "Bash-Debug (simplest configuration)", + "program": "${file}" + } + ] +} +``` diff --git a/01-debug-your-script/0-examples/01.sh b/01-debug-your-script/0-examples/01.sh index baea7b8..61e3a6a 100755 --- a/01-debug-your-script/0-examples/01.sh +++ b/01-debug-your-script/0-examples/01.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-13 -# Modified: 2024-03-14 +# Version: 1.0 # Description: Common error types in Bash # Usage: ./01.sh diff --git a/01-debug-your-script/0-examples/02.sh b/01-debug-your-script/0-examples/02.sh index 5946d36..7f90de9 100755 --- a/01-debug-your-script/0-examples/02.sh +++ b/01-debug-your-script/0-examples/02.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 -# Modified: 2024-03-14 +# Version: 1.0 # Description: Finding help in Bash # Usage: ./02.sh diff --git a/01-debug-your-script/0-examples/03.sh b/01-debug-your-script/0-examples/03.sh index 2b0daec..d36c279 100755 --- a/01-debug-your-script/0-examples/03.sh +++ b/01-debug-your-script/0-examples/03.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 -# Modified: 2024-03-14 +# Version: 1.0 # Description: the help command # Usage: ./03.sh diff --git a/01-debug-your-script/0-examples/04.sh b/01-debug-your-script/0-examples/04.sh index 940a5a3..ea2b5fb 100755 --- a/01-debug-your-script/0-examples/04.sh +++ b/01-debug-your-script/0-examples/04.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 -# Modified: 2024-03-14 +# Version: 1.0 # Description: the man command # Usage: ./04.sh diff --git a/01-debug-your-script/0-examples/05.sh b/01-debug-your-script/0-examples/05.sh index 56697bd..ec25fb3 100755 --- a/01-debug-your-script/0-examples/05.sh +++ b/01-debug-your-script/0-examples/05.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 -# Modified: 2024-03-14 +# Version: 1.0 # Description: the info command # Usage: ./05.sh diff --git a/01-debug-your-script/1-blanks/01.sh b/01-debug-your-script/1-blanks/01.sh index 68abc0f..aebcfc9 100755 --- a/01-debug-your-script/1-blanks/01.sh +++ b/01-debug-your-script/1-blanks/01.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: ________ # Date: __________ @@ -38,5 +38,3 @@ hello # _______________ #------------------------------------------------------------------# exit 0 - - diff --git a/01-debug-your-script/1-blanks/02.sh b/01-debug-your-script/1-blanks/02.sh index 8647d4d..3a496c0 100755 --- a/01-debug-your-script/1-blanks/02.sh +++ b/01-debug-your-script/1-blanks/02.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: ________ # Date: __________ diff --git a/01-debug-your-script/1-blanks/03.sh b/01-debug-your-script/1-blanks/03.sh index 605ad58..e68bca3 100755 --- a/01-debug-your-script/1-blanks/03.sh +++ b/01-debug-your-script/1-blanks/03.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: ________ # Date: __________ diff --git a/01-debug-your-script/1-blanks/04.sh b/01-debug-your-script/1-blanks/04.sh index 119f613..ac261a5 100755 --- a/01-debug-your-script/1-blanks/04.sh +++ b/01-debug-your-script/1-blanks/04.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: ________ # Date: __________ diff --git a/01-debug-your-script/1-blanks/05.sh b/01-debug-your-script/1-blanks/05.sh index a8b668f..f0ac4dc 100755 --- a/01-debug-your-script/1-blanks/05.sh +++ b/01-debug-your-script/1-blanks/05.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: ________ # Date: __________ diff --git a/01-debug-your-script/2-bugs/01.sh b/01-debug-your-script/2-bugs/01.sh index 0715c88..4d9e583 100755 --- a/01-debug-your-script/2-bugs/01.sh +++ b/01-debug-your-script/2-bugs/01.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-13 diff --git a/01-debug-your-script/2-bugs/02.sh b/01-debug-your-script/2-bugs/02.sh index 3cfbcca..8162768 100755 --- a/01-debug-your-script/2-bugs/02.sh +++ b/01-debug-your-script/2-bugs/02.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/2-bugs/03.sh b/01-debug-your-script/2-bugs/03.sh index 9d938be..d73ad0a 100755 --- a/01-debug-your-script/2-bugs/03.sh +++ b/01-debug-your-script/2-bugs/03.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/2-bugs/04.sh b/01-debug-your-script/2-bugs/04.sh index cd47dbb..6aa1148 100755 --- a/01-debug-your-script/2-bugs/04.sh +++ b/01-debug-your-script/2-bugs/04.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/2-bugs/05.sh b/01-debug-your-script/2-bugs/05.sh index d8f0594..a5d8b08 100755 --- a/01-debug-your-script/2-bugs/05.sh +++ b/01-debug-your-script/2-bugs/05.sh @@ -1,5 +1,4 @@ -#!/bin/bash - +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 # Modified: 2024-03-14 diff --git a/01-debug-your-script/3-refactor/01.sh b/01-debug-your-script/3-refactor/01.sh index baea7b8..63064c2 100755 --- a/01-debug-your-script/3-refactor/01.sh +++ b/01-debug-your-script/3-refactor/01.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-13 diff --git a/01-debug-your-script/3-refactor/02.sh b/01-debug-your-script/3-refactor/02.sh index 5946d36..1ff1dea 100755 --- a/01-debug-your-script/3-refactor/02.sh +++ b/01-debug-your-script/3-refactor/02.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/3-refactor/03.sh b/01-debug-your-script/3-refactor/03.sh index 2b0daec..7c405c2 100755 --- a/01-debug-your-script/3-refactor/03.sh +++ b/01-debug-your-script/3-refactor/03.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/3-refactor/04.sh b/01-debug-your-script/3-refactor/04.sh index 940a5a3..cc9ce33 100755 --- a/01-debug-your-script/3-refactor/04.sh +++ b/01-debug-your-script/3-refactor/04.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/3-refactor/05.sh b/01-debug-your-script/3-refactor/05.sh index 56697bd..1f41698 100755 --- a/01-debug-your-script/3-refactor/05.sh +++ b/01-debug-your-script/3-refactor/05.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-14 diff --git a/01-debug-your-script/4-modify/01.sh b/01-debug-your-script/4-modify/01.sh index 1386218..675d094 100755 --- a/01-debug-your-script/4-modify/01.sh +++ b/01-debug-your-script/4-modify/01.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/opt/homebrew/bin/bash # Author: Samir M. # Date: 2024-03-13 @@ -26,7 +26,7 @@ touch 01.sh # File exists # 4. Permission Denied Errors # Example of Permission Denied Error: Attempting to write to a directory without proper permissions -echo "Hello" < /root/test.txt # Permission denied +echo "Hello" /dev/null stands for redirecting the error message to /dev/null - -echo "Hello, ${USER^}!" -echo "Creating a backup of all files in the home directory, ${HOME}..." - -current_dir=$(pwd) - -echo "You are currently in ${current_dir}." - -# Remove v option to make the script silent -tar -cf "${current_dir}"_"$(date +"%Y-%m-%d_%H-%M-%S")".tar ${HOME}/* 2 > /dev/null - -echo "Backup completed successfully!" - - -exit 0 - diff --git a/02-variables-and-shell-expansions/07.sh b/02-variables-and-shell-expansions/07.sh deleted file mode 100755 index 8981f03..0000000 --- a/02-variables-and-shell-expansions/07.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -# Author: Samir M. -# Date: 2024-03-03 -# Modified: 2024-03-03 -# Description: Arithmatic expansion -# Usage: ./07.sh - - -# What is arithmatic expansion? -# Arithmatic expansion is a way to perform arithmatic operations in bash. - -# Syntax: -# $((expression)) - -# Example: - -num1=10 -num2=5 - -echo $(($num1 + $num2)) # 15 -echo $(($num1 - $num2)) # 5 -echo $(($num1 * $num2)) # 50 -echo $(($num1 / $num2)) # 2 -echo $(($num1 ** $num2)) # 100000 -echo $(($num1 % $num2)) # 0 - -# Note: Arrithmatic expansion can't be used to perform floating point operations. - -# Operation precedence -# 1. Parentheses -# 2. Exponentiation -# 3. Multiplication, Division, Modulus -# 4. Addition, Subtraction - - -exit 0 \ No newline at end of file diff --git a/02-variables-and-shell-expansions/1-blanks/01.sh b/02-variables-and-shell-expansions/1-blanks/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/02.sh b/02-variables-and-shell-expansions/1-blanks/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/03.sh b/02-variables-and-shell-expansions/1-blanks/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/04.sh b/02-variables-and-shell-expansions/1-blanks/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/05.sh b/02-variables-and-shell-expansions/1-blanks/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/06.sh b/02-variables-and-shell-expansions/1-blanks/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/07.sh b/02-variables-and-shell-expansions/1-blanks/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/08.sh b/02-variables-and-shell-expansions/1-blanks/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/09.sh b/02-variables-and-shell-expansions/1-blanks/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/10.sh b/02-variables-and-shell-expansions/1-blanks/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/1-blanks/11.sh b/02-variables-and-shell-expansions/1-blanks/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/1-blanks/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/01.sh b/02-variables-and-shell-expansions/2-bugs/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/02.sh b/02-variables-and-shell-expansions/2-bugs/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/03.sh b/02-variables-and-shell-expansions/2-bugs/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/04.sh b/02-variables-and-shell-expansions/2-bugs/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/05.sh b/02-variables-and-shell-expansions/2-bugs/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/06.sh b/02-variables-and-shell-expansions/2-bugs/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/07.sh b/02-variables-and-shell-expansions/2-bugs/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/08.sh b/02-variables-and-shell-expansions/2-bugs/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/09.sh b/02-variables-and-shell-expansions/2-bugs/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/10.sh b/02-variables-and-shell-expansions/2-bugs/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/2-bugs/11.sh b/02-variables-and-shell-expansions/2-bugs/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/2-bugs/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/01.sh b/02-variables-and-shell-expansions/3-refactor/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/02.sh b/02-variables-and-shell-expansions/3-refactor/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/03.sh b/02-variables-and-shell-expansions/3-refactor/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/04.sh b/02-variables-and-shell-expansions/3-refactor/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/05.sh b/02-variables-and-shell-expansions/3-refactor/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/06.sh b/02-variables-and-shell-expansions/3-refactor/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/07.sh b/02-variables-and-shell-expansions/3-refactor/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/08.sh b/02-variables-and-shell-expansions/3-refactor/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/09.sh b/02-variables-and-shell-expansions/3-refactor/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/10.sh b/02-variables-and-shell-expansions/3-refactor/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/3-refactor/11.sh b/02-variables-and-shell-expansions/3-refactor/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/3-refactor/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/01.sh b/02-variables-and-shell-expansions/4-modify/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/02.sh b/02-variables-and-shell-expansions/4-modify/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/03.sh b/02-variables-and-shell-expansions/4-modify/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/04.sh b/02-variables-and-shell-expansions/4-modify/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/05.sh b/02-variables-and-shell-expansions/4-modify/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/06.sh b/02-variables-and-shell-expansions/4-modify/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/07.sh b/02-variables-and-shell-expansions/4-modify/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/08.sh b/02-variables-and-shell-expansions/4-modify/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/09.sh b/02-variables-and-shell-expansions/4-modify/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/10.sh b/02-variables-and-shell-expansions/4-modify/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/4-modify/11.sh b/02-variables-and-shell-expansions/4-modify/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/4-modify/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/01.sh b/02-variables-and-shell-expansions/5-chunks/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/02.sh b/02-variables-and-shell-expansions/5-chunks/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/03.sh b/02-variables-and-shell-expansions/5-chunks/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/04.sh b/02-variables-and-shell-expansions/5-chunks/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/05.sh b/02-variables-and-shell-expansions/5-chunks/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/06.sh b/02-variables-and-shell-expansions/5-chunks/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/07.sh b/02-variables-and-shell-expansions/5-chunks/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/08.sh b/02-variables-and-shell-expansions/5-chunks/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/09.sh b/02-variables-and-shell-expansions/5-chunks/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/10.sh b/02-variables-and-shell-expansions/5-chunks/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/5-chunks/11.sh b/02-variables-and-shell-expansions/5-chunks/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/5-chunks/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/01.sh b/02-variables-and-shell-expansions/6-blank-pages/01.sh new file mode 100755 index 0000000..ae0f430 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/01.sh @@ -0,0 +1,41 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Shell parameters +# Usage: ./01.sh + +#------------------------------------------------------------------# + +# User-defined Variables +name="John" +age=30 + +echo "User-defined Variables Example:" +echo "Name: $name" +echo "Age: $age" + +# Shell Variables +echo "Environment Variables Example:" +echo "Home Directory: $HOME" +echo "User Name: $USER" + +# Positional Parameters +echo "Positional Parameters Example:" +echo "Script Name: $0" +echo "First Parameter: $1" +echo "Second Parameter: $2" +echo + +# Special Parameters +echo "Special Parameters Example:" +echo "Process ID: $$" +echo "Exit Status of Last Command: $?" +echo "Number of Parameters: $#" +echo "All Parameters as Separate Strings: $*" +echo "All Parameters as a Single String: $*" + +#------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/02.sh b/02-variables-and-shell-expansions/6-blank-pages/02.sh new file mode 100644 index 0000000..11758e7 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/02.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-15 +# Version: 1.0 +# Description: Variables(User defined variables) +# Usage: ./02.sh + +# ------------------------------------------------------------------# +# What is a variable? +# A variable is a container that holds values that are used in a bash script. + +# How to declare a variable? +# By convention user variables are declared in lower case. +# No space is allowed between the variable name and the value. +# If the varibales is more than one word, use (_) to separate the words. +# Syntax: +# variable_name=value + +# Example: + +name="Sam" + +# How to use a variable? +# 1. use the variable name with a dollar sign in front of it. +echo $name + +# 2. use a variable in a string? +# Example: +echo "Hello, $name" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "Hello, ${name}" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/03.sh b/02-variables-and-shell-expansions/6-blank-pages/03.sh new file mode 100644 index 0000000..84a7b9e --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/03.sh @@ -0,0 +1,56 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Variables(Shell defined) +# Usage: ./03.sh + +#------------------------------------------------------------------# +# What is a shell variable? +# A shell variable is a variable that is defined by the shell and have a useful value . +# By convention shell variables are declared in upper case. + +# How to use a shell variable? +# 1. use the variable name with a dollar sign in front of it. +echo "$HOME" + +# 2. use a variable in a string? +# Example: +echo "My home directory is $HOME" + +# 3. use a variable in a string with curly braces? (recommended) +# Example: +echo "My home directory is ${HOME}" + +# There are around 105 shell variables that are defined by the shell. + +# Some of the most commonly used shell variables are: + +# SHELL: The shell that is being used. +echo "The shell that is being used is $SHELL" + +# PATH: The list of directories that the shell searches for commands. +echo "The list of directories that the shell searches for commands is $PATH" + +# HOME: The home directory of the user. +echo "The home directory of the user is $HOME" + +# PWD: The current working directory. +echo "The current working directory is $PWD" + +# USER: The username of the user. +echo "The username of the user is $USER" + +# HOSTNAME: The hostname of the computer. +echo "The hostname of the computer is $HOSTNAME" + +# HOSTTYPE: The type of the computer. +echo "The type of the computer is $HOSTTYPE" + +# PS1: The primary prompt string, which is displayed before each command and you can customize it. +echo "The primary prompt string is $PS1" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/04.sh b/02-variables-and-shell-expansions/6-blank-pages/04.sh new file mode 100755 index 0000000..3a5d732 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/04.sh @@ -0,0 +1,22 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: Positional parameters. +# Usage: ./04.sh [arg1] [arg2] + +# ------------------------------------------------------------------# + +# Positional parameters refer to arguments or values supplied to shell scripts or functions during execution. +# These parameters are accessed within scripts using special notation ($1, $2, $3, etc.), with $1 representing +# the first parameter, $2 the second, and so on. Additionally, $0 denotes the script or function name." + +echo "Example:" + +echo "echo Script name: $0" +echo "echo First parameter: $1" +echo "echo Second parameter: $2" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/05.sh b/02-variables-and-shell-expansions/6-blank-pages/05.sh new file mode 100755 index 0000000..fdc85fe --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/05.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-19 +# Version: 1.0 +# Description: special parameters +# Usage: ./05.sh + +# ------------------------------------------------------------------# + +# Special parameters, predefined by the shell, provide essential information about script execution environment and status. + +echo "Commonly used special parameters:" + +echo "$$" # Current shell process ID (PID)." +echo "$?" # Exit status of the last command." +echo "$#" # Number of positional parameters passed to the script." +echo "$@" # Represents all positional parameters as separate quoted strings." +echo "$*" # Represents all positional parameters as a single quoted string." + +echo "Process ID: $$" +echo "Exit status of last command: $?" +echo "Number of parameters: $#" +echo "All parameters as separate strings:" "$@" +echo "All parameters as a single string: $*" + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/06.sh b/02-variables-and-shell-expansions/6-blank-pages/06.sh new file mode 100755 index 0000000..1d10bbd --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/06.sh @@ -0,0 +1,44 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Parameter expansion +# Usage: ./06.sh + +# ------------------------------------------------------------------# +# What is parameter expansion? +# When we use a varibale with curly braces, that is what we call parameter expansion. +name="Sam" +echo "Hello, ${name}" + +# paramter expansion tricks and tips + +# Note that the original value of the variable is not changed + +# Convert the first character to lowercase +echo ${name,} + +# Convert the whole parameter to lowercase +echo ${name,,} + +# Convert the first character to uppercase +echo ${name^} + +# Convert the whole parameter to uppercase +echo ${name^^} + +# Check the length of the parameter +echo ${#name} + +# Substring the parameter, starting from the 0th index and 2 characters long +echo ${name:0:2} + +# If we don't specify the length, it will go to the end of the string +echo ${name:1} + +# We can also use negative numbers to count from the end of the string +echo ${name: -1} # Note the space between the colon and the negative number is important + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/07.sh b/02-variables-and-shell-expansions/6-blank-pages/07.sh new file mode 100755 index 0000000..c47e00b --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/07.sh @@ -0,0 +1,34 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Command substitution +# Usage: ./07.sh + +# ------------------------------------------------------------------# +# What is command substitution? +# Command substitution is a mechanism by which the shell performs a command and replaces it with its output or in a varibale. + +# Syntax +# $(command) + +# Example + +time=$(date +"%Y-%m-%d %H:%M:%S") +echo "The current date and time is: ${time}" + +# date format +# %Y: Year (e.g. 2024), %y: Year (e.g. 24) +# %m: Month (e.g. 03) +# %d: Day (e.g. 03) +# %H: Hour (e.g. 12) +# %M: Minute (e.g. 30) +# %S: Second (e.g. 30) + +# Another example +echo "The current date and time is: $(date +"%Y-%m-%d %H:%M:%S")" + +# ------------------------------------------------------------------# + +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/08.sh b/02-variables-and-shell-expansions/6-blank-pages/08.sh new file mode 100755 index 0000000..59f9015 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/08.sh @@ -0,0 +1,37 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-03 +# Version: 1.0 +# Description: Arithmatic expansion +# Usage: ./08.sh + +# ------------------------------------------------------------------# +# What is arithmatic expansion? +# Arithmatic expansion is a way to perform arithmatic operations in bash. + +# Syntax: +# $((expression)) + +# Example: + +num1=10 +num2=5 + +echo $(("$num1" + "$num2")) # 15 +echo $(("$num1" - "$num2")) # 5 +echo $(("$num1" * "$num2")) # 50 +echo $(("$num1" / "$num2")) # 2 +echo $(("$num1" ** "$num2")) # 100000 +echo $(("$num1" % "$num2")) # 0 + +# Note: Arrithmatic expansion can't be used to perform floating point operations. + +# Operation precedence +# 1. Parentheses +# 2. Exponentiation +# 3. Multiplication, Division, Modulus +# 4. Addition, Subtraction + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/09.sh b/02-variables-and-shell-expansions/6-blank-pages/09.sh new file mode 100755 index 0000000..08720f3 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/09.sh @@ -0,0 +1,26 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: bc command for floating point operations +# Usage: ./09.sh + +# ------------------------------------------------------------------# +# What is bc command? +# bc command is a command line calculator that performs floating point operations. +# Note: ** is replaced by ^ in bc command. + +# Syntax: +# echo "expression" | bc + +# Example: +num1=10 +num2=3 + +echo "scale=2; $num1 / $num2" | bc # 3.33 + +# Note: scale=2 is used to set the number of decimal places. + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/10.sh b/02-variables-and-shell-expansions/6-blank-pages/10.sh new file mode 100644 index 0000000..44760a8 --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/10.sh @@ -0,0 +1,28 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Tilde expansion +# Usage: ./10.sh + +# ------------------------------------------------------------------# +# What is tilde expansion? +# Tilde expansion is a way to refer to home directory in bash. + +# Syntax: +# ~ +# ~username # Refers to the home directory of this user if the user exists. + +# Example: + +echo ~ # /home/sam +echo ~root # /var/root +echo ~sam # /home/sam + +# You can also use ~+ to refer to the current working directory and ~- to refer to the previous working directory. +echo ~+ # the current working directory +echo ~- # the previous working directory + +# ------------------------------------------------------------------# +exit 0 diff --git a/02-variables-and-shell-expansions/6-blank-pages/11.sh b/02-variables-and-shell-expansions/6-blank-pages/11.sh new file mode 100644 index 0000000..4e6343a --- /dev/null +++ b/02-variables-and-shell-expansions/6-blank-pages/11.sh @@ -0,0 +1,40 @@ +#!/opt/homebrew/bin/bash + +# Author: Samir M. +# Date: 2024-03-04 +# Version: 1.0 +# Description: Brace expansion +# Usage: ./11.sh + +# ------------------------------------------------------------------# +# What is brace expansion? +# Brace expansion is a way to generate a text with a specific pattern. + +# Syntax: +# {pattern1,pattern2,pattern3,...} +# Note : no spaces between the patterns allowed. + +# Example: +echo {1,2,3} # 1 2 3 + +# Generate a sequence of numbers. +echo {1..5} # 1 2 3 4 5 + +# Generate a sequence of numbers in reverse order. +echo {5..1} # 5 4 3 2 1 + +# Generate a sequence of characters. +echo {a..e} # a b c d e + +# Generate a sequence of characters in reverse order. +echo {e..a} # e d c b a + +# Generate a sequence of characters with a specific step. +echo {a..e..2} # a c e + +# Prefix and suffix. +echo Month{01..12} # Month01 Month02 Month03 ... Month12 +echo {01..12}Month # 01Month 02Month 03Month ... 12Month + +# ------------------------------------------------------------------# +exit 0 diff --git a/README.md b/README.md index 64b4b5d..8316648 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Bash Scripting Master Class +# Bash Scripting ## Introduction -Welcome to Bash Scripting Master class! This course is designed to provide you with a thorough understanding of Bash scripting, empowering you to automate tasks, enhance your productivity, and delve into the world of shell scripting. +Welcome to Bash Scripting Course! This course is designed to provide you with a thorough understanding of Bash scripting, empowering you to automate tasks, enhance your productivity, and delve into the world of shell scripting. ## Course Overview