Day 5 of 5
⏱ ~60 minutes
Linux in 5 Days — Day 5

Shell Scripting

Write bash scripts with variables, conditionals, loops, and functions. Automate backups and deployments.

Bash Script Basics

script.sh
#!/bin/bash
# The shebang tells the OS to use bash
set -euo pipefail  # exit on error, undefined var, pipe failure

# Variables (no spaces around =)
NAME="World"
echo "Hello, $NAME!"
echo "Script: $0, Arg1: $1, All args: $@"

# Command substitution
TODAY=$(date +%Y-%m-%d)
FILE_COUNT=$(ls | wc -l)
echo "Today: $TODAY, Files: $FILE_COUNT"
Conditionals and Loops
# if/elif/else
if [ -f "file.txt" ]; then
    echo "File exists"
elif [ -d "mydir" ]; then
    echo "Directory exists"
else
    echo "Neither exists"
fi

# for loop
for file in *.txt; do
    echo "Processing: $file"
done

# while loop
COUNT=0
while [ $COUNT -lt 5 ]; do
    echo "Count: $COUNT"
    COUNT=$((COUNT + 1))
done
Functions and Error Handling
#!/bin/bash
set -euo pipefail

# Function
backup() {
    local SRC=$1
    local DST=$2
    local DATE=$(date +%Y%m%d_%H%M%S)
    cp -r "$SRC" "${DST}_${DATE}"
    echo "Backed up $SRC to ${DST}_${DATE}"
}

# Error handling
if ! command -v docker &>/dev/null; then
    echo "Error: Docker not installed" >&2
    exit 1
fi

# Run backup
backup /var/www/html /backup/html
Cron: Schedule Scripts
# Edit crontab
crontab -e

# Format: minute hour day month weekday command
# Run backup every day at 2am
0 2 * * * /home/bo/scripts/backup.sh >> /var/log/backup.log 2>&1

# Every 15 minutes
*/15 * * * * /home/bo/scripts/check-disk.sh

# Every Monday at 9am
0 9 * * 1 /home/bo/scripts/weekly-report.sh
📝 Day 5 Exercise
Write a Deployment Script
  1. W
  2. r
  3. i
  4. t
  5. e
  6. a
  7. b
  8. a
  9. s
  10. h
  11. s
  12. c
  13. r
  14. i
  15. p
  16. t
  17. t
  18. h
  19. a
  20. t
  21. :
  22. c
  23. h
  24. e
  25. c
  26. k
  27. s
  28. i
  29. f
  30. D
  31. o
  32. c
  33. k
  34. e
  35. r
  36. i
  37. s
  38. i
  39. n
  40. s
  41. t
  42. a
  43. l
  44. l
  45. e
  46. d
  47. ,
  48. p
  49. u
  50. l
  51. l
  52. s
  53. a
  54. D
  55. o
  56. c
  57. k
  58. e
  59. r
  60. i
  61. m
  62. a
  63. g
  64. e
  65. ,
  66. s
  67. t
  68. o
  69. p
  70. s
  71. t
  72. h
  73. e
  74. o
  75. l
  76. d
  77. c
  78. o
  79. n
  80. t
  81. a
  82. i
  83. n
  84. e
  85. r
  86. ,
  87. s
  88. t
  89. a
  90. r
  91. t
  92. s
  93. a
  94. n
  95. e
  96. w
  97. o
  98. n
  99. e
  100. ,
  101. a
  102. n
  103. d
  104. l
  105. o
  106. g
  107. s
  108. e
  109. a
  110. c
  111. h
  112. s
  113. t
  114. e
  115. p
  116. w
  117. i
  118. t
  119. h
  120. a
  121. t
  122. i
  123. m
  124. e
  125. s
  126. t
  127. a
  128. m
  129. p
  130. .
  131. M
  132. a
  133. k
  134. e
  135. i
  136. t
  137. e
  138. x
  139. e
  140. c
  141. u
  142. t
  143. a
  144. b
  145. l
  146. e
  147. .
  148. S
  149. c
  150. h
  151. e
  152. d
  153. u
  154. l
  155. e
  156. i
  157. t
  158. w
  159. i
  160. t
  161. h
  162. c
  163. r
  164. o
  165. n
  166. .

Day 5 Summary

  • #!/bin/bash shebang + set -euo pipefail at the top of every script.
  • Variables: no spaces around =. Reference with $NAME or ${NAME}.
  • [ -f file ] tests file existence. [ -d dir ] for directories. [ $a -eq $b ] for numbers.
  • Cron syntax: minute, hour, day, month, weekday. Use crontab.guru to verify your expressions.
Finished this lesson?