scripting

All posts tagged scripting by Linux Bash
  • Posted on
    Featured Image
    Learn how Model Context Protocol (MCP) turns Bash scripts into safe, AI-discoverable tools with typed parameters, centralized policies, and timeouts. Build a minimal Python MCP server (sys_info, tail_log, service_action, backup_dir), install via apt/dnf/zypper, wire to Cursor or VS Code+Cline, then apply guardrails (whitelists, read-only vs mutating, JSON outputs, logging) with troubleshooting and real-world tasks.
  • Posted on
    Featured Image
    The blog delves into controlling GPIO pins in Linux via the `sysfs` interface, explaining its use in user space for simple file operations on pins. Steps covered include exporting the pin, setting its direction, and manipulating its state. The guide includes examples like LED control and a script for blinking an LED, highlighting the ease and effectiveness of `sysfs` for basic hardware interfacing.
  • Posted on
    Featured Image
    The blog post explores the `FUNCNAME` array in Bash, highlighting its importance in debugging by tracing function call sequences. It shows how `FUNCNAME` aids in understanding script execution paths and function hierarchies, enhancing script management and error diagnosis for both novice and experienced scripters. Various examples demonstrate its practical applications in real-world scripting scenarios.
  • Posted on
    Featured Image
    This article explores managing Linux kernel modules using commands `modprobe` for loading modules with dependencies, and `rmmod` for unloading them without dependency management. Tools like `lsmod` are also discussed for tracking active modules. Practical examples and a script for automating tasks, such as managing the `vboxdrv` module, are provided, underscoring their utility for system administrators and developers.
  • Posted on
    Featured Image
    The blog explores using the `declare -p` command in Bash for variable serialization to facilitate data sharing across scripts, enhancing script interaction and maintainability. It details how `declare -p` allows exporting all variable types, including arrays, and demonstrates practical examples of sharing simple variables and complex data structures between scripts, improving data continuity and script dependencies management.
  • Posted on
    Featured Image
    This article discusses Bash process substitution, highlighting the challenge of capturing stderr within it, such as in `diff <(cmd 2>&1)`. Normally, stderr directly outputs to the terminal, but using redirection (`2>&1`), stderr can be merged with stdout for comprehensive command comparisons, crucial for debugging and automation in scripting. Examples demonstrate how to manage output streams effectively in Bash scripts.
  • Posted on
    Featured Image
    The blog post details using the `lsblk --json` command in Linux to programmatically manage block devices. It explains `lsblk`'s importance in viewing device relationships and its JSON output option for easy scripting. The article provides simple `lsblk` usage examples, an executable script using `jq` to extract device names and sizes, and discusses the command's integration into automation scripts for efficient storage management in system administration and DevOps.
  • Posted on
    Featured Image
    The article explains how to capture the Process ID (PID) of background processes in a Bash environment, especially when using pipelines. It highlights the challenges and complexities involved in capturing PIDs in such scenarios and provides practical examples of managing these processes, including monitoring their status and gracefully stopping them. The use of the special variable `$!` to obtain the PID of the last job in the background is demonstrated with scripts, aiding in robust system operations and process management.
  • Posted on
    Featured Image
    The blog post explores the `mktemp` utility, particularly focusing on the `mktemp -u` command for generating unique temporary filenames without creating actual files on Linux and Unix-like systems. This is crucial for reserving filenames for later use, enhancing script safety and efficiency. The post discusses utility installation, provides usage examples, and differentiates between `mktemp` and `mktemp -u`, emphasizing its importance in system management and security.
  • Posted on
    Featured Image
    Detecting mounted filesystems in Linux typically requires parsing `/proc/mounts`. However, `findmnt` from the `util-linux` package presents a robust alternative. This command omits manual file parsing, can list all mounted filesystems, supports filtering, and allows formatted output, which is ideal for scripting, thus making it a secure and adaptable method.
  • Posted on
    Featured Image
    The article explains using the `stat -c %y` command in Linux to check file modification times, vital for system admins and developers. It covers the command's basics and integration into Bash scripts for automated monitoring, such as checking if a file has been modified within the last hour. It also touches on checking file size and inode numbers, concluding with `stat` being generally pre-installed in Linux distributions.
  • Posted on
    Featured Image
    This article provides guidance on managing tricky filenames in Linux Bash, like those with newlines, spaces, or leading dashes, which can disrupt script performance and pose security risks. It recommends quoting filenames, using `find ... -exec` for safe operations, and handling leading dashes with `--`, exemplified by safely using `xargs` for deletion and removing a file like `-myfile.txt` with `rm --`. Skills in handling these filenames increase script robustness and security.
  • Posted on
    Featured Image
    The article discusses the `coproc` keyword in Bash, which was introduced in version 4.0 for bidirectional communication with subprocesses, allowing dynamic two-way data exchanges. This enhances script functionality by enabling complex operations such as text processing and real-time calculations, demonstrated with use cases like an echo server using `cat` and arithmetic operations with `bc`.
  • Posted on
    Featured Image
    The article discusses the `compgen` command in Bash, a useful tool for listing variables with a specified prefix. Using `compgen -A variable USER`, for example, outputs variables like `USER`, `USERNAME`, and `USER_ID`. The command is particularly beneficial for writing scripts that include auto-completion features, making them more robust and user-friendly. The article also highlights other uses of `compgen` and provides resources for further learning on Bash scripting.
  • Posted on
    Featured Image
    In Bash scripting, effectively handling errors is key to robust automation. The article explores using `ERR` traps to manage errors selectively within scripts. Setting `ERR` traps for specific commands allows for differentiated error handling, ensuring critical operations receive appropriate attention, thus improving script reliability and clarity.
  • Posted on
    Featured Image
    The blog article explains the usage of the `declare -n` command in Bash, a feature that enhances script flexibility by creating indirect references to variables. This is particularly useful for dynamic scenarios, like updating variables through functions without preset names, swapping values, or customizable output locations in functions. The article emphasizes the necessity of using Bash version 4.3 or newer to effectively leverage this capability, which, while powerful, requires cautious use to maintain script readability.
  • Posted on
    Featured Image
    Introduced in Bash 4.3, nameref variables enhance scripting by creating aliases to other variables, thus allowing dynamic data manipulation. Declaring a nameref variable involves using `declare -n`, linking it to an existing variable. Changes to either the nameref or its target affect both, useful in scripts needing flexible configurations.
  • Posted on
    Featured Image
    The article emphasizes the importance of Linux Bash for tech professionals aiming for open-source certifications to boost their careers. Covering Bash's role in automation and system management, it details certifications like LPIC, RHCE, and CompTIA Linux+. Training platforms such as Linux Academy and Coursera are recommended, along with the advice of gaining hands-on experience through real-world projects.
  • Posted on
    Featured Image
    The article discusses integrating the Linux Bash shell into digital literacy programs, emphasizing its value in enhancing technological education access, fostering innovation through community projects, and aligning with real-world tech environments. Bash offers practical computing, scripting, and collaborative learning opportunities, despite challenges like its steep learning curve and the need for experienced instructors.
  • Posted on
    Featured Image
    This article discusses the benefits of teaching children programming using Linux Bash, highlighting its accessibility, simplicity, and real-world application. It covers setting up a learning environment, proposes educational projects like creating directories and simple games, and offers teaching tips. The guide emphasizes how programming prepares kids for a technological future through practical, interactive lessons using open source tools.