0

I would like to urldecode a whole file, because there are a few %20 and other ASCII-Numbers in it. I tried this but I do not know how I can call the function defined above in the script in the main routine.

    #!/bin/bash                
    urlencode() {                
        # urlencode <string>                
                    
        old_lc_collate=$LC_COLLATE                
        LC_COLLATE=C                
                    
        local length="${#1}"                
        for (( i = 0; i < length; i++ )); do                
            local c="${1:$i:1}"                
            case $c in                
                [a-zA-Z0-9.~_-]) printf '%s' "$c" ;;                
                *) printf '%%%02X' "'$c" ;;                
            esac                
        done                
                    
        LC_COLLATE=$old_lc_collate                
    }                
                    
    urldecode() {                
        # urldecode <string>                
                    
        local url_encoded="${1//+/ }"                
        printf '%b' "${url_encoded//%/\\x}"                
    }                
                    
    while IFS= read -r line; do                
        echo urldecode($line)                
    done < "$1"                
    
ZygD
  • 22,092
  • 39
  • 79
  • 102
Walter Schrabmair
  • 1,251
  • 2
  • 13
  • 26

2 Answers2

3

The format of the program is correct. But you have called your functions not in a correct way.

This is correct way of calling your function in your loop:

    while IFS= read -r line; do
        urldecode "$line"
    done < "$1"  

To get the inputs for the function you need to use $1, $2, $3, etc. This is already implemented in your code. As a sample:

# define the function
myfunction() {
  echo "$1";
  echo "$2";
  echo "$3";
}

# call the function
myfunction "First Input" "Second Input" "Third Input"
Raham
  • 113
  • 7
  • 1
    THANKS a lot! You are the best. – Walter Schrabmair Feb 08 '21 at 15:43
  • 1
    You should be careful to [double-quote variable references](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable), especially if they might contain spaces. That is, use `urldecode "$line"` and `echo "$1"` instead of just `urldecode $line` and `echo $1`. – Gordon Davisson Feb 08 '21 at 18:59
0

You can separate the functions in one script:

    #!/bin/bash                
urlencode() {                
    # urlencode <string>                
                
    old_lc_collate=$LC_COLLATE                
    LC_COLLATE=C                
                
    local length="${#1}"                
    for (( i = 0; i < length; i++ )); do                
        local c="${1:$i:1}"                
        case $c in                
            [a-zA-Z0-9.~_-]) printf '%s' "$c" ;;                
            *) printf '%%%02X' "'$c" ;;                
        esac                
    done                
                
    LC_COLLATE=$old_lc_collate                
}                
                
urldecode() {                
    # urldecode <string>                
                
    local url_encoded="${1//+/ }"                
    printf '%b' "${url_encoded//%/\\x}"                
} 

And then execute this functions in other script the way you intended by adding the source command and the script with the declared functions:

source script.sh

while IFS= read -r line; do                
    echo urldecode($line)                
done < "$1"
Nahuel
  • 158
  • 8