我试图复制整个脚本,而不是脚本输出,而是将整个脚本复制为名为test.sh的文件中的文本行。我正在使用另一个脚本来执行此操作,因此这就是为什么我没有使用像vi这样的文本编辑器的原因。
我不能使用echo“”或echo“,因为脚本字符串中包含双引号和单引号。
我使用过但失败了的是:
echo "rm disktemp;./xpinfo -i|awk '{print $1}'|grep rhdisk|sed 's!/dev/r!!'>disktemp;for i in $(cat disktemp);do ./xpinfo -i|grep $i|sed 's!/dev/r!!'|awk '{print $6","$1'};done" > test.sh
有什么办法吗?
答案 0 :(得分:1)
扩展报价
$''
$""
echo $'rm disktemp;./xpinfo -i|awk \'{print $1}\'|grep rhdisk|sed \'s!/dev/r!!\'>disktemp;for i in $(cat disktemp);do ./xpinfo -i|grep $i|sed \'s!/dev/r!!\'|awk \'{print $6","$1\'};done'
输出:
rm disktemp;./xpinfo -i|awk '{print $1}'|grep rhdisk|sed 's!/dev/r!!'>disktemp;for i in $(cat disktemp);do ./xpinfo -i|grep $i|sed 's!/dev/r!!'|awk '{print $6","$1'};done
不幸的是,您需要用\转义每个引号,以将其保留在输出中。最好将所有内容都转义,但正如您在下面看到的,仅用于$''或$“”的那个就足够了:
echo $'This is a text \'single\' and "double" quote in it.'
This is a text 'single' and "double" quote in it.
echo $"This is a text 'single' and \"double\" quote in it."
This is a text 'single' and "double" quote in it.
echo $'This is a text \'single\' and \"double\" quote in it.'
This is a text 'single' and "double" quote in it.
答案 1 :(得分:0)
我认为上面的答案涵盖了您想要做的事情,但是您的要求令我有些困惑。如果要添加到脚本中的代码段是静态的(看起来像是静态的),您是否不能一次将其写入文件并执行以下操作:
cat common-code > test.sh
在您的脚本内?