Technotes

Technotes for future me

Loops

For loop

#!/bin/bash
for i in $(cat ips); do
echo quit | timeout --signal=9 3 telnet "$i" 443
done

for s in $ONE $TWO $THREE
do
    echo "Server ${s}"
done

Run a command for each entry

This script defines an array of IDs and uses a for loop to iterate over each element.

For every ID, it prints an command that includes the ID in the key path.

The use of "${inputlist[@]}" ensures each array element is handled safely, even if it contains spaces.

This approach is efficient for generating a series of similar commands programmatically, without reading from an external file.

#!/bin/bash

# Define the list of IDs in an array
inputlist=(
  "4c6a224d-97c3-4c19-974d-0b93f18546f2"
  "another-uuid-goes-here"
  "and-another-one"
)

echo "--- Generating 'etcdctl get' commands ---"
# Loop through each item and PRINT the command
for i in "${inputlist[@]}"; do
  echo "ETCDCTL_API=3 etcdctl get --prefix=true pwx/blaat-cluster/storage/backup/$i"
done

This Bash script reads each non-empty line from inputlist.txt and prints an etcdctl get command for each line. It first checks if the file exists; if not, it exits with an error. For every line in the file, it generates a command to fetch etcd keys with a specific prefix, using the line as part of the key path. The script only prints the commands, not executes them.

#!/bin/bash

inputlist_file="inputlist.txt"

if [ ! -f "$inputlist_file" ]; then
    echo "Error: File '$inputlist_file' not found."
    exit 1
fi

echo "--- Generating 'etcdctl get' commands from file '$inputlist_file' ---"
# Loop through each line and PRINT the command
while IFS= read -r i; do
  if [ -z "$i" ]; then continue; fi
  echo "ETCDCTL_API=3 etcdctl get --prefix=true pwx/blaat-cluster/storage/backup/$i"
done < "$inputlist_file"

For loop n times

REPETITIONS=3
for i in $(seq 1 $REPETITIONS)
do
  curl https://blaataap.com
done

While loop

while true
do
  curl -I -vk https://blaataap.com
  sleep 1
done
Last updated on 25 Jun 2025
Published on 25 Jun 2025
Edit on GitHub