0

The below code is working:

[ec2-user@ip restore]$ echo $snap_name
manual-test-2024-01-11-11-26-19
[ec2-user@ip restore]$  aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --query 'DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == `'"$snap_name"'`']'' | grep creating
[ec2-user@ip-10-0-39-226 restore]$

But when trying to get the output as a variable, it is getting an error:

[ec2-user@ip restore]$ snap_count=`aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --query 'DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == `'"$snap_name"'`']'' | grep creating`
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 2: syntax error: unexpected end of file
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 2: syntax error: unexpected end of file
[ec2-user@ip restore]$

Please suggest here.

AdminBee
  • 22,803
  • 1
    backticks cannot be nested, but $( .. ) can be – Lorinczy Zsigmond Jan 11 '24 at 13:29
  • tried but still getting the error :

    snap_count=aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --query 'DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == '$snap_name']' | grep available

    Bad value for --query DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == manual-test-creditpoc5-2024-01-11-11:24:50]: Bad jmespath expression: Unknown token '-':

    – jayvardhan Jan 11 '24 at 13:41
  • backticks don't work in the comment section either. – Lorinczy Zsigmond Jan 11 '24 at 14:17

1 Answers1

3

The problem is that you are using the old-style "backticks"-notation for the command substitution. Their use is discouraged, among other reasons because they cannot be nested easily.

The error messages you receive can be understood if you consider that your command-substitution only spans your command up to the opening backtick of your query condition, i.e. only the part

aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --query 'DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == 

The shell then tries to concatenate the variable snap_count from

  • the result of that command invocation - which the shell can't parse because you have an unbalanced opening single-quote (this is the reason for the first of your two error messages)
  • the interpretation of '"$snap_name"' which results in the literal string "$snap_name" because from the point-of-view of the shell, it is placed in single-quotes, and then
  • another command-substution started by what you inteded as closing backtick of your query condition. This would be the output of the command ']'' | grep creating which again has unbalanced single-quotes and is the reason for the second of your two error messages.

The result, as noted by @ilkkachu, is that $snap_count is assigned the only valid part of the assigment, which is the literal string "$snap_name".

Use the recommended $( ... )-notation for the actual command-substitution instead (i.e. instead of the outer backticks), this will not lead to conflict with the ` ... ` that you presumably require for the query syntax in the ?DBClusterSnapshotIdentifier condition.

AdminBee
  • 22,803
  • Thanks for the support, below is working now.

    [ec2-user@ restore]$ snap_count=aws rds describe-db-cluster-snapshots --db-cluster-identifier creditpoc3 --query 'DBClusterSnapshots[].{DBClusterSnapshotIdentifier:DBClusterSnapshotIdentifier,Status:Status} | [?DBClusterSnapshotIdentifier == \'"$snap_name"'`']'' | grep available` [ec2-user@ip-10-0-39-226 restore]$ echo $snap_count "Status": "available" [ec2-user@ restore]$

    – jayvardhan Jan 11 '24 at 15:51
  • @jayvardhan That is great to hear, but somewhat surprising. It may be due to the limited formatting options of the comment section, but syntactically the commend you describe as working doesn't seem any different from the one you described as not working in your post. Perhaps you could explain the differences between both in more detail? – AdminBee Jan 12 '24 at 13:14