Bash:关于printf

时间:2017-08-22 15:33:48

标签: bash variables printf

我在bash中有一个脚本,它用另一个脚本打印一些文件,它再次执行相同的工作(第一个脚本打印另一个可执行脚本,再次生成第三个脚本):

#!/bin/bash
# it is a first script which I have
home=$(pwd)
output=${home}/output

printf "#!/bin/bash
# it is a second script which will be printed upon execution of the first script
k=2 # number of resubmission
i=32 # cpu to use
time="1:30:00"
HOME=\$(pwd)
for sim in \${HOME}/* ; do
  cd \${sim}
  printf \"#!/bin/csh
  # it is a third script which will be printed only upon execution of the second script
set cnt    = \${k}
set cntmax = 10

while ( \${cnt} <= \${cntmax} )
do something
done" > \${sim}/the_inside_script.sh
cd -
done" > ${output}/the_outside_script.sh

这是关于第三个(内部)脚本文件的问题:

printf \"#!/bin/csh

set cnt    = \${k}
set cntmax = 10

while ( \${cnt} <= \${cntmax} ) # I need always escape these variables
do something
done"

因为它将由另一个将由第一个打印的脚本打印,我需要在第三个中转义每个变量 - 两次 - 所以在the_inside_script.sh中变量\ $ {cnt}和\ $ {cntmax}应该仍然被转义(它现在不起作用)并且$ {k}不应该被转义(现在是正确的)。内部脚本的预期输出应该包含所有打开的变量:

#!/bin/csh
# it is a third script which will be printed only upon execution of the second script
set cnt    = 2
set cntmax = 10

while ( ${cnt} <= ${cntmax} ) # here the variables are present explicitly
do something
done

在当前版本中,不会预期$ {cnt}和$ {cntmax},因此会丢失。

#!/bin/csh
# it is a current version of the third script which will be printed only upon execution of the second script
set cnt    = 2
set cntmax = 10

while (  <=  ) # here the variables are present explicitly
do something
done

如何在printf的第二次正确转义\ $ {cnt}和\ $ {cntmax}?

我感谢您的建议!

0 个答案:

没有答案