Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell script: "'<<' unmatched"-syntax error using here document

Tags:

shell

ksh

Hi I am attempting to write a program that will alert the user if a person of interest has come online at a given time. My program thus far is

#!/usr/bin/ksh
message=""
when=""
validFiles=""
validUsers=""

if [ $# -gt 0 ] ; then
    while getopts w:m: opt
    do
        case $opt in
        w) when=$OPTARG;;
        m) message=$OPTARG;;
        \?) echo $USAGE exit 2;;
        esac
    done
    shift $(($OPTIND - 1))
    if [[ $# -gt 0 ]] ; then
        for i; do
            if [[ -f "$i" && -r "$i" ]]; then
                if ! echo $validFiles | grep $i >/dev/null; then
                    validFiles="$validFiles $i"
                fi
            elif id $i 2> /dev/null 1>&2; then
                if ! echo $validUsers | grep $i > /dev/null; then
                    validUsers="$validUsers $i"
                fi
            fi
        done
        if [[ $when != "" && $validFiles != "" || $validUsers != "" ]] ;then
            for i in $validUsers; do
                if ! grep $i $validFiles >/dev/null; then
                    at $when <<"END"
                        if finger $i | grep $i; then
                            echo "$i is online" | elm $message
                        fi
                    END
                fi
            done
        fi
    else
        echo "No files or usernames"
    fi
else
    echo "No arguments provided"
fi

My problem is that when I attempt to run this I get the error message

 syntax error at line 33 : `<<' unmatched

I am not sure as to why this is appearing. I have checked many other examples and my at command,here document, appears to be the same as theirs. Could anybody help me out? Thanks.

like image 705
cogle Avatar asked Dec 16 '22 00:12

cogle


1 Answers

The here string delimiter must not be indented, your END should be at the beginning of the line:

$ cat <<EOT
> foo
>         bar
> EOT
foo
        bar

If you want the trailing delimiter to be indented you can use the following syntax, but this will also strip all leading tabs from the here document itself (this only works with tabs!):

$ cat <<-EOT
>         foo
>         bar
>         quux
>         EOT
foo
bar
quux

Note that this behaviour is specified by POSIX so should work in all compliant shells:

If the redirection symbol is "<<-", all leading tabs shall be stripped from input lines and the line containing the trailing delimiter.

like image 140
Adrian Frühwirth Avatar answered Jan 07 '23 18:01

Adrian Frühwirth