bash中如何去掉逗号

[ad_1]

大家好,我有一个代码,这个代码打印这个

https://api.binance.com/api/v3/ticker?symbols=["BTCUSDT","BNBBTC",]&windowSize=1d&type=full

我想删除方括号内的最后一个逗号我想要这样的东西

https://api.binance.com/api/v3/ticker?symbols=["BTCUSDT","BNBBTC"]&windowSize=1d&type=full

怎么可能这样做

我尝试过的:

<pre>urlencode() {
    local string="$1"
    local length="${#string}"
    local result=""

    for (( i = 0; i < length; i++ )); do
        local c="${string:$i:1}"
        case $c in
            [-_.~a-zA-Z0-9]) result+="$c" ;;
            *) result+="%$(printf '%02X' "'$c")" ;;
        esac
    done

    echo "$result"
}


# ----------------  BINANCE  -------------------------------------
# Market
# Market Data

ServerBinance="https://api.binance.com/api/v3"
response="" 
_Bin_RollingWindowPriceChangeStat() {
    local apiUrl="$ServerBinance/ticker"

    # Verifica la presenza di symbol o symbols
    if [[ "$1" == *","* ]]; then
        IFS=',' read -ra symbols <<< "$1"
        local encodedSymbols=$(IFS=,; printf "\"%s\"," "${symbols[@]}")
        apiUrl+="?symbols=[$encodedSymbols]"
        shift
    elif [ -n "$1" ]; then
        apiUrl+="?symbol=[\"$1\"]"
        shift
    fi

    # Aggiungi i restanti parametri
    if [ -n "$1" ] && [ -n "$2" ]; then
        apiUrl+="&windowSize=$1&type=$2"
    fi

    # Visualizza l'URL costruito prima della chiamata di curl
    echo "URL: $apiUrl"

    response=$(curl -s -X GET "$apiUrl")
}

解决方案1

使用字符串替换: 如何本机替换 Bash 中的子字符串[^]

要搜索的字符串: ",]

将其替换为的字符串: "]

如果存在“备用逗号”,它将被删除。 如果不是,则字符串将保持不变。

[ad_2]

コメント

标题和URL已复制