Home > Mobile >  Shell/Bash - positional parameter for script and function colliding
Shell/Bash - positional parameter for script and function colliding

Time:11-07

My script is to accept one parameter dms. Once obtained it creates multiple variables that are used as argument for retry_cli. Issue I'm having is with positional parameter usage. How to workaround the mechanism for script to create variables and use them as parameters for each function's run?

Currently it seems that function is using positional parameters injected when script is triggered, not the ones created inside of it. Documentation indicates that two $1 positional arguments (for script and function) shouldn't be an issue.

dms_name=$1
aws_region="us-east-1"
dms_arn=`aws dms describe-replication-tasks --filter Name=replication-task-id,Values="$dms_name" --query=ReplicationTasks[0].ReplicationTaskArn --output text --region "$aws_region"`

ri_arn=`aws dms describe-replication-tasks --filter Name=replication-task-id,Values=$dms_name --query=ReplicationTasks[0].ReplicationInstanceArn --region "$aws_region"`
target_arn=`aws dms describe-replication-tasks --filter Name=replication-task-id,Values=$dms_name --query=ReplicationTasks[0].TargetEndpointArn --region "$aws_region"`

ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region "$aws_region"`
target_status=`aws dms describe-connections --filter Name=endpoint-arn,Values=$target_arn --query=Connections[0].Status --region "$aws_region"`

retry_cli () {
        local max_retry=100
        local counter=0
        local sleep_seconds=15 
        local aws_region="us-east-1"  
        until [[ $1 =~ "success" ]]
        do 
            status=`aws dms describe-connections --filter Name=$2,Values="$3" --query=Connections[0].Status --region "$aws_region"`
            [[ counter -eq $max_retry ]] && echo "connection status of $3 failed!" && exit 1
            ((counter  ))
            sleep $sleep_seconds
        done
        echo "connection for $3 OK!"
}

retry_cli "$ri_status" replication-instance-arn "$ri_arn"
retry_cli "$target_status" endpoint-arn "$target_arn"

echo startting dms
aws dms start-replication-task --replication-task-arn "$dms_arn" --start-replication-task-type resume-processing --region "$aws_region"
echo done

Expected behaviour:

  1. script is ran: sh script.sh dms_name
  2. every dms_ variable is populated
  3. retry_cli is called with 3 arguments. When $*_status is not like success string, then the function must obtain latest status with aws cli command and repeat it as long as it returns success
  4. move to the second function call

CodePudding user response:

I'm assuming what the function's until loop is doing is repeatedly running a command until it receives a status message that includes the string success.

For the first pass the until loop is testing the function's first input parameter ($1) for the string success.

Subsequent passes through the until loop are still testing the function's first input parameter ($1) for the string success.

I'm guessing for subsequent passes what you want to do is test the latest status for the string success; if this is the case then perhaps a small code change:

retry_cli () {
    ...
    local status="$1"                           # new line
    until [[ "${status}" =~ "success" ]]        # modified line
    do
        ....
    done
    ...
}

If this doesn't address the issue then OP may want to provide some more details on the issue (eg, loop never exits, loop is never entered, ???).

  • Related