AWS re/Start Lab · Linux

Bash Shell Scripting Exercise

This challenge lab required the creation of an automated Bash script capable of generating batches of 25 empty files. The script uses logic to determine the highest existing numbered file and increment the next batch accordingly, heavily utilizing tools like sed, sort, and seq.

Lab Summary

Connected to the EC2 instance via PuTTY following the process described in Lab 225. The core deliverable was a shell script that dynamically identified existing files to avoid overwriting, before using a for loop to generate 25 new sequentially numbered files.

The Bash Script Logic

The custom script designed to tackle the incremental automation requirement.

script.sh
#!/bin/bash

# Isolate the highest existing number by stripping the string 'quijano'
ultimo=$(ls quijano* 2</dev/null | sed 's/quijano//' | sort -n | tail -1)

# Set to 0 if no files exist yet
[ -z "$ultimo" ] && ultimo=0

# Create a sequence of 25 files starting from the isolated number
for i in $(seq $((ultimo+1)) $((ultimo+25)))
do
  touch "quijano$i"
done

# Verify creation
ls -l quijano*

Execution & Verification

01

Writing the script

  • Created the script using the nano text editor.
  • Implemented error suppression on the `ls` command (`2>/dev/null`) to prevent warnings when no files exist on the first run.
02

First execution (Batch 1-25)

  • Saved the script and added execute permissions (chmod +x).
  • Ran the script (./script.sh). It successfully identified the absence of previous files and created files numbered from 1 to 25.
03

Incremental validation (Batch 26-50)

  • Ran the script a second time. The automation successfully identified `25` as the highest number.
  • The `seq` logic incremented correctly, creating the next batch from 26 to 50.
04

Final verification

  • Used a standard ls command to view the directory contents, confirming all 50 files existed without any name collisions.

Command Reference

cmd

sed

Stream editor used for filtering and transforming text.

  • sed 's/quijano//' : Substitutes (removes) the word "quijano" from the string to isolate the number.
cmd

sort

Sorts lines of text files or streams.

  • -n : Evaluates strings as numerical values instead of standard alphabetic sorting.
cmd

seq

Prints a sequence of numbers.

  • seq START END : Outputs every integer between START and END. Crucial for bash `for` loops.

Key Learnings

What Was Actually Learned

How to suppress `stderr` output using 2>/dev/null.
How to use sed to clean strings dynamically inside a bash variable assignment.
How to use bash conditional expressions [ -z ] to check for empty variables.
How to combine seq and arithmetic expansion $((...)) in a for loop.

Technical Conclusion

This challenge demonstrated that bash scripting is highly capable of self-aware execution. By reading from the environment prior to acting, scripts can be made idempotent or appropriately incremental.

String isolation is key to this logic: removing the alphabetical prefix to extract the raw integer allows mathematical sorting. Furthermore, anticipating the failure state (like running the script the very first time when no prior numbers exist) ensures robust automation.