我的 bash 脚本有点问题。
#!/bin/bash
ex xxx.html << "HERE"
1,$s/\(foo\)/$1\1/
wq
HERE
这只是我剧本的一小部分。当我运行它时,这是输出。
$1foo
有什么办法可以解决这个问题,所以 $1 将成为脚本的参数?
谢谢!
尝试替换"HERE"
为HERE
(未引用)。也1,$s
变成了1,\$s
。
Here Documents
This type of redirection instructs the shell to read input from the
current source until a line containing only delimiter (with no trailing
blanks) is seen. All of the lines read up to that point are then used
as the standard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter expansion, command substitution, arithmetic expansion, or
pathname expansion is performed on word. If any characters in word are
quoted, the delimiter is the result of quote removal on word, and the
lines in the here-document are not expanded. If word is unquoted, all
lines of the here-document are subjected to parameter expansion, com-
mand substitution, and arithmetic expansion. In the latter case, the
character sequence \<newline> is ignored, and \ must be used to quote
the characters \, $, and `.
If the redirection operator is <<-, then all leading tab characters are
stripped from input lines and the line containing delimiter. This
allows here-documents within shell scripts to be indented in a natural
fashion.
巴什手册。
替换"HERE"
为HERE
(不带引号)并替换1,$
为1,\$
或%
您可以按如下方式编写脚本:
#!/bin/bash
ex xxx.html <<-HERE
%s/foo/$1&/
x
HERE
尽管您也可以构建一个较小的脚本:
#!/bin/bash
sed -i "s/foo/$1&/g" xxx.html
试试这个。
#!/bin/bash
(echo '1,$s/\(foo\)/'"$1"'\1/'; echo 'wq') | ex xxx.html
那是1,$s/\(foo\)/
在单引号中,$1
在双引号中相邻(因此shell替换了参数),\1/
在单引号中相邻。