From f0af189645e3066eeeb31426ecebbeca020f86e7 Mon Sep 17 00:00:00 2001 From: samirm00 Date: Tue, 5 Mar 2024 07:14:43 +0100 Subject: [PATCH] add how bash process commands --- 03-how-bash-process-commands/01.sh | 42 +++++++++++++++++++++++++++--- 03-how-bash-process-commands/02.sh | 32 +++++++++++++++++++++++ 03-how-bash-process-commands/03.sh | 40 ++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100755 03-how-bash-process-commands/02.sh create mode 100755 03-how-bash-process-commands/03.sh diff --git a/03-how-bash-process-commands/01.sh b/03-how-bash-process-commands/01.sh index 29720d4..620f30f 100644 --- a/03-how-bash-process-commands/01.sh +++ b/03-how-bash-process-commands/01.sh @@ -1,8 +1,8 @@ #!/bin/bash # Author: Samir M. -# Date: 2024-03-04 -# Modified: 2024-03-04 +# Date: 2024-03-05 +# Modified: 2024-03-05 # Description: How bash processes command lines # Usage: ./01.sh @@ -11,4 +11,40 @@ # Step 1: Tokenisation: # The command line is broken into tokens. Tokens are the smallest units of a command line. -# Tokens are separated by metacharacters, such as spaces, tabs, and newlines, | ; & ( ) < >. +# Tokens are separated by metacharacters, such as spaces, tabs, and newlines, | ; & () < >. + +# example +# echo "Hello, World!" # 4 tokens: echo, "Hello, World!", newline + +# What is the differnce between a word and an operator? +# A word never contain unquoted metacharacters. +# An operator always contains unquoted metacharacters. + +# example +# echo "Hello, World!" # 3 words: echo, "Hello, World!" +# echo "Hello, World!" # 1 operator: newline + +# Step 2: Command identification: +# Bash identifies simple or compound commands. + +# example +# echo "Hello, World!" # simple command +# if [ -f file ]; then echo "File exists"; fi # compound command + +# Step 3: Expansions: +# Bash do expansions on the tokens. Expansions are the process of replacing a token with its value. + +# example +# echo * # expansion of * is the list of files in the current directory + +# Step 4: Quote removal: +# Bash removes quotes from the tokens. + +# example +# echo "Hello, World!" # "Hello, World!" is removed + +# step 5: Redirection: +# Bash performs redirections and then executes the command. + + +exit 0 \ No newline at end of file diff --git a/03-how-bash-process-commands/02.sh b/03-how-bash-process-commands/02.sh new file mode 100755 index 0000000..2cbbfb3 --- /dev/null +++ b/03-how-bash-process-commands/02.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Author: Samir M. +# Date: 2024-03-05 +# Modified: 2024-03-05 +# Description: Quoting +# Usage: .\02.sh + +# Quoting: is about removing the special meaning of characters. +# There are three types of quotes in bash: single quotes, double quotes, and back slashes. + +# Single quotes: +# Single quotes are used to prevent the interpretation of all of the special characters. + +# example +filepath1='\home\sam\My Documents' +echo $filepath1 # \home\sam\My Documents + +# Double quotes: +# Double quotes are used to prevent the interpretation of most of the special characters, except $ , back ticks ` and \. + +# example +filepath2="\home\\${USER}\My Documents" +echo $filepath2 # \home\sam\My Documents + +# Back slashes: +# Back slashes are used to prevent the interpretation of the next character. + +# example +echo Hello John \& Jane # Hello John & Jane + +exit 0 diff --git a/03-how-bash-process-commands/03.sh b/03-how-bash-process-commands/03.sh new file mode 100755 index 0000000..064da97 --- /dev/null +++ b/03-how-bash-process-commands/03.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Author: Samir M. +# Date: 2024-03-05 +# Modified: 2024-03-05 +# Description: Tokenisation +# Usage: .\02.sh + +# Tokenisation: +# The process of breaking a command line into its a smaller parts called tokens using metacharacters. + +# What are metacharacters? +# Metacharacters are characters that have a special meaning to the shell. +# The metacharacters are: space, tab, newline, ;, (, ), <, >, |, and &. + +# The token can be either a word or an operator. +# A word doesn't contain any unquoted metacharacters. +# An operator does contain at least one unquoted metacharacters. + +# Example: + +name="Sam" +echo $name > file.txt +# we have 5 metacharacters here: space, > and newline. +# we have 4 tokens: echo, $name and file.txt all are words, because they don't contain any unquoted metacharacters. +# > is an operator, because it contains one unquoted metacharacter. + + + + + + + + + + + + + +exit 0