Sign inSign up free

752. Add a Line Only Once

medium 20 XP ~10 minHands-on lab

Provisioning scripts run again and again. Every run that blindly does echo "x" >> file adds another copy. Write /home/student/bin/ensure-line.sh FILE LINE.

Task

  • If FILE already contains LINE as a whole line (exactly, including any special characters), do nothing.
  • Otherwise append LINE at the end. If the file doesn't end with a newline, add one first so the existing last line stays intact.
  • If FILE doesn't exist, create it containing just LINE.
  • Print added or present, and exit 0.

Lines may contain characters that mean something to regular expressions or the shell, like ., *, [, $, \ or a leading -.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Part of Shell Scripting for Automation. Next step: Long Options by Hand

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.