Link to home
Start Free TrialLog in
Avatar of Paulo Leitao
Paulo LeitaoFlag for Spain

asked on

MacOSX: awk using input variable from command line

I have this script which return me a list for selection with the correct information

#!/bin/bash
choices=('Cancel Restore')
choices+=('Delete Older Instances')
choices+=($(aws rds describe-db-snapshots --db-instance-identifier rdsinstance | awk -F'"' '/"DBSnapshotIdentifier": "testvar",/ { print $4 }'))

select choice in "${choices[@]}"; do
...

Open in new window


however, I want to dynamically replace the tesvar by a variable pushed from the command line

I'm trying the following:

> /location/mybash.sh testvar
#!/bin/bash

variable=$1

choices=('Cancel Restore')
choices+=('Delete Older Instances')

variable=$1
choices+=($(aws rds describe-db-snapshots --db-instance-identifier rdsinstance | awk -v var="$variable" -F'"' '/"DBSnapshotIdentifier": "var",/ { print $4 }'))

select choice in "${choices[@]}"; do
...

Open in new window


but is not working, I have tried several combinations but without success.
the idea is to filter the below result into a selection list only when the line matches "DBSnapshotIdentifier": "testvar",

{
    "DBSnapshots": [
        {
            "DBSnapshotIdentifier": "testvar",
            "DBInstanceIdentifier": "rdsinstance",
            "SnapshotCreateTime": "2018-05-23T14:32:50.955Z",
            "Engine": "sqlserver-ex",
            "Status": "available",
            "EngineVersion": "11.00.2100.60.v1",
            "SnapshotType": "manual",
            "DBSnapshotArn": "arn:aws:rds:xxxxxxxxxxxxxxxxxx:snapshot: testvar",
        }
    ]
}

Open in new window


If I use the testvar in the script, I will obtain a choice testvar (the name of the instance) if I try to pass it from the command line, it will not work.

how can I filter the value using an input?
ASKER CERTIFIED SOLUTION
Avatar of Paulo Leitao
Paulo Leitao
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paulo Leitao

ASKER

No better solution was presented