【问题标题】:Accept multiple lines of input in a bash script在 bash 脚本中接受多行输入
【发布时间】:2023-03-23 03:10:02
【问题描述】:

我正在编写一个 bash 脚本。我坚持的未决点是如何一次接受来自用户的多个输入。

具体来说,当脚本要求输入时,用户必须能够输入多个域名。

示例,脚本运行部分:

Enter the domain names :

并且用户必须能够通过手动输入每个域名来逐行输入域名,或者他/她只需从某处复制域名列表并将其粘贴到脚本输入中,如下所示:

domain1.com
domain2.com
domain3.com
domain4.com

有可能吗?

【问题讨论】:

    标签: linux bash unix awk sed


    【解决方案1】:

    是的,您可以:使用readarray

    printf "Enter the domain names: "
    readarray -t arr
    # Do something...
    declare -p arr
    

    上面的最后一行只是记录了 bash 现在看到的数组。

    用户可以键入或复制并粘贴数组名称。用户完成后,他在一行的开头键入 Ctrl-D

    例子:

    $ bash script
    Enter the domain names: domain1.com
    domain2.com
    domain3.com
    domain4.com
    declare -a arr='([0]="domain1.com" [1]="domain2.com" [2]="domain3.com" [3]="domain4.com")'
    

    【讨论】:

    • 我发布了一个部分基于此代码的 sn-p。我完全不确定declare -p 应该做什么,但它绝对没有把它放在变量中。 gist.github.com/hopeseekr/460700d166487dcd11d2dbd5f36b8077
    • @TheodoreR.Smith declare -p arr 的目的是打印 以标准输出变量的内容。在答案的示例中,declare -p 行是产生输出 eclare -a arr='([0]="domain1.com" [1]="domain2.com" [2]="domain3.com" [3]="domain4.com")' 的原因
    【解决方案2】:

    使用loop:

    #!/bin/bash
    
    arrDomains=()
    echo "Enter the domain names :"
    
    while read domain
    do
        arrDomains+=($domain)
        # do processing with each domain
    done
    
    echo "Domain List : ${arrDomains[@]}"
    

    输入所有域名后,按ctrl + D 结束输入。

    【讨论】:

    • 谢谢一百万 :)
    【解决方案3】:

    所以@John1024's answer 确实让我走上了正确的道路,但对于如何将这些数据不仅分配给变量,而且重要的是要同时保留两者,我仍然超级感到困惑空格和换行符。

    在之后的许多 StackOverflow 和 StackExchange 答案之后,我使用以下 sn-p 创建了显示方法。来自我的Uber BashScripts project@wifi-autorun-on-connect.installer

    #############################################################
    # Grab the script from an existing file -or- user input...  #
    #                                                           #
    # Copyright © 2020 Theodore R. Smith                        #
    # License: Creative Commons Attribution v4.0 International  #
    # From: https://github.com/hopeseekr/BashScripts/           #
    # @see https://stackoverflow.com/a/64486155/430062          #
    #############################################################
    function grabScript()
    {
        if [ ! -z "$1" ] &&  [ -f "$1" ]; then
            echo $(<"$1")
        else
            echo "" >&2
            echo "Please type/paste in bash script you wish to be run when NetworkManager connects to '${HOTSPOT}'." >&2
            echo "Press CTRL+D when finished." >&2
            echo "You should start with '#!/bin/bash'..." >&2
            echo "" >&2
    
            # Read user input until CTRL+D.
            # @see https://stackoverflow.com/a/38811806/430062
            readarray -t user_input
    
            # Output as a newline-dilemeted string.
            # @see https://stackoverflow.com/a/15692004/430062
            printf '%s\n' "${user_input[@]}"
        fi
    }
    
    SCRIPT=$(grabScript "$2")
    
    # Preserve white spaces and newlines.
    # @see https://stackoverflow.com/a/18018422/430062
    echo "$SCRIPT"
    

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 1970-01-01
      • 2022-10-21
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 2011-10-24
      相关资源
      最近更新 更多