0

I have a shell script that contains this command:

  SNAPSHOT_ID=$(bash <<-EOF 
              aws rds create-db-snapshot 
              --db-instance-identifier $RDS_INSTANCE_ID 
              --db-snapshot-identifier $RDS_INSTANCE_ID-manual-$NOW_DATE 
              --query 'DBSnapshot.[DBSnapshotIdentifier]' --output text 
EOF
   )

But I'm receiving this error:

line 191: unexpected EOF while looking for matching `)'

How can I fix this?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user1297406
  • 1,241
  • 1
  • 18
  • 36
  • Could you check your file for [carriage returns](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings)? – that other guy Sep 26 '19 at 17:27

3 Answers3

1

You don't need a here-doc to send your command to a new shell (and you're already in a subshell):

snapshot_id=$(aws rds create-db-snapshot \
    --db-instance-identifier "$RDS_INSTANCE_ID" \
    --db-snapshot-identifier "$RDS_INSTANCE_ID-manual-$NOW_DATE" \
    --query 'DBSnapshot.[DBSnapshotIdentifier]' --output text)

You do have to escape your newlines, though.

I've also added quotes around parameter expansions and lowercased the one variable being created, as uppercase variable names are reserved for shell and utilities (see POSIX spec, fourth paragraph).

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

I am not sure if this is your case but if the second EOF does not start from the beginning the line (column 1) you will get such an error. For example:

[root@tsekmanrhel771 ~]# cat eoftest.sh
#!/bin/sh

 SNAPSHOT_ID=$(bash <<-EOF
              aws rds create-db-snapshot 
              --db-instance-identifier $RDS_INSTANCE_ID 
              --db-snapshot-identifier $RDS_INSTANCE_ID-manual-$NOW_DATE 
              --query 'DBSnapshot.[DBSnapshotIdentifier]' --output text 
 EOF
   )
[root@tsekmanrhel771 ~]# ./eoftest.sh
./eoftest.sh: line 3: unexpected EOF while looking for matching `)'
./eoftest.sh: line 10: syntax error: unexpected end of file
tsekman
  • 398
  • 1
  • 5
0
SNAPSHOT_ID=$(bash <<EOF
aws rds create-db-snapshot --db-instance-identifier $RDS_INSTANCE_ID --db-snapshot-identifier $RDS_INSTANCE_ID-manual-$NOW_DATE --query 'DBSnapshot.[DBSnapshotIdentifier]' --output text
EOF
)

problems were with the "-" before EOF and the last ")" seemed to be badly idented, not sure if from copy/paste. Give it a try, if the commands need to be inserted in new lines finish them with "\n".

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
tavanez
  • 13
  • 7