-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
samirm00
committed
Mar 12, 2024
0 parents
commit cda81cd
Showing
84 changed files
with
2,389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Ignore system and editor-specific files | ||
*.swp | ||
.DS_Store | ||
|
||
# Ignore compiled binaries | ||
*.o | ||
*.out | ||
|
||
# Ignore log files | ||
*.log | ||
|
||
# Ignore temporary files | ||
.tmp/ | ||
.temp/ | ||
|
||
# Ignore user-specific files | ||
.idea/ | ||
.vscode/ | ||
|
||
# Ignore environment-specific files | ||
.env | ||
|
||
# Ignore generated documentation | ||
doc/ | ||
|
||
# Ignore compiled bash scripts | ||
*.shc | ||
|
||
# Ignore backup files | ||
*.bak | ||
|
||
# Ignore dependencies and virtual environments | ||
node_modules/ | ||
venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Introduction to Shells and Shell Scripts | ||
|
||
## What is a Shell? | ||
|
||
A shell is a fundamental `program` that facilitates interaction between `users` and the `operating system` by interpreting commands. Essentially, it takes commands entered via the keyboard and directs them to the operating system for execution. | ||
|
||
In the Linux environment, there are various shells available, with the most prevalent being `bash`—an acronym for `Bourne Again SHell`. It serves as the default shell for the majority of Linux distributions. | ||
|
||
`NOTE: Please use a bash with a version higher than 4.0.0` | ||
|
||
## Understanding Shell Scripts | ||
|
||
A shell script is a file that comprises a sequence of commands designed to be executed by the shell. These scripts contribute to the automation of diverse tasks, ranging from system administration to file management and software installation. | ||
|
||
## Components of a Shell Script | ||
|
||
A typical shell script file consists of three main components: | ||
|
||
### 1. Shebang | ||
|
||
The shebang, denoted by `#!`, is the initial line in a script that informs the operating system about the interpreter to be used for execution. For instance, `#!/bin/bash` specifies the usage of the Bash shell. | ||
|
||
### 2. Commands | ||
|
||
The body of the script comprises a series of commands that the shell executes in sequence. These commands can encompass a wide range of operations, allowing users to perform various tasks efficiently. | ||
|
||
### 3. Exit Status | ||
|
||
Every shell command returns an exit status, indicating whether the command execution was `successful` or `not`. `0` exit code means `successful` and `non 0` exit code means `non successful` | ||
|
||
## Significance of Shell Scripts | ||
|
||
Shell scripts play a pivotal role in automating tasks, simplifying complex processes, and enhancing system efficiency. Common applications include system administration, repetitive tasks, and the facilitation of software installations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
# Author: Samir M. | ||
# Date: 2024-03-12 | ||
# Modified: 2024-03-12 | ||
# Description: The main components of a shell script | ||
# Usage: ./00.sh | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# Shell script consists of three main components: | ||
|
||
# 1. Shebang must be the first line of the script | ||
|
||
# 2. Commands | ||
|
||
# 3. Exit status will be at the end of the script | ||
|
||
exit 0 | ||
#-------------------------------------------------------------# | ||
|
||
# How to run the script: | ||
# 1. Open the terminal | ||
# 2. `chmod 744 <path-to-the-script>` | ||
# 3. `./<path-to-the-script>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Using the `echo` Command in Bash | ||
|
||
## Introduction | ||
|
||
The `echo` command in Bash is a fundamental tool for displaying text or variables on the terminal. | ||
|
||
## Basic Syntax | ||
|
||
The basic syntax for the `echo` command is as follows: | ||
|
||
```bash | ||
echo [options] [text or variables] | ||
``` | ||
|
||
## Displaying Text | ||
|
||
The primary purpose of the `echo` command is to display text on the terminal. | ||
|
||
```bash | ||
echo "Hello, World!" | ||
``` | ||
|
||
## Escape Characters | ||
|
||
Escape characters are essential for formatting and displaying special characters. Some commonly used escape characters include: | ||
|
||
- `\n`: Newline | ||
- `\t`: Tab | ||
- `\\`: Backslash | ||
|
||
```bash | ||
echo -e "Line 1\nLine 2" | ||
``` | ||
|
||
## Best Practices | ||
|
||
Follow these best practices to make your `echo` commands more readable and maintainable. | ||
|
||
- Use quotes around strings to handle spaces and special characters. | ||
- Utilize the `-e` option for interpreting escape characters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
# Author: Samir M. | ||
# Date: 2024-03-12 | ||
# Modified: 2024-03-12 | ||
# Description: echo command | ||
# Usage: ./01.sh | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# The echo command is used to displaying text or variables on the terminal | ||
|
||
echo "Hello, World! first example" | ||
|
||
echo -e "Hello,\nWorld! second example" | ||
|
||
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 <path-to-the-script>` | ||
# 3. `./<path-to-the-script>` | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# The `$SHELL` Variable | ||
|
||
## Introduction | ||
|
||
The `$SHELL` variable in Unix-like operating systems is an environment variable that holds the path to the user's preferred shell. | ||
|
||
## Usage | ||
|
||
To access the value stored in the `$SHELL` variable, you can use the `echo` command: | ||
|
||
```bash | ||
echo $SHELL | ||
``` | ||
|
||
This will output the path to the current user's default shell. | ||
|
||
## Common Shells | ||
|
||
The `$SHELL` variable can point to various shell programs. Some common shells include: | ||
|
||
- `/bin/bash`: Bash | ||
- `/bin/zsh` : Zsh | ||
- `/bin/fish`: Fish | ||
- `/bin/dash`: Dash | ||
|
||
## Changing the Default Shell | ||
|
||
Users can change their default shell using the `chsh` command. This will update the value stored in the `$SHELL` variable: | ||
|
||
```bash | ||
chsh -s /bin/zsh | ||
``` | ||
|
||
This command sets the default shell to Zsh, for example. | ||
|
||
## Best Practices | ||
|
||
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. | ||
- Be cautious when changing the default shell, considering compatibility with existing scripts and configurations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
|
||
# Author: Samir M. | ||
# Date: 2024-03-12 | ||
# Modified: 2024-03-12 | ||
# Description: The $SHELL varibale | ||
# Usage: ./02.sh | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# $SHELL is a shell variable that contains the path to 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? | ||
# 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. `chmod 744 <path-to-the-script>` | ||
# 3. `./<path-to-the-script>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Comments in Shell Scripts | ||
|
||
## Overview | ||
|
||
Comments in shell scripts are non-executable lines that are meant for human readers. They are ignored by the shell when the script is executed. Adding comments to your code is crucial for explaining complex logic, providing context, and facilitating collaboration among developers. | ||
|
||
## Syntax | ||
|
||
In shell scripting, comments can be created using the `#` symbol. Anything following the `#` on a line is treated as a comment. | ||
|
||
```bash | ||
# This is a single-line comment | ||
``` | ||
|
||
## Placement | ||
|
||
Comments can be placed at the beginning of a line or after a command. They can also follow the actual code on the same line. | ||
|
||
```bash | ||
echo "Hello, World!" # This is a comment | ||
``` | ||
|
||
## Multiline Comments | ||
|
||
While shell scripting does not have a specific syntax for multiline comments, you can achieve a similar effect by placing `#` at the beginning of each line. | ||
|
||
```bash | ||
# This is a multiline | ||
# comment in a shell script | ||
``` | ||
|
||
## Best Practices | ||
|
||
Follow these best practices to maximize the effectiveness of comments in your shell scripts: | ||
|
||
- **Be Clear and Concise:** Write comments that are easy to understand and provide valuable information. | ||
- **Update Comments:** Keep comments up-to-date as you make changes to the script to ensure accuracy. | ||
- **Avoid Over-commenting:** Focus on explaining complex sections or unusual code rather than commenting on every line. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
|
||
# Author: Samir M. | ||
# Date: 2024-03-02 | ||
# Modified: 2024-03-02 | ||
# Description: Comments in bash script. | ||
# Usage: ./02.sh | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# Comments used to describe the script or the code. | ||
|
||
# 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 | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# How to run the script: | ||
# 1. Open the terminal | ||
# 2. `chmod 744 <path-to-the-script>` | ||
# 3. `./<path-to-the-script>` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
# Author: Samir M. | ||
# Date: 2024-03-02 | ||
# Modified: 2024-03-02 | ||
# Description: Make your script more professional. | ||
# Usage: ./04.sh | ||
|
||
#-------------------------------------------------------------# | ||
|
||
# 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 | ||
|
||
exit 0 | ||
|
||
#-------------------------------------------------------------# | ||
# How to run the script: | ||
# 1. Open the terminal | ||
# 2. `chmod 744 <path-to-the-script>` | ||
# 3. `./<path-to-the-script>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# The `$PATH` Variable | ||
|
||
The `$PATH` variable serves as a `list of directories` that the `shell searches`, in sequence, `for executable files` when a command is entered. It plays a pivotal role in determining which programs are accessible and executable directly from the command line. | ||
|
||
## Understanding `$PATH` | ||
|
||
In Bash, `$PATH` is a colon-separated list of directory paths. When a command is executed, the shell looks for the corresponding executable file in each directory specified in `$PATH`, in the order they are listed. | ||
|
||
## Default `$PATH` | ||
|
||
Upon startup, Bash initializes the `$PATH` variable with a default set of directories commonly containing system executables. These directories typically include `/usr/bin`, `/bin`, `/usr/local/bin`, and others. | ||
|
||
## Modifying `$PATH` | ||
|
||
Users can modify the `$PATH` variable to include additional directories or reorder the existing ones. This enables customization of the command search path, allowing users to prioritize specific directories or include custom script directories. | ||
|
||
```bash | ||
export PATH=$PATH:/<PATH-TO-YOUR-DIRECTORY> | ||
``` | ||
|
||
## Using `$PATH` | ||
|
||
Understanding and effectively utilizing the `$PATH` variable is essential for: | ||
|
||
- Accessing and executing programs and scripts directly from the command line. | ||
- Organizing custom scripts and executables in user-defined directories. | ||
- Facilitating system administration and development tasks by customizing the command search path. | ||
|
||
## Best Practices | ||
|
||
Follow these best practices when working with the `$PATH` variable: | ||
|
||
- **Be cautious when modifying `$PATH`:** Ensure that directories added to `$PATH` contain only trusted executables to avoid security risks. | ||
- **Keep `$PATH` organized:** Limit the number of directories included in `$PATH` to maintain a clear and manageable command search path. | ||
- **Document customizations:** Document any modifications made to `$PATH` for future reference and collaboration. |
Oops, something went wrong.