Nested else statement on bash script

For some reason, the first else statement is throwing a syntax error and I don't know why

#! /usr/bin/bash
    clear
    read -p "Enter the file you want to append the date to" FILENAME

if [ -f $FILENAME ]
then
    if [ -w $FILENAME ]
        echo "`date +"%d-%m-%Y"`" >> $FILENAME
    else    # FOR SOME REASON THIS ELSE STATEMENT IS THROWING AN ERROR
        echo "$FILENAME cannot be written to"
    fi
    
else
    touch $FILENAME
    echo "`date +"%d-%m-%Y"`" >> $FILENAME
fi 

1 answer

  • answered 2021-07-31 07:40 Jay

    missing "then" in your second if statement.
    add a "then" to line 8 as you do in line 6.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum