Pass data at function , and stored in array

Hi , i have this function

_ByBit_Batch_Place_Order() {
    local apiUrl=""
    RECVWINDOW=5000
    TIMESTAMP=$(($(date +%s%N) / 1000000))

    # Imposta il tuo timestamp all'interno della finestra di ricezione
    local TIMESTAMP_INSIDE_WINDOW=$((TIMESTAMP + RECVWINDOW))

    # Confronta con il timestamp corrente
    if [ "$TIMESTAMP_INSIDE_WINDOW" -gt "$TIMESTAMP" ]; then
    local URLPART2="" whichapi="" category="" request=() symbol="" side="" ordertype="" orderid="" qty="" marketunit=""
    local price="" triggerdirection="" orderfilter="" triggerprice="" triggerby="" orderiv="" timeinforce=""
    local positionindex="" orderlinkid="" takeprofit=""  stoploss=""  tptriggerby=""  sltriggerby="" reduceonly=""
    local closeontrigger="" smptype="" mmp="" tpslmode="" tplimitprice="" sllimitprice="" tpordertype="" slordertype=""
   
    # Leggi i parametri della funzione
    while [ "$#" -gt 0 ]; do
        case "$1" in
            "--which-api")
                shift
                whichapi="$1"
                ;;
            "--category")
                shift
                category="$1"
                ;;
            "--request")
                shift
                # Verifica il valore di category e imposta la lunghezza dell'array request
                if [ "$category" = "option" ]; then
                    request=($(seq 1 20))
                elif [ "$category" = "spot" ] || [ "$category" = "linear" ]; then
                    request=($(seq 1 10))
                else
                    echo "Errore: Valore non valido per la categoria: $category , deve essere o spot o linear o option"
                    return 1
                fi
				  for req in "${!request[@]}"; do
						# Aggiungi ciascun elemento dell'array alla tua logica
						if [ "$req" -eq 0 ]; then
							symbol="$1"
						elif [ "$req" -eq 1 ]; then
							side="$1"
						# Aggiungi altri controlli per gli altri elementi dell'array
						fi
						shift
					done
                            ;;
            "--symbol")
                shift
                symbol="$1"
                ;;
            "--side")
                shift
                side="$1"
                ;;
            "--order-type")
                shift
                ordertype="$1"
                ;;
            "--orderid")
                shift
                orderid="$1"
                ;;
            "--orderlinkid")
                shift
                orderlinkid="$1"
                ;;
            "--orderiv")
                shift
                orderiv="$1"
                ;;
            "--triggerprice")
                shift
                triggerprice="$1"
                ;;
            "--qty")
                shift
                qty="$1"
                ;;
            "--price")
                shift
                price="$1"
                ;;
            "--tpslmode")
                shift
                tpslmode="$1"
                ;;
            "--takeprofit")
                shift
                takeprofit="$1"
                ;;
            "--stoploss")
                shift
                stoploss="$1"
                ;;
            "--tptriggerby")
                shift
                tptriggerby="$1"
                ;;
            "--sltriggerby")
                shift
                sltriggerby="$1"
                ;;
            "--triggerby")
                shift
                triggerby="$1"
                ;;
            "--tplimitprice")
                shift
                tplimitprice="$1"
                ;;
            "--sllimitprice")
                shift
                sllimitprice="$1"
                ;;
            *)
                echo "Errore: Parametro non riconosciuto: $1"
                return 1
                ;;
        esac
        shift
    done


        _Init_BYBIT_ApiSecretIDData "$gApiTypo"

        if [ "$whichapi" == "sand_bybit" ]; then
            ApiUrl="https://api-testnet.bybit.com/"
            _Init_BYBIT_ApiSecretIDData "sand_bybit"
        elif [ "$whichapi" == "bybit" ]; then
            ApiUrl="https://api.bybit.com/"
            _Init_BYBIT_ApiSecretIDData "bybit"
        fi

        apiUrl="${ApiUrl}v5/order/create-batch"

        # Verifica se i parametri obbligatori sono presenti
        #if [ -z "$category" ] || [ ${#request[@]} -eq 0 ]  || [ -z "$symbol" ] || [ -z "$side" ] || [ -z "$ordertype" ]  || [ -z "$qty" ] ; then
        if [ -z "$category" ] || [ -z "$symbol" ] || [ -z "$side" ] || [ -z "$ordertype" ]  || [ -z "$qty" ] ; then
            echo "Errore: I parametri obbligatori sono --category,  --request ,--symbol, --side, --ordertype, --qty"
            return 1
        fi

        # Costruisci la variabile URLPART2 in base ai parametri forniti
        URLPART2="{\"category\":\"$category\",\"request\":[{\"symbol\":\"$symbol\",\"side\":\"$side\",\"orderType\":\"$ordertype\",\"qty\":\"$qty\"}"

        # Aggiungi parametri solo se sono presenti e non vuoti

		[ -n "$orderid" ] && URLPART2+=",\"orderId\":\"$orderid\""
		[ -n "$orderlinkid" ] && URLPART2+=",\"orderLinkId\":\"$orderlinkid\""
		[ -n "$orderiv" ] && URLPART2+=",\"orderIv\":\"$orderiv\""
		[ -n "$triggerprice" ] && URLPART2+=",\"triggerPrice\":\"$triggerprice\""
		[ -n "$qty" ] && URLPART2+=",\"qty\":\"$qty\""
		[ -n "$price" ] && URLPART2+=",\"price\":\"$price\""
		[ -n "$tpslmode" ] && URLPART2+=",\"tpslMode\":\"$tpslmode\""
		[ -n "$takeprofit" ] && URLPART2+=",\"takeProfit\":\"$takeprofit\""
		[ -n "$stoploss" ] && URLPART2+=",\"stopLoss\":\"$stoploss\""
		[ -n "$tptriggerby" ] && URLPART2+=",\"tpTriggerBy\":\"$tptriggerby\""
		[ -n "$sltriggerby" ] && URLPART2+=",\"slTriggerBy\":\"$sltriggerby\""
		[ -n "$triggerby" ] && URLPART2+=",\"triggerBy\":\"$triggerby\""
		[ -n "$tplimitprice" ] && URLPART2+=",\"tpLimitPrice\":\"$tplimitprice\""
		[ -n "$sllimitprice" ] && URLPART2+=",\"slLimitPrice\":\"$sllimitprice\""

        URLPART2="$URLPART2}]}"
        
        ORIGINAL_STRING="${TIMESTAMP}${BYBITApiKey}${RECVWINDOW}${URLPART2}"
        # Calcola la firma HMAC
        SIGNATURE=$(echo -n "$ORIGINAL_STRING" | openssl dgst -sha256 -hmac "$BYBITSecretKey" | cut -c 18-)

        #response=$(nice -n -20 ionice -c 1 -n 0 curl --location --request POST ${apiUrl} 
        response=$(curl --location --request POST ${apiUrl} \
            --header 'X-BAPI-SIGN-TYPE: 2' \
            --header 'X-BAPI-SIGN: '${SIGNATURE} \
            --header 'X-BAPI-API-KEY: '${BYBITApiKey} \
            --header 'X-BAPI-TIMESTAMP: '${TIMESTAMP} \
            --header 'X-BAPI-RECV-WINDOW: '$RECVWINDOW \
            --header 'Content-Type: application/json' \
            --data-raw "${URLPART2}")

        echo "$response"
    else
        echo "Errore: Il timestamp corretto รจ al di fuori della finestra di ricezione."
        return 1
    fi
}

_Initialize_Api_ByBit --api-typo "sand_bybit" 
req1='{"symbol": "ETH-30DEC22-500-C", "orderType": "Limit", "side": "Buy", "qty": "1", "price": null, "orderIv": "6", "timeInForce": "GTC", "orderLinkId": "option-test-001", "mmp": false, "reduceOnly": false}'
req2='{"symbol": "ETH-30DEC22-700-C", "orderType": "Limit", "side": "Buy", "qty": "2", "price": "700", "iv": null, "timeInForce": "GTC", "orderLinkId": "option-test-001", "mmp": false, "reduceOnly": false}'

#--request "${req1}" "${req2}" 
_ByBit_Batch_Place_Order --category "spot"  --symbol "ETHUSDT" --request "${req1}" "${req2}" --side "buy" --order-type "Market" --qty "2"

if i call the function with out param --request work , but if i use also a param --request "${req1}" "${req2}" tell me error I parametri obbligatori sono --category, --request ,--symbol, --side, --ordertype, --qty", why if not in if ?, i suppose the problem is in this part of code

   "--request")
                shift
                # Verifica il valore di category e imposta la lunghezza dell'array request
                if [ "$category" = "option" ]; then
                    request=($(seq 1 20))
                elif [ "$category" = "spot" ] || [ "$category" = "linear" ]; then
                    request=($(seq 1 10))
                else
                    echo "Errore: Valore non valido per la categoria: $category , deve essere o spot o linear o option"
                    return 1
                fi
				  for req in "${!request[@]}"; do
						# Aggiungi ciascun elemento dell'array alla tua logica
						if [ "$req" -eq 0 ]; then
							symbol="$1"
						elif [ "$req" -eq 1 ]; then
							side="$1"
						# Aggiungi altri controlli per gli altri elementi dell'array
						fi
						shift
					done

but for me is corect anyone have some idea ?? thanks

@faustf ,

given the length of this and the fact some bits are missing (for testing),
i made a modification where you test for zero/null values of various options - simple replace your existing test by this then run, it will show you the values of the various mandatory options,
you can then use that to further debug your logic


        # Verifica se i parametri obbligatori sono presenti
        #if [ -z "$category" ] || [ ${#request[@]} -eq 0 ]  || [ -z "$symbol" ] || [ -z "$side" ] || [ -z "$ordertype" ]  || [ -z "$qty" ] ; then

        if [ -z "$category" ] || [ -z "$symbol" ] || [ -z "$side" ] || [ -z "$ordertype" ]  || [ -z "$qty" ] ; then
                cat << EOerrors
Errore: I parametri obbligatori sono 

--category [$category]

--symbol [$symbol]

--side [$side]

--ordertype [$ordertype]

--qty [$qty]

--request [$request]

EOerrors

            return 1
        fi

when i run with this modifcation i get the following output

./faustf.bash
.bash
./stripped.bash: line 205: _Initialize_Api_ByBit: command not found
./stripped.bash: line 127: _Init_BYBIT_ApiSecretIDData: command not found
Errore: I parametri obbligatori sono 

--category [spot]

--symbol [{"symbol": "ETH-30DEC22-500-C", "orderType": "Limit", "side": "Buy", "qty": "1", "price": null, "orderIv": "6", "timeInForce": "GTC", "orderLinkId": "option-test-001", "mmp": false, "reduceOnly": false}]

--side [{"symbol": "ETH-30DEC22-700-C", "orderType": "Limit", "side": "Buy", "qty": "2", "price": "700", "iv": null, "timeInForce": "GTC", "orderLinkId": "option-test-001", "mmp": false, "reduceOnly": false}]

--ordertype []

--qty []

--request [1]

hope this helps

Indeed the for req loop looks strange.
The request array is indexed either 0..9 or 0..19, dependent on the (previously given) category.
Then, for each index it consumes a further parameter via shift. The first two parameters (after the --request) are assigned to the variables symbol and side, the following 8 or 18 parameters are dropped.

agreed, that area is a minefield (a faustian quagmire maybes :joy: )) - i've done some more 'debugging' around it , but will let @faustf go down that :rabbit: :hole: and come back if needed