我有一个脚本,该脚本具有$variable
存储行数不确定的文本。假设我已经命名了它script.bash
,它位于$HOME
:
#!/bin/bash
# commands on the beginning of my script
variable='Lorem Ipsum is simply dummied
text of the printing and
typesetting industry. Lorem Ipsum has been the
industry standard dummy text ever since the 1500s'
# commands at the end of my script
我想创建一个新脚本,该脚本将更改variable
on中的文本script.bash
。代码的想法是这样的:
#!/bin/bash
file="$HOME/script.bash"
mynewvariable='one
two
three'
substitute_everything_inside_variable () {
# code
}
substitute_everything_inside_variable "$mynewvariable" "$file"
只要只有一行,它就可以sed
像下面这样用新的一行替换整个行:
sed -i "5s/.*/$mynewvariable/" "$HOME/script.bash"
但是考虑到我需要用单引号代替所有内容(并且其中的文本行数是可变的),我不知道如何使用命令行工具来实现。是否有一种无需阅读和解释每一行会发生什么的方式?
替换文件的整个内容比更改包含许多您不想破坏的其他内容的文件中的多行带引号的字符串要容易得多。
尝试将的内容$variable
放入文件中(例如vfile.txt
),然后让您的shell脚本执行variable="$(cat vfile.txt)"
。
然后,您可以使用要替换或修改的任何方法vfile.txt
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句