$_ CLI In Depth
Learning progress 0%
Build · Environment, PATH & Dotfiles

Build: A Dotfiles Installer with Backups

Build install.sh in three steps: link dotfiles, move conflicting files into a backup, and refuse a missing source.

~40 min Intermediate 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Link every dotfile

Create install.sh in /workspace. It takes a source directory and a destination directory, and it links every dotfile from the source into the destination:

BASH12 lines
cat > /workspace/install.sh <<'EOF'
#!/bin/bash
set -eu
src=$1
dest=$2
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  name=$(basename "$f")
  ln -sfn "$(readlink -f "$f")" "$dest/$name"
  echo "linked $name"
done
EOF

Run bash install.sh /workspace/src /workspace/home. Then /workspace/home/.bashrc and /workspace/home/.vimrc should be symbolic links.

Step 2: Move conflicting files into a backup

Replace install.sh with a version that moves any existing entry that does not already link to the source into .dotfiles-backup, and then creates the link. Rerunning must leave correct links alone:

BASH17 lines
cat > /workspace/install.sh <<'EOF'
#!/bin/bash
set -eu
src=$1
dest=$2
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done
EOF

The setup creates a real /workspace/home/.bashrc that holds export OLD=1. After you run the script, the backup should hold that original file, and .bashrc should be a link to the source.

Step 3: Refuse a missing source directory

Replace install.sh with the final version. It stops with a message and a non-zero status when the source directory does not exist, and it creates nothing in that case:

BASH19 lines
cat > /workspace/install.sh <<'EOF'
#!/bin/bash
# usage: install.sh SRC_DIR DEST_DIR
set -eu
src=${1:?usage: install.sh SRC_DIR DEST_DIR}
dest=${2:?usage: install.sh SRC_DIR DEST_DIR}
[ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; }
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done
EOF

Run bash install.sh /workspace/nope /workspace/home-x. It should fail and leave no /workspace/home-x directory.

Files

  • install.sh: the finished script.
  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

install.sh

BASH17 lines
#!/bin/bash
# usage: install.sh SRC_DIR DEST_DIR
set -eu
src=${1:?usage: install.sh SRC_DIR DEST_DIR}
dest=${2:?usage: install.sh SRC_DIR DEST_DIR}
[ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; }
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done

walkthrough.sh

BASH76 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Link every dotfile
# (the lab prepares this first)
# rm -rf /workspace/src /workspace/home /workspace/home-x
# mkdir -p /workspace/src /workspace/home
# printf &#039;export NEW=1\n&#039; > /workspace/src/.bashrc
# printf &#039;set -o vi\n&#039; > /workspace/src/.vimrc
# 
cat > /workspace/install.sh <<&#039;EOF&#039;
#!/bin/bash
set -eu
src=$1
dest=$2
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  name=$(basename "$f")
  ln -sfn "$(readlink -f "$f")" "$dest/$name"
  echo "linked $name"
done
EOF

# Step 2: Move conflicting files into a backup
# (the lab prepares this first)
# rm -rf /workspace/src /workspace/home /workspace/home-x
# mkdir -p /workspace/src /workspace/home
# printf &#039;export NEW=1\n&#039; > /workspace/src/.bashrc
# printf &#039;set -o vi\n&#039; > /workspace/src/.vimrc
# printf &#039;export OLD=1\n&#039; > /workspace/home/.bashrc
# 
cat > /workspace/install.sh <<&#039;EOF&#039;
#!/bin/bash
set -eu
src=$1
dest=$2
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done
EOF

# Step 3: Refuse a missing source directory
# (the lab prepares this first)
# rm -rf /workspace/src /workspace/home /workspace/home-x
# mkdir -p /workspace/src /workspace/home
# printf &#039;export NEW=1\n&#039; > /workspace/src/.bashrc
# printf &#039;set -o vi\n&#039; > /workspace/src/.vimrc
# printf &#039;export OLD=1\n&#039; > /workspace/home/.bashrc
# 
cat > /workspace/install.sh <<&#039;EOF&#039;
#!/bin/bash
# usage: install.sh SRC_DIR DEST_DIR
set -eu
src=${1:?usage: install.sh SRC_DIR DEST_DIR}
dest=${2:?usage: install.sh SRC_DIR DEST_DIR}
[ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; }
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done
EOF

Lessons this build uses