Ответы пользователя по тегу bash
  • Urlencode в bash?

    bmkobzar
    @bmkobzar
    #!/bin/bash
    phpurlencode() {
      local hexchars="0123456789ABCDEF"
      local string="${1}"
      local strlen=${#string}
      local encoded=""
    
      for (( pos=0 ; pos<strlen ; pos++ )); do
            c=${string:$pos:1}
            if [ "$c" == ' ' ];then
                    encoded+='+'
            elif ( [[ "$c" != '!' ]] && [ "$c" \< "0" ] && [[ "$c" != "-" ]] && [[ "$c" != "." ]] ) || ( [ "$c" \< 'A' ] && [ "$c" \> '9' ]  ) || ( [ "$c" \> 'Z' ] && [ "$c" \< 'a' ] && [[ "$c" != '_' ]]  ) || ( [ "$c" \> 'z' ] );then
                    hc=`printf '%X' "'$c"`
                    dc=`printf '%d' "'$c"`
                    encoded+='%'
                    f=$(( $dc >> 4 ))
                    s=$(( $dc & 15 ))
                    encoded+=${hexchars:$f:1}
                    encoded+=${hexchars:$s:1}
            else
                    encoded+=$c
            fi
      done
      echo "${encoded}"    # You can either set a return variable (FASTER) 
      REPLY="${encoded}"   #+or echo the result (EASIER)... or both... :p
    }
    
    echo $1 
    echo http://url/q?=$( phpurlencode "$1" )

    Обработку русских букв можеш сам придумать,
    тут реализация php
    Ответ написан