Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

A cleanup script logs `cp: cannot stat '/var/log/app/*.log': No such file or directory` on quiet days.

BeginnerTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → No file matched the glob, so Bash passed the pattern literally to cp. A for f in /var/log/app/*.log loop has the same issue and runs once with the literal pattern.

Diagnose → ls /var/log/app/ on that day; bash -x shows the literal *.

Fix →

shopt -s nullglob
files=(/var/log/app/*.log)
(( ${#files[@]} )) || { echo "nothing to copy"; exit 0; }
cp -- "${files[@]}" /backup/

Prevent → Use nullglob (or [[ -e $f ]] || continue inside loops) whenever a glob may match nothing.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs