2016-10-27 49 views
-2

我对sed相当新,并且试图使用sed将用户输入行插入另一个文本文件的第一行。 shell脚本,我尝试使用是:sed不能在shell脚本中工作

echo -n "Enter values: " 
read text 
echo "You entered: $text" 

sed -i '1i $text' $file 

我得到的回报是:

“的sed:-i可能无法与标准输入使用”

+2

你确定'$ file'不是空的吗?另外,单引号中的“$ text”不会扩展。 – choroba

+0

啊是的,由于某种原因$文件是空的。它没有通过前面的脚本。我将文件更改为直接文件名并得到了“sed:1:”new.txt“:n个命令末尾的多余字符” –

+1

“1i $ text”的声音被解释为“-i”选项。 – choroba

回答

0

我做了这个example.sh:

#!/bin/bash 
file="new.txt" 
echo>$file # the file must not be empty, as sed needs a stream to work with 
echo -n "Enter values: " 
read text 
echo "You entered: $text" 
sed -i "\$a$text" $file 

说明:

越狱$指的是最后一行,而a是要附加的命令。
我想你想学习sed,但显然你应该更喜欢echo而不是>>