尝试创建一个小脚本,能够在输出文件中编写一部分脚本而不做任何更改(按原样)
源文件文本
pip
期望的输出:
apt-get
我得到的是:
echo "
yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0
echo"#${green}Installing packages${reset}#" &&
" >> output.txt
使用CentOS 7最小的全新安装 寻找一个适用于完整脚本/文本的解决方案,没有每行的行更改,我想也可以使用sed来完成...
答案 0 :(得分:23)
你需要逃避反引号(`):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p id="yourid">Hello world.</p>
<p>..</p>
<p>..</p>
<p>..</p>
<p>..</p>
我更喜欢将此方法用于多行:
#!/bin/bash
echo "
yellow=\`tput setaf 3\`
bel=\`tput bel\`
red=\`tput setaf 1\`
green=\`tput setaf 2\`
reset=\`tput sgr0\`
" >> output.txt
答案 1 :(得分:5)
使用单引号来防止扩展:
echo '
yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
' >> output.txt
有关详细信息,请参阅Difference between double and single quote。
如果您的文字包含单引号,则上述内容可能无效。在这种情况下,使用here doc会更安全。例如,如果您插入一行,上述内容将会中断:var='something'
。
使用这里的doc就是这样的:
cat >> output.txt <<'EOF'
yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
var='something'
EOF
答案 2 :(得分:1)
只是迟到了:
echo 'string' >> output
命令非常简单。
但它可能会给你&#39; ...:权限被拒绝&#39;错误与sudo
结合。
我最近对sudo echo 'string \n other string \n' > /path/to/file
最适合我的是什么:
的 printf "Line1\nLine2\nLine3" | sudo tee --append /path/to/file
强>
这是一个额外的,你实际上也有字符串打印到stdout,所以你会看到写入文件的内容。
答案 3 :(得分:0)
感谢chepner
我找到的最佳解决方案是:
echo '
yellow=$(tput setaf 3)
bel=$(tput bel)
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
echo"#${green}Installing packages${reset}#"
' >> output
使用此解决方案,所有文本都会转到输出文件而不进行任何修改,颜色定义也可以不加修改地工作。