简体   繁体   English

在 bash 脚本中接受多行输入

[英]Accept multiple lines of input in a bash script

I am in the middle of writing a bash script.我正在编写 bash 脚本。 The pending point I am stuck with is how to accept multiple inputs from the user at a time.我遇到的问题是如何一次接受来自用户的多个输入。

To be specific, a user must be able to input multiple domain names when the script asks to enter the input.具体来说,当脚本要求输入时,用户必须能够输入多个域名。

Example, script running portion:示例,脚本运行部分:

Enter the domain names :

and user must be able to enter the domain names line by line either by entering each of them manually or he/she just have to copy domain names list from somewhere and able to paste it in the script input, like as follows:并且用户必须能够通过手动输入每个域名来逐行输入域名,或者他/她只需从某处复制域名列表并能够将其粘贴到脚本输入中,如下所示:

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

Is it possible?.有可能吗?

Yes, you can: use readarray :是的,您可以:使用readarray

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

The last line above just documents what bash now sees as the array.上面的最后一行只记录了 bash 现在看到的数组。

The user can type or copy-and-paste the array names.用户可以键入或复制并粘贴数组名称。 When the user is done, he types Ctrl-D at the beginning of a line.用户完成后,他在一行的开头键入Ctrl-D

Example:示例:

$ 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")'

Use loop :使用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[@]}"

Once you have entered all domain names press ctrl + D to end of input.输入所有域名后,按ctrl + D结束输入。

So @John1024's answer really set me down the right path, but it was still super confusing to me on how to get this data not only assigned to a variable, but also importantly, to preserve both whitespace and newlines.所以@John1024 的回答确实让我走上了正确的道路,但对于如何将这些数据不仅分配给变量,而且重要的是,如何保留空格和换行符,我仍然非常困惑。

After many StackOverflow and StackExchange answers later, I have created with the following snippet that shows how.经过许多 StackOverflow 和 StackExchange 的回答之后,我创建了以下代码片段,展示了如何。 It is from my Uber BashScripts project @ wifi-autorun-on-connect.installer :它来自我的Uber BashScripts 项目@ 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM