Line error in shell script

I have the following code in a shell script. It seems that this only works if the function is not defined . The problematic line is the one that contains "<lt;". Error message

"./run: line 210: syntax error: unexpected end of file"

How can I correctly write this function inside a function?

init_database()
{
    cd ../cfg
    db.sh << ENDC
    $DB_ADMIN

    0
    y
    n
    ENDC

    check_status

    sqlplus $DB_SCHEMA@$DB_NAME < initial_data.sql

    cd -
}

      

0


a source to share


3 answers


There are several ways to fix this problem.

1 / Undo the end-of-document marker here, for example:

cat <<EOF
    hello
    $PWD
EOF

      

but this will make your code look ugly.

2 / "Indent" marker of the beginning of the document here:

cat <<' EOF'
    hello
    $PWD
    EOF

      

where this bit before the first EOF is exactly the same as before the second (tab, four spaces, two tabs, whatever). This allows for good indentation, although it does not expand variables within the document here ( $PWD

does not change).



3 / Allow to remove tabs from the beginning of input lines and ending marker.

cat <<-EOF
    hello
    $PWD
    EOF

      

but there is no way to get tabs to the beginning of lines.

4 / For your purposes, you can also use:

(   echo "$DB_ADMIN";
    echo "" ;
    echo "0" ;
    echo "y" ;
    echo "n"
) | db.sh
check_status
sqlplus $DB_SCHEMA@$DB_NAME < initial_data.sql
cd -

      

I think number 4 is the best option for you. This allows you to line up the input, tabs, and spaces well anywhere in lines and variable expansion.

+5


a source


The end of your "Here" document should be unable, I'm afraid.



+1


a source


The label ENDC

must be one per line with no leading / trailing whitspaces.

+1


a source







All Articles