在shell脚本中使用Sed插入文本到文件

nev*_*int 1 unix sed

我尝试使用文本插入文件的第一行sed.我在sh 脚本中执行此操作.

但为什么它挂在sed执行线上呢?

#! /bin/sh

# Command to execute
# ./mybashcode.sh test.nbq

nbqfile=$1   
nbqbase=$(basename $nbqfile nbq)
taglistfiletemp="${nbqbase}taglist_temp"
taglistfile="${nbqbase}taglist"


./myccode $nbqfile | 
sort | 
uniq -c | 
awk '{print $2}' > $taglistfiletemp 

noftags=$(wc -l $taglistfiletemp | awk '{print $1}')
echo $noftags


# We want to append output of noftags
# to the first line of taglistfile

sed '1i\
$noftags' > $taglistfile

# why it hangs here
# the content of taglistfile is NIL
Run Code Online (Sandbox Code Playgroud)

Mik*_*aid 6

我不确定你要做什么,sed但它需要两个输入,脚本(通常是搜索/替换)和你想要执行它的数据.如果您只指定一个,则假定它已获得正则表达式并等待数据stdin.由于你没有提供任何东西,stdin它将无限期地挂起.

另外,你有$noftags'而不是' $noftags".先前将输出$noftags,后者将输出变量的内容,因为单引号不允许变量扩展.