【问题标题】:HOW can I call a function in a bash file script?如何在 bash 文件脚本中调用函数?
【发布时间】:2021-05-12 05:10:41
【问题描述】:

我想对整个文件进行 urldecode,因为其中有几个 %20 和其他 ASCII 数字。 我试过了,但我不知道如何在主程序的脚本中调用上面定义的函数。

    #!/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"                
    

【问题讨论】:

    标签: bash function pass-by-reference urldecode


    【解决方案1】:

    您可以在一个脚本中分离功能:

        #!/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}"                
    } 
    

    然后通过添加source 命令和带有声明函数的脚本,按照您想要的方式在其他脚本中执行此函数:

    source script.sh
    
    while IFS= read -r line; do                
        echo urldecode($line)                
    done < "$1"
    

    【讨论】:

      【解决方案2】:

      程序的格式是正确的。但是您调用函数的方式不正确。

      这是在循环中调用函数的正确方法:

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

      要获得函数的输入,您需要使用 $1、$2、$3 等。 这已经在您的代码中实现。 作为示例:

      # define the function
      myfunction() {
        echo "$1";
        echo "$2";
        echo "$3";
      }
      
      # call the function
      myfunction "First Input" "Second Input" "Third Input"
      

      【讨论】:

      • 非常感谢!你是最棒的。
      • 你应该小心double-quote variable references,特别是如果它们可能包含空格。也就是说,使用urldecode "$line"echo "$1" 而不仅仅是urldecode $lineecho $1
      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多