show_env.sh
Overview
show_env.sh is a Bash script designed to gather and display key environment and system information relevant for development and debugging purposes. It collects details such as the current Git repository name and commit ID, operating system distribution and version, CPU type, memory size, Docker version, and Python version. This script helps users quickly assess their runtime environment, particularly useful in software development, deployment, or troubleshooting contexts.
Detailed Explanation
Functions
get_distro_info()
Purpose:
Retrieves the Linux distribution ID and version along with the current kernel version.Implementation details:
Attempts to use
lsb_releaseto get distribution info (IDandVERSION).If
lsb_releaseis unavailable or returns empty, falls back to parsing/etc/*-releasefiles forIDandVERSION_ID.Obtains the kernel version using
uname -r.Returns a string combining the distribution ID, version, and kernel version.
Parameters:
NoneReturns:
Outputs a single string in the format:<distro_id> <distro_version> (Kernel version: <kernel_version>)Usage example:
os_info=$(get_distro_info) echo "Operating system: $os_info"
Script Variables and Commands
Variable/Command | Purpose | Details |
|---|---|---|
| Name of the current Git repository | Uses |
| CPU architecture/type | Obtained via |
| Total system memory | Extracted from the output of |
| Installed Docker version | Checks if |
| Installed Python version | Attempts to get Python 3 version, falls back to |
| Commit ID of the latest Git commit | Uses |
Output
The script prints all gathered information to standard output in the following order:
Current Git repository name (or message if not in a Git repo)
Current Git commit ID (or message if not available)
Operating system distribution, version, and kernel version
CPU type
Memory size
Docker version
Python version
This output provides a snapshot of the development environment.
Important Implementation Details
Robustness with fallback mechanisms:
The script uses multiple methods to obtain system info, e.g., it trieslsb_releasefirst, then falls back to parsing/etc/*-releasefiles to maximize compatibility across Linux distros.Error handling:
Where commands might fail (e.g., not a Git repo, missing Docker), the script sets clear, user-friendly messages rather than failing silently or crashing.Command checks:
Usescommand -vto check command availability (docker), and redirect error output to/dev/nullto avoid clutter.Use of standard Unix utilities:
Relies on standard commands likegrep,cut,awk,uname,free, andgitto extract information efficiently.
Interaction with Other System Components
Git:
The script interacts with Git repositories by querying the repository name and latest commit hash. It requires that the current directory be part of a Git repo and that Git is installed.Docker:
Detects installed Docker version if Docker is available in the system PATH.Operating System:
Uses OS-level commands and files to get distribution and kernel information.Python:
Queries the installed Python interpreter version, tryingpython3first, thenpython.
This script is typically run from a shell environment within a development machine or container and does not modify system state.
Usage Example
./show_env.sh
Sample output might be:
Current Repository: my-project
Commit Id: a1b2c3d
Operating system: Ubuntu 22.04 (Kernel version: 5.15.0-50-generic)
CPU Type: x86_64
Memory: 15Gi
Docker Version: 20.10.7
Python Version: Python 3.10.4
Mermaid Diagram: Flowchart of Main Steps and Data Flow
flowchart TD
Start((Start))
A[get_distro_info()]
B[Check if inside Git repo]
C[Get Git repo name]
D[Get latest commit ID]
E[Get CPU type]
F[Get Memory size]
G[Check Docker installation & version]
H[Check Python installation & version]
I[Print all collected info]
End((End))
Start --> A
A --> B
B -->|Yes| C
B -->|No| C
C --> D
D --> E
E --> F
F --> G
G --> H
H --> I
I --> End
Summary
show_env.sh is a utility script that provides a concise environmental snapshot by combining system, Git, Docker, and Python details. It is useful for developers to verify their current environment quickly and can be integrated into larger scripts or CI/CD pipelines for environment validation.
Notes
Requires Git for full functionality.
For optimal results, run in a Linux environment.
Python and Docker versions are only reported if those tools are installed and accessible in the shell environment.