diff --git a/functions.sh b/functions.sh index d63ea1d..ce93f42 100755 --- a/functions.sh +++ b/functions.sh @@ -53,7 +53,8 @@ delete_line() { } edit_line() { - sed -i "${task}s/.*/- $new_task/" "$file.md" + escaped_task=$(sed 's/[\/&]/\\&/g' <<< "$new_task") + sed -i "${task}s/.*/- $escaped_task/" "$file.md" echo "Line ${task} edited." } diff --git a/tests.sh b/tests.sh index 5fc44b2..ecf98b1 100755 --- a/tests.sh +++ b/tests.sh @@ -128,8 +128,17 @@ if [ -z "$(sed -n "/# $mon/ p" "$file.md")" ]; then fi cd - > /dev/null -# Test edit command -output=$("$woche_script_path" edit 2 "New task") +# Test add task with punctituation to a day command +output=$("$woche_script_path" mon "Test task with punctuation: ;,!@#$%^&*()_+") +if [[ "$output" == *"Task 'Test task with punctuation: ;,!@#$%^&*()_+' added to"* ]]; then + echo "Test 'add task to a day' command: PASSED" +else + echo "Test 'add task to a day' command: FAILED" +fi +check_test_result + +# Test edit command with punctituation +output=$("$woche_script_path" edit 2 "New task ,.!@") if [[ "$output" == *"Line 2 edited."* ]]; then echo "Test 'edit' command: PASSED" else @@ -138,7 +147,7 @@ fi ## Check if the task is edited on last test cd "$path_to_files" > /dev/null -if [ -z "$(sed -n "2 p" "$file.md" | grep "New task")" ]; then +if [ -z "$(sed -n "2 p" "$file.md" | grep "New task ,.!@")" ]; then echo "Error: Task has not been edited." exit 1 fi diff --git a/woche.sh b/woche.sh index d01bb67..b264d5f 100755 --- a/woche.sh +++ b/woche.sh @@ -61,7 +61,8 @@ case $1 in *) file_exists day=$(eval echo \$$1) - sed -i "/# $day/ a\\- $task" "$file.md" + escaped_task=$(sed 's/[\/&]/\\&/g' <<< "$task") + sed -i "/# $day/ a\\- $escaped_task" "$file.md" echo "Task '$task' added to $day." exit 0 ;;