这个问题在这里已有答案:
我试图复制整个脚本,而不是脚本输出,但整个脚本作为文本行复制到名为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
有没有办法做到这一点?
扩展引用
$''
$""
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.
我认为上面的答案涵盖了你想做的事情,但我仍然对你的要求感到困惑。如果要添加到脚本的代码段是静态的(看起来是这样的),您是否可以将其写入文件并执行以下操作:
cat common-code > test.sh
你的脚本内?