0

I wrote a "Todo" bash scripting program which have menu of options for adding a new task, remove finished task from pending file and add it to done file ,etc. it supposed to make pending_file.txt and done_file.txt after running options, but it only creates .txt which is regarding to pending_file.txt and done_file.txt isnt created

it runs properly and I expect it to make pending_file.txt and done_file.txt, but it didn't create them

move_to_done(){
    read -p "Enter finished task description: " finished_task
    if [ ! -f "$pending_file.txt" ]; then
        echo "No pending tasks"
    return
    else
        sed -i "s/$finished_task//g" $pending_file.txt
        if [ ! -f "$done_file.txt" ]; then
            touch $done_file.txt
            echo "$finished_task" >> $done_file.txt
            echo "Task marked as done"
        fi
    fi
}

show_finished_tasks(){
    if [ ! -f "$done_file.txt" ]; then
        echo "No finished tasks"
    else
        cat $done_file.txt
    fi
}
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • 5
    The `$` at the beginning of `$pending_file` and `$done_file` tells the shell to "expand" them as variable references, but neither variable is defined so the both expand to the empty string. `"$done_file.txt"` -> `".txt"` -> `".txt"`. You probably just want to remove those dollar signs. – Gordon Davisson Mar 31 '23 at 17:52
  • 1
    Agree with above, OR you assign values to those variables before the functions get called, ie. `pending_file="/path/to/ToDo/pend/$(date +%Y%m%d)_pending.txt; echo "pending_file=$pending_file"` etc. Good luck. – shellter Mar 31 '23 at 18:33
  • 2
    Your script has several problems that http://shellcheck.net would tell you about and using `sed -i "s/$finished_task//g"` could remove the wrong lines if `$finished_task` contained regexp metachars - see [is-it-possible-to-escape-regex-metacharacters-reliably-with-sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) for how to handle that with `sed` but you'd be better off just using `awk` (or maybe even `grep -Fv` for what you're doing) since `awk` understands literal strings. Similarly, you need to use `printf` instead of `echo`. – Ed Morton Apr 01 '23 at 12:23

0 Answers0