【解決方法】Bash が cile でエラーを返す …なぜですか?

プログラミングQA


こんにちは、私は bash に関数を持っています、私にとっては正しいですが、なぜ私に言い続けるのか分かりません

./BBSW.sh: 17: Syntax error: Bad for loop variable

誰かが私に大丈夫かどうか確認できますか?

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

    # Verifica se la sintassi del ciclo for è supportata
    if [ -z "$(bash -c 'for ((i=0; i<1; i++)); do echo "OK"; done' 2>/dev/null)" ]; then
        echo "Errore: La sintassi del ciclo for non è supportata in questa shell."
        return 1
    fi

    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"
}

私のバージョンは

 bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

i use linux mint 64 last version 

私が試したこと:

このモードでも試してみましたが何もありませんでした

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"
}

解決策 1

コメント

タイトルとURLをコピーしました