Read a Filtered List of Files from a Directory and Ask the User to Choose One (Bash)

One of the standout features of Docksal is its extensibility through Bash scripting, making it a valuable tool for automating various tasks. A common scenario is the need to import databases when initializing projects. The snippet below serves this purpose by scanning a directory for database files and allowing the user to select which one to import. # Get a list of *.mysql | *.sql.gz | *.sql in /db/ and let the user choose which to import unset options i while IFS= read -r -d $'\0' f; do options[i++]="$f" done < <(find ${PROJECT_ROOT}/db/ -maxdepth 1 -type f \( -name "*.mysql" -o -name "*.sql.gz" -o -name "*.sql" \) -print0) echo "Press [Enter] to choose a DB to import" read -t5 -p "(times out in 5 seconds) " if [ $? -gt 0 ]; then echo "" echo "Timed out waiting for response. Skipping DB import..." else select opt in "${options[@]}" "Skip DB Import" do case $opt in *.mysql | *.sql.gz | *.sql) echo "Importing $opt ..." fin db import $opt break ;; "Skip DB Import") echo "Skipping DB Import..." break ;; *) echo "Invalid choice, please enter the number of your choice above." ;; esac done fi This script effectively searches a specified directory for files that match certain extensions and prompts the user to make a selection. The inclusion of a timeout is a thoughtful touch, especially for scripts designed to run unattended or as part of a larger automation workflow. ...

September 30, 2019 · 2 min · 369 words

How to Use the AWS CLI to List IP Addresses and Other Instance Information

When it comes to managing cloud infrastructure, clarity is paramount. Security officers, managers, and other stakeholders often seek a high-level summary of instances within their hosting environment. While visualizations can be enlightening, there are times when raw data is simply more useful. Enter the AWS Command Line Interface (CLI) — a powerful tool that streamlines this process with the describe-instances command. The basic command is straightforward and well-documented. To retrieve a comprehensive overview of your EC2 instances, you can run: ...

March 5, 2019 · 2 min · 282 words