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()


Script Variables and Commands

Variable/Command

Purpose

Details

git_repo_name

Name of the current Git repository

Uses git rev-parse to check if inside a Git repo and retrieve the repo top-level directory name. If not a Git repo or on error, sets appropriate message.

cpu_model

CPU architecture/type

Obtained via uname -m (e.g., x86_64, armv7l)

memory_size

Total system memory

Extracted from the output of free -h command, specifically the "Mem" line's total value (human-readable)

docker_version

Installed Docker version

Checks if docker command exists; if yes, extracts the version string; otherwise notes Docker is not installed

python_version

Installed Python version

Attempts to get Python 3 version, falls back to python version, or notes if Python is not installed

git_version

Commit ID of the latest Git commit

Uses git log -1 to get the abbreviated commit hash; if unavailable, shows a message


Output

The script prints all gathered information to standard output in the following order:

  1. Current Git repository name (or message if not in a Git repo)

  2. Current Git commit ID (or message if not available)

  3. Operating system distribution, version, and kernel version

  4. CPU type

  5. Memory size

  6. Docker version

  7. Python version

This output provides a snapshot of the development environment.


Important Implementation Details


Interaction with Other System Components

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