seed: curriculum content
This commit is contained in:
45
1.solar-system/1.welcome/09.the-scripter/index.md
Normal file
45
1.solar-system/1.welcome/09.the-scripter/index.md
Normal file
@@ -0,0 +1,45 @@
|
||||
---
|
||||
type: challenge
|
||||
title: "The Scripter"
|
||||
xp: 100
|
||||
duration: 35
|
||||
difficulty: 3
|
||||
---
|
||||
|
||||
# The Scripter
|
||||
|
||||
> **[INCOMING — Mission Control, Earth]**
|
||||
>
|
||||
> Cadet, time to combine commands. Bash lets you embed the output of
|
||||
> one command inside another using `$(...)` — *command substitution*:
|
||||
>
|
||||
> ```bash
|
||||
> echo "Today is $(date '+%Y-%m-%d')"
|
||||
> # → Today is 2026-05-04
|
||||
> ```
|
||||
>
|
||||
> When your script runs, a directory `supplies/` exists with several
|
||||
> files. Produce `report.txt` containing exactly two lines:
|
||||
>
|
||||
> ```
|
||||
> Date: <today in YYYY-MM-DD format>
|
||||
> Files in supplies: <count of entries in supplies/>
|
||||
> ```
|
||||
>
|
||||
> Two helpers:
|
||||
>
|
||||
> - `date '+%Y-%m-%d'` — today in the right format
|
||||
> - `ls supplies | wc -l | tr -d ' '` — entry count, whitespace stripped
|
||||
>
|
||||
> [END TRANSMISSION]
|
||||
|
||||
## Your Task
|
||||
|
||||
In `starter/starter.sh`, use `echo` + `$(...)` substitution to
|
||||
produce the two-line report.
|
||||
|
||||
## Objectives
|
||||
|
||||
- `report.txt` exists
|
||||
- Line 1 matches `Date: <today>`
|
||||
- Line 2 matches `Files in supplies: <count>`
|
||||
17
1.solar-system/1.welcome/09.the-scripter/starter/starter.sh
Normal file
17
1.solar-system/1.welcome/09.the-scripter/starter/starter.sh
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
# The Scripter — combine commands into a script.
|
||||
#
|
||||
# When this script runs, a directory `supplies/` exists with several
|
||||
# files. Your script must produce `report.txt` containing exactly two
|
||||
# lines:
|
||||
#
|
||||
# Date: <today, in YYYY-MM-DD format>
|
||||
# Files in supplies: <number of entries in supplies/>
|
||||
#
|
||||
# Use command substitution `$(...)` to embed command output inside
|
||||
# strings:
|
||||
#
|
||||
# $(date '+%Y-%m-%d')
|
||||
# $(ls supplies | wc -l | tr -d ' ')
|
||||
|
||||
# Your code here.
|
||||
21
1.solar-system/1.welcome/09.the-scripter/testing/test.sh
Normal file
21
1.solar-system/1.welcome/09.the-scripter/testing/test.sh
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
mkdir -p supplies
|
||||
touch supplies/oxygen-tank.txt \
|
||||
supplies/ration-pack.txt \
|
||||
supplies/medkit.txt \
|
||||
supplies/repair-kit.txt \
|
||||
supplies/comms-unit.txt
|
||||
|
||||
bash solution.sh > /dev/null 2>&1
|
||||
|
||||
N=0
|
||||
report() { N=$((N+1)); if [ "$1" = "0" ]; then echo "ok $N - $2"; else echo "not ok $N - $2"; fi; }
|
||||
|
||||
[ -f report.txt ]; report $? "report.txt exists"
|
||||
|
||||
TODAY=$(date '+%Y-%m-%d')
|
||||
LINE1=$(sed -n '1p' report.txt 2>/dev/null)
|
||||
LINE2=$(sed -n '2p' report.txt 2>/dev/null)
|
||||
|
||||
[ "$LINE1" = "Date: $TODAY" ]; report $? "line 1 is 'Date: $TODAY'"
|
||||
[ "$LINE2" = "Files in supplies: 5" ]; report $? "line 2 is 'Files in supplies: 5'"
|
||||
Reference in New Issue
Block a user