简体   繁体   English

使用echo / linux使用多行脚本创建输出文件

[英]Creating an output file with multi line script using echo / linux

Trying to create a small script capable to write a part off the script in the output file without any changes, (as is) 尝试创建一个小脚本,能够在输出文件中编写一部分脚本而不做任何更改(按原样)

source file text 源文件文本

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

Desired output: 期望的输出:

yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0
echo"#${green}Installing packages${reset}#" &&

What I'm getting is: 我得到的是:

yellow=^[[33m
bel=^G
red=^[[31m
green=^[[32m
reset=^[(B^[[m
echo"#${green}Installing packages${reset}#" &&

Using CentOS 7 Minimal fresh install Looking for a solution to be applied to the full script/text, no the line per line changes, I suppose may be done using sed too ... 使用CentOS 7最小的全新安装寻找一个适用于完整脚本/文本的解决方案,每行没有变化,我想也可以使用sed来完成...

You need to escape the backticks ( ` ): 你需要逃避反引号( ` ):

#!/bin/bash
echo "
yellow=\`tput setaf 3\`
bel=\`tput bel\`
red=\`tput setaf 1\`
green=\`tput setaf 2\`
reset=\`tput sgr0\`
" >> output.txt

As a bonus: 作为奖励:

I prefer using this method for multiline: 我更喜欢将此方法用于多行:

#!/bin/bash
cat << 'EOF' >> output.txt
yellow=$(tput setaf 3)
bel=$(tput bel)
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
EOF

Use single quote to prevent expansions: 使用单引号来防止扩展:

echo '
yellow=`tput setaf 3`
bel=`tput bel`
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
' >> output.txt

For more details see Difference between double and single quote . 有关详细信息,请参阅双引号和单引号之间的差异


If your text includes single quote then the above may not work. 如果您的文字包含单引号,则上述内容可能无效。 In that case using a here doc would be safer. 在这种情况下,使用这里的doc会更安全。 For example, the above will break if you insert a line: var='something' . 例如,如果您插入一行,上面的内容将会中断: var='something'

Using a here doc it will be like this: 使用这里的文档将是这样的:

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

Just a late addition: 只是一个迟到的补充:

The echo 'string' >> output command is simple and great. echo 'string' >> output命令简单而且很棒。 But it may will give you the ' ...: Permission denied ' error combined with sudo . 但它可能会给你' su:Permission denied '错误和sudo

I recently had a lil issue with sudo echo 'string \\n other string \\n' > /path/to/file 我最近有一个问题是sudo echo 'string \\n other string \\n' > /path/to/file

What worked for me the best: 什么对我最有用:
printf "Line1\\nLine2\\nLine3" | sudo tee --append /path/to/file

It's an extra that you actually have the string printed to the stdout too, so you will see what was written to the file. 这是一个额外的,你实际上也有字符串打印到stdout,所以你会看到写入文件的内容。

Thanks to chepner 感谢chepner

Best solution I found is : 我找到的最佳解决方案是:

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

With this solution all text goes to output file without modifications and color definitions are working without modifications too. 使用此解决方案,所有文本都可以在没有修改的情况下转到输出文件,并且颜色定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM