random updates
This commit is contained in:
223
docs/tech_docs/linux/bash.md
Normal file
223
docs/tech_docs/linux/bash.md
Normal file
@@ -0,0 +1,223 @@
|
||||
### 1. Bash Startup Files
|
||||
|
||||
- **`~/.bash_profile`, `~/.bash_login`, and `~/.profile`**: Used for login shells.
|
||||
- **`~/.bashrc`**: Used for non-login shells. Essential for setting environment variables, aliases, and functions that are used across sessions.
|
||||
|
||||
### 2. Shell Scripting
|
||||
|
||||
- **Variables and Quoting**: Discusses how to correctly use and quote variables to avoid common pitfalls.
|
||||
- **Conditional Execution**: Covers the use of `if`, `else`, `elif`, `case` statements, and the `[[ ]]` construct for test operations.
|
||||
- **Loops**: Explains `for`, `while`, and `until` loops, with examples on how to iterate over lists, files, and command outputs.
|
||||
- **Functions**: How to define and use functions in scripts for reusable code.
|
||||
- **Script Debugging**: Using `set -x`, `set -e`, and other options to debug shell scripts.
|
||||
|
||||
### 3. Advanced Command Line Tricks
|
||||
|
||||
- **Brace Expansion**: Using `{}` for generating arbitrary strings.
|
||||
- **Command Substitution**: Using `$(command)` or `` `command` `` to capture the output of a command.
|
||||
- **Process Substitution**: Utilizes `<()` and `>()` for treating the output or input of a command as a file.
|
||||
- **Redirection and Pipes**: Advanced uses of `>`, `>>`, `<`, `|`, and `tee` for controlling input and output streams.
|
||||
|
||||
### 4. Job Control
|
||||
|
||||
- **Foreground and Background Jobs**: Using `fg`, `bg`, and `&` to manage jobs.
|
||||
- **Job Suspension**: Utilizing `Ctrl+Z` to suspend jobs and `jobs` to list them.
|
||||
|
||||
### 5. Text Processing Tools
|
||||
|
||||
- **`grep`, `awk`, `sed`**: Mastery of these tools for text processing and data extraction.
|
||||
- **Regular Expressions**: Advanced patterns and their applications in text processing commands.
|
||||
|
||||
### 6. Networking Commands
|
||||
|
||||
- **`ssh`, `scp`, `curl`, and `wget`**: For remote access, file transfer, and downloading content from the internet.
|
||||
- **`netstat`, `ping`, `traceroute`**: Basic networking diagnostics tools.
|
||||
|
||||
### 7. System Administration
|
||||
|
||||
- **File Permissions and Ownership**: Advanced manipulation with `chmod`, `chown`, and `chgrp`.
|
||||
- **Process Management**: Using `ps`, `top`, `htop`, `kill`, `pkill`, and `killall` for process monitoring and management.
|
||||
- **Disk Usage**: Utilizing `df`, `du`, and `lsblk` to monitor disk space and file system usage.
|
||||
|
||||
### 8. Environment Customization
|
||||
|
||||
- **Aliases and Functions**: Creating efficient shortcuts and reusable commands.
|
||||
- **Prompt Customization**: Modifying the Bash prompt (`PS1`) for better usability and information display.
|
||||
|
||||
### 9. Package Management
|
||||
|
||||
- **For Linux**: Using package managers like `apt`, `yum`, or `dnf`.
|
||||
- **For macOS**: Utilizing `brew` (Homebrew) for package management.
|
||||
|
||||
### 10. Security
|
||||
|
||||
- **File Encryption**: Using tools like `gpg` for encrypting and decrypting files.
|
||||
- **SSH Keys**: Generating and managing SSH keys for secure remote access.
|
||||
|
||||
### Conclusion and Resources
|
||||
|
||||
Conclude with the importance of continuous learning and experimentation in mastering Bash. Provide resources for further exploration, such as the GNU Bash manual, advanced scripting guides, and forums like Stack Overflow.
|
||||
|
||||
This structure should provide a comprehensive guide for advanced CLI users to deepen their mastery of Bash on Linux and macOS systems. Each section can be expanded with examples, best practices, and detailed explanations tailored to advanced users' needs.
|
||||
|
||||
---
|
||||
|
||||
To create a practical and instructional guide for power users of the CLI, let's provide sample shell scripts and commands that embody the key areas of focus. These examples will help to solidify understanding and demonstrate the utility of Bash in various common scenarios.
|
||||
|
||||
### 1. Bash Startup Files
|
||||
|
||||
```bash
|
||||
# ~/.bash_profile example
|
||||
if [ -f ~/.bashrc ]; then
|
||||
source ~/.bashrc
|
||||
fi
|
||||
|
||||
export PATH="$PATH:/opt/bin"
|
||||
alias ll='ls -lah'
|
||||
```
|
||||
|
||||
### 2. Shell Scripting
|
||||
|
||||
- **Variables and Quoting**:
|
||||
|
||||
```bash
|
||||
greeting="Hello, World"
|
||||
echo "$greeting" # Correctly quotes the variable.
|
||||
```
|
||||
|
||||
- **Conditional Execution**:
|
||||
|
||||
```bash
|
||||
if [[ -f "$file" ]]; then
|
||||
echo "$file exists."
|
||||
elif [[ -d "$directory" ]]; then
|
||||
echo "$directory is a directory."
|
||||
else
|
||||
echo "Nothing found."
|
||||
fi
|
||||
```
|
||||
|
||||
- **Loops**:
|
||||
|
||||
```bash
|
||||
# Iterate over files
|
||||
for file in *.txt; do
|
||||
echo "Processing $file"
|
||||
done
|
||||
|
||||
# While loop
|
||||
counter=0
|
||||
while [[ "$counter" -lt 10 ]]; do
|
||||
echo "Counter: $counter"
|
||||
((counter++))
|
||||
done
|
||||
```
|
||||
|
||||
- **Functions**:
|
||||
|
||||
```bash
|
||||
greet() {
|
||||
echo "Hello, $1"
|
||||
}
|
||||
greet "World"
|
||||
```
|
||||
|
||||
- **Script Debugging**:
|
||||
|
||||
```bash
|
||||
set -ex # Exit on error and print commands and their arguments as they are executed.
|
||||
```
|
||||
|
||||
### 3. Advanced Command Line Tricks
|
||||
|
||||
- **Brace Expansion**:
|
||||
|
||||
```bash
|
||||
cp /path/to/source/{file1,file2,file3} /path/to/destination/
|
||||
```
|
||||
|
||||
- **Command Substitution**:
|
||||
|
||||
```bash
|
||||
current_dir=$(pwd)
|
||||
echo "You are in $current_dir"
|
||||
```
|
||||
|
||||
- **Process Substitution**:
|
||||
|
||||
```bash
|
||||
diff <(ls dir1) <(ls dir2)
|
||||
```
|
||||
|
||||
- **Redirection and Pipes**:
|
||||
|
||||
```bash
|
||||
grep 'error' logfile.txt | tee errorlog.txt
|
||||
```
|
||||
|
||||
### 4. Job Control
|
||||
|
||||
```bash
|
||||
# Run a command in the background
|
||||
long_running_process &
|
||||
|
||||
# Bring the last job to the foreground
|
||||
fg
|
||||
|
||||
# Suspend the current job
|
||||
Ctrl+Z
|
||||
|
||||
# List jobs
|
||||
jobs
|
||||
```
|
||||
|
||||
### 5. Text Processing Tools
|
||||
|
||||
- Using `awk` to sum the first column of a file:
|
||||
|
||||
```bash
|
||||
awk '{ sum += $1 } END { print sum }' numbers.txt
|
||||
```
|
||||
|
||||
### 6. Networking Commands
|
||||
|
||||
- Secure file transfer:
|
||||
|
||||
```bash
|
||||
scp localfile.txt user@remotehost:/path/to/destination/
|
||||
```
|
||||
|
||||
### 7. System Administration
|
||||
|
||||
- Monitoring disk usage:
|
||||
|
||||
```bash
|
||||
df -h # Human-readable disk space of file systems
|
||||
du -sh /path/to/directory # Disk usage of the specified directory
|
||||
```
|
||||
|
||||
### 8. Environment Customization
|
||||
|
||||
- Customizing the Bash prompt:
|
||||
|
||||
```bash
|
||||
export PS1='\u@\h:\w\$ '
|
||||
```
|
||||
|
||||
### 9. Package Management
|
||||
|
||||
- Installing a package on Linux (Debian/Ubuntu):
|
||||
|
||||
```bash
|
||||
sudo apt-get update && sudo apt-get install packagename
|
||||
```
|
||||
|
||||
### 10. Security
|
||||
|
||||
- Generating an SSH key pair:
|
||||
|
||||
```bash
|
||||
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
|
||||
```
|
||||
|
||||
Each of these sections and examples can be further detailed and expanded upon in a comprehensive guide. The intention is to provide a solid foundation of practical Bash usage and scripting techniques, encouraging further exploration and mastery of the shell environment. Continuous learning and experimentation are key to becoming proficient in Bash scripting and command-line usage.
|
||||
67
docs/tech_docs/linux/script_folder.md
Normal file
67
docs/tech_docs/linux/script_folder.md
Normal file
@@ -0,0 +1,67 @@
|
||||
Organizing, naming, and storing shell scripts, especially for system administration tasks, require a systematic approach to ensure ease of maintenance, scalability, and accessibility. When using Git for version control, it becomes even more crucial to adopt best practices for structure and consistency. Here's a comprehensive guide on organizing system reporting scripts and other utility scripts for a single user, leveraging Git for version control.
|
||||
|
||||
### Directory Structure
|
||||
|
||||
Organize your scripts into logical directories within a single repository. A suggested structure could be:
|
||||
|
||||
```plaintext
|
||||
~/scripts/
|
||||
│
|
||||
├── system-reporting/ # Scripts for system reporting
|
||||
│ ├── disk-usage.sh
|
||||
│ ├── system-health.sh
|
||||
│ └── login-attempts.sh
|
||||
│
|
||||
├── on-demand/ # Scripts to run on demand for various tasks
|
||||
│ ├── update-check.sh
|
||||
│ ├── backup.sh
|
||||
│ ├── service-monitor.sh
|
||||
│ └── network-info.sh
|
||||
│
|
||||
└── greetings/ # Scripts that run at login or when a new terminal is opened
|
||||
└── greeting.sh
|
||||
```
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
- Use lowercase and describe the script's purpose clearly.
|
||||
- Use hyphens to separate words for better readability (`disk-usage.sh`).
|
||||
- Include a `.sh` extension to indicate that it's a shell script, though it's not mandatory for execution.
|
||||
|
||||
### Script Storage and Version Control
|
||||
|
||||
1. **Central Repository**: Store all your scripts in a Git repository located in a logical place, such as `~/scripts/`. This makes it easier to track changes, revert to previous versions, and share your scripts across systems.
|
||||
|
||||
2. **README Documentation**: Include a `README.md` in each directory explaining the purpose of each script and any dependencies or special instructions. This documentation is crucial for maintaining clarity about each script's functionality and requirements.
|
||||
|
||||
3. **Commit Best Practices**:
|
||||
- Commit changes to scripts with descriptive commit messages, explaining what was changed and why.
|
||||
- Use branches to develop new features or scripts, merging them into the main branch once they are tested and stable.
|
||||
|
||||
4. **Script Versioning**: Consider including a version number within your scripts, especially for those that are critical or frequently updated. This can be as simple as a comment at the top of the script:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# Script Name: system-health.sh
|
||||
# Version: 1.0.2
|
||||
# Description: Reports on system load, memory usage, and swap usage.
|
||||
```
|
||||
|
||||
5. **Use of Git Hooks**: Utilize Git hooks to automate tasks, such as syntax checking or automated testing of scripts before a commit is allowed. This can help maintain the quality and reliability of your scripts.
|
||||
|
||||
6. **Regular Backups and Remote Repositories**: Besides version control, regularly push your changes to a remote repository (e.g., GitHub, GitLab) for backup and collaboration purposes. This also allows you to easily synchronize your script repository across multiple machines.
|
||||
|
||||
### Execution and Accessibility
|
||||
|
||||
- **Permissions**: Ensure your scripts are executable by running `chmod +x scriptname.sh`.
|
||||
- **Path Accessibility**: To run scripts from anywhere, you can add the scripts directory to your `PATH` environment variable in your `~/.bashrc` or `~/.bash_profile` file:
|
||||
|
||||
```bash
|
||||
export PATH="$PATH:~/scripts"
|
||||
```
|
||||
|
||||
Alternatively, consider creating symbolic links for frequently used scripts in a directory that's already in your `PATH`.
|
||||
|
||||
- **Cron Jobs**: For scripts that need to run at specific times (e.g., backups, updates checks), use cron jobs to schedule their execution.
|
||||
|
||||
By adhering to these best practices for organizing, naming, storing, and version-controlling your shell scripts, you ensure a robust, maintainable, and scalable scripting environment that leverages the full power of Git and shell scripting for system administration tasks.
|
||||
43
docs/tech_docs/linux/shell_scripting.md
Normal file
43
docs/tech_docs/linux/shell_scripting.md
Normal file
@@ -0,0 +1,43 @@
|
||||
### 1. Bash Startup Files
|
||||
|
||||
Understanding Bash startup files is crucial for setting up your environment effectively:
|
||||
|
||||
- **`~/.bash_profile`, `~/.bash_login`, and `~/.profile`**: These files are read and executed by Bash for login shells. Here, you can set environment variables, and startup programs, and customize user environments that should be applied once at login.
|
||||
- **`~/.bashrc`**: For non-login shells (e.g., opening a new terminal window), Bash reads this file. It's the place to define aliases, functions, and shell options that you want to be available in all your sessions.
|
||||
|
||||
### 2. Shell Scripting
|
||||
|
||||
A foundational understanding of scripting basics enhances the automation and functionality of tasks:
|
||||
|
||||
- **Variables and Quoting**: Use variables to store data and quotations to handle strings containing spaces or special characters. Always quote your variables (`"$variable"`) to avoid unintended splitting and globbing.
|
||||
|
||||
- **Conditional Execution**:
|
||||
- Use `if`, `else`, `elif`, and `case` statements to control the flow of execution based on conditions.
|
||||
- The `[[ ]]` construct offers more flexibility and is recommended over `[ ]` for test operations.
|
||||
|
||||
- **Loops**:
|
||||
- `for` loops are used to iterate over a list of items.
|
||||
- `while` and `until` loops execute commands as long as the test condition is true (or false for `until`).
|
||||
- Example: `for file in *; do echo "$file"; done`
|
||||
|
||||
- **Functions**: Define reusable code blocks. Syntax: `myfunc() { command1; command2; }`. Call it by simply using `myfunc`.
|
||||
|
||||
- **Script Debugging**: Utilize `set -x` to print each command before execution, `set -e` to exit on error, and `set -u` to treat unset variables as an error.
|
||||
|
||||
### 3. Advanced Command Line Tricks
|
||||
|
||||
Enhance your command-line efficiency with these advanced techniques:
|
||||
|
||||
- **Brace Expansion**: Generates arbitrary strings, e.g., `file{1,2,3}.txt` creates `file1.txt file2.txt file3.txt`.
|
||||
|
||||
- **Command Substitution**: Capture the output of a command for use as input in another command using `$(command)` syntax. Example: `echo "Today is $(date)"`.
|
||||
|
||||
- **Process Substitution**: Treats the input or output of a command as if it were a file using `<()` and `>()`. Example: `diff <(command1) <(command2)` compares the output of two commands.
|
||||
|
||||
- **Redirection and Pipes**:
|
||||
- Redirect output using `>` for overwrite or `>>` for append.
|
||||
- Use `<` to redirect input from a file.
|
||||
- Pipe `|` connects the output of one command to the input of another.
|
||||
- `tee` reads from standard input and writes to standard output and files, useful for viewing and logging simultaneously.
|
||||
|
||||
This cheatsheet provides a concise overview of essential Bash scripting and command-line techniques, serving as a quick reference for advanced CLI users to enhance their productivity and scripting capabilities on Linux and macOS systems.
|
||||
Reference in New Issue
Block a user