How to read multi-line input in a Bash script? [closed]

£可爱£侵袭症+ 提交于 2019-12-05 04:46:21

You just have to decide how much to read.

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!" 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!