1

I am trying to gather all of my TODOs and make one file out of them.

#!/bin/bash

# Script for getting all TODOS

PATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"

echo "#TODO" > $OUTPUT

grep -hr --exclude-dir=plugins "\bTODO\b:.*" $PATH | awk '{print $0,"\n"}' >> $OUTPUT

If I run each line in my prompt it works perfectly. When I add them to a script and make it executable and run it I get

./obtodo.sh: line 10: grep: command not found
./obtodo.sh: line 10: awk: command not found
./obtodo.sh: line 12: chown: command not found

I tried running as sudo and I made the script executable with chmod a+x

sneakyfishies
  • 165
  • 10
  • 2
    There are a bunch of all-caps variable names with special meanings and functions, and `PATH` is one of the most critical of them. It's safest to use lower- or mixed-case variable names to avoid conflicts like this. Also, `~` won't expand properly when it's in quotes; either remove the quotes or just quote the part after `~/`. Finally, it's a good idea to double-quote variable references (i.e. `"$path"` instead of just `$path`). – Gordon Davisson Mar 10 '22 at 08:33
  • @GordonDavisson Thank you for the tips! :) I will put this all in place now! – sneakyfishies Mar 10 '22 at 12:19
  • @GordonDavisson What's the reason behind adding quotes to var references? – sneakyfishies Mar 10 '22 at 12:38
  • 1
    [Shellcheck](https://www.shellcheck.net/) identifies several significant problems in the code. It also provides links to details of how to fix them. [Shellcheck](https://www.shellcheck.net/) can find *many* problems in Bash, and other shell, code. – pjh Mar 10 '22 at 12:43
  • 1
    See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375) for an explanation of why ALL_CAPS variable names cause problems. – pjh Mar 10 '22 at 12:45
  • 1
    See [Quotes - Greg's Wiki](https://mywiki.wooledge.org/Quotes) for essential information about using quotes in Bash code. In particular, see the [When Should You Quote?](https://mywiki.wooledge.org/Quotes#When_Should_You_Quote.3F) section. – pjh Mar 10 '22 at 12:49

1 Answers1

3

$PATH is a special variable to the shell. It defines the list of directories to be searched when executing subcommands.

Bash won't know where grep, awk or chown are. Please use different variable name instead of $PATH.

Try

#!/bin/bash

# Script for getting all TODOS

xPATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"

echo "#TODO" > $OUTPUT

grep -hr --exclude-dir=plugins "\bTODO\b:.*" $xPATH | awk '{print $0,"\n"}' >> $OUTPUT
nemesis
  • 150
  • 9