2016-10-27 150 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而不是>>