Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for Bash Scripting now.

Bash Scripting

Class length: 18 weeks. Start anytime.

Creator: kday

Status: Established

Join this class!

Lesson 5: Assignment 1

Read chapter 6 and do the examples.

Homework Submissions

3 total

rohshall (Self-grade: Outstanding)
Submitted 4 months ago | Permalink | Time spent: 2 hours

/usr/bin/env sh

re="([a-zA-Z ]) - ([0-9]) - (.*)$"

while read cdtrack; do if [[ "$cdtrack" =~ $re ]]; then echo Track ${BASH_REMATCH[2]} is ${BASH_REMATCH[3]} fi done < tracks.txt

The cookbook does not say this because it predates the version 3.2, but using quotes in RHS in =~ makes it a string comparison rather than a regex match. That is why RE needs to be stored in a variable first.

kday (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 20 minutes

Complete

johnny (Self-grade: Could be better)
Submitted 1 year ago | Permalink

I "graded" myself ahead of time, while I'm reading the chapter I thought I would post some things I thought where interesting:

  • double bracket if tests enable regular expression testing: I see this all the time in scripts online but I don't think many script writers understand the implications of double brackets compared to single brackets.

  • expressions inside parentheses that evaluate to a nonzero status will return a 0, a zero result from an expression returns a 1. Code like C or Java and while still operates correctly even though in bash exit status's are opposite of boolean values (1 is boolean true but exit status of error, 0 is boolean false but exit status of OK)