如何从控制台读取多行文本,然后将内容保存到带换行符的文件中?

How to read multiple line of text from console and then save the content to a file with line break?

因为用户不知道如何使用vi,我需要创建一个解决方案,让用户使用复制和粘贴的方式在AIX服务器上创建一个文件。

我参考下面的解决方法link:

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

我创建了一个脚本如下:

#!/bin/bash
echo "Paste the Bulletin:"
IFS= read -d '' -n 1 keyvariable   
while IFS= read -d '' -n 1 -t 2 c
do
    keyvariable+="$c"$'\n'
done
echo $keyvariable >abc.txt
echo "Thanks!" 

输入:

sdsdfsdfsd
4354353453
/*-/*/-/--/`

预期 abc.txt 内容:

sdsdfsdfsd
4354353453
/*-/*/-/--/

实际abc.txt内容:

sd s d f s d f s d 4 3 5 4 3 5 3 4 5 3  /*- /* /-/- - /

不幸的是,原文中的换行符没有保留,那么如何保留换行符然后将文本写入输出文件?

如果您的 AIX 上有 bash,那么您可以试试这个:

#!/bin/bash
echo "Paste the Bulletin:"
while IFS= read -r -t 2 c
do
    keyvariable+="$c"$'\n'
done
echo "$keyvariable" >abc.txt
echo "Thanks!" 

作为简化/备选方案,考虑对工作流程进行轻微更改,然后执行以下操作:

#!/bin/sh
echo "Paste the Bulletin; hit Control-D on its own line when you're done"
cat > abc.txt