shelllings

a practical way to learn shell
Log | Files | Refs | README | LICENSE

31_trap.sh (420B)


      1 #!/bin/sh
      2 
      3 set -eu
      4 RED="\033[31m"
      5 GREEN="\033[32m"
      6 RESET="\033[0m"
      7 
      8 cleanup() {
      9 	[ -f "file.txt" ] && rm -f file.txt
     10 }
     11 trap cleanup EXIT
     12 
     13 failed() {
     14 	printf "${RED}Failed${RESET}\n"
     15 	exit 1
     16 }
     17 
     18 sh exercises/31_trap.sh | grep -Eq "Removing..." || failed
     19 
     20 grep 'trap' exercises/31_trap.sh | grep "rm file\.txt" | grep 'Removing...' | grep -q 'EXIT' || failed
     21 
     22 [ -f "file.txt" ] && failed
     23 
     24 printf "${GREEN}Passed${RESET}\n"