如何在Bash脚本中读取多行输入

如何在Bash脚本中读取多行输入

本文介绍了如何在Bash脚本中读取多行输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过shellscript将文件从粘贴"存储在文件中,并以可变的行数存储.

I want store in a file and in a variable multiples lines from a "paste" via shellscript.

我该如何完成呢?

示例:

echo "Paste the  certificate key:"

1fv765J85HBPbPym059qseLx2n5MH4NPKFhgtuFCeU2ep6B19chh4RY7ZmpXIvXrS7348y0NdwiYT61
1RkW75vBjGiNZH4Y9HxXfQ2VShKS70znTLxlRPfn3I7zvNZW3m3zQ1NzG62Gj1xrdPD7M2rdE2AcOr3
Pud2ij43br4K3729gbG4n19Ygx5NGI0212eHN154RuC4MtS4qmRphb2O9FJgzK8IcFW0sTn71niwLyi
JOqBQmA5KtbjV34vp3lVBKCZp0PVJ4Zcy7fd5R1Fziseux4ncio32loIne1a7MPVqyIuJ8yv5IJ6s5P
485YQX0ll7hUgqepiz9ejIupjZb1003B7NboGJMga2Rllu19JC0pn4OmrnxfN025RMU6Qkv54v2fqfg
UmtbXV2mb4IuoBo113IgUg0bh8n2bhZ768Iiw2WMaemgGR6XcQWi0T6Fvg0MkiYELW2ia1oCO83sK06
2X05sU4Lv9XeV7BaOtC8Y5W7vgqxu69uwsFALripdZS7C8zX1WF6XvFGn4iFF1e5K560nooInX514jb
0SI6B1m771vqoDA73u1ZjbY7SsnS07eLxp96GrHDD7573lbJXJa4Uz3t0LW2dCWNy6H3YmojVXQVYA1
v3TPxyeJD071S20SBh4xoCCRH4PhqAWBijM9oXyhdZ6MM0t2JWegRo1iNJN5p0IhZDmLttr1SCHBvP1
kM3HbgpOjlQLU8B0JjkY8q1c9NLSbGynKTbf9Meh95QU8rIAB4mDH80zUIEG2qadxQ0191686FHn9Pi

读取并存储文件,例如/tmp/$ keyfile读取并将其存储在变量$ keyvariable

read it and store it file say /tmp/$keyfileread it and store it in a variable $keyvariable

推荐答案

您只需要决定要阅读多少即可.

You just have to decide how much to read.

如果这是唯一输入,则可以读取直到文件末尾.大多数UNIX实用程序是这样工作的:

If this is the only input, you could read until end of file. This is how most UNIX utilities work:

#!/bin/bash
echo "Pipe in certificate, or paste and it ctrl-d when done"
keyvariable=$(cat)

如果您想在以后的脚本中继续阅读内容,可以阅读直到看到空白行为止.

If you want to continue reading things later in the script, you can read until you see a blank line:

#!/bin/bash
echo "Paste certificate and end with a blank line:"
keyvariable=$(sed '/^$/q')

如果您希望它在交互中更像是魔术,那么您可以阅读直到脚本消失两秒钟而无需输入:

If you want it to feel more like magic interactively, you could read until the script has gone two seconds without input:

#!/bin/bash
echo "Paste your certificate:"
IFS= read -d '' -n 1 keyvariable
while IFS= read -d '' -n 1 -t 2 c
do
    keyvariable+=$c
done
echo "Thanks!"

这篇关于如何在Bash脚本中读取多行输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:38