使用printf()在C中打印shell命令行

时间:2013-07-03 17:16:10

标签: c shell printf

我正在尝试用c程序构建一个shell程序。基本上我需要一个文本文件,其中一遍又一遍地重复相同的shell命令,但更改了一个部分。我尝试构建一个从1到750计数的C程序,并打印出一个更改了一个数字的段落,但它正在尝试读取shell命令并给我错误。我如何忽略shell命令并打印printf中的内容?

以下是该计划:

#include <stdio.h>

int main ()
{
    int x;
    for(x=1;x<751;x++){
        printf("#!/bin/sh");
        printf("NRNHOME="/Applications/NEURON-7.3/nrn"");
        printf("\NEURONHOME="${NRNHOME}/share/nrn"");
        printf("CPU=i386");
        printf("NRNBIN="${NRNHOME}/i386/bin/"");
        printf("PATH="${NRNHOME}/i386/bin:${PATH}"");
        printf("export NRNHOME");
        printf("export NEURONHOME");
        printf("export NRNBIN");
        printf("export PATH");
        printf("export CPU");
        printf("nrncarbon=yes");
        printf("export nrncarbon");
        printf("cd "\${NRNHOME}/i386/bin"");
        printf("./nrngui.sh "/Applications/NSD2013/s%d.hoc"\n\n",x);
    }
}

它告诉我所有的目录都是未申报的,它预计a)在$之前?我想要的是打印出更改.hoc文件的命令,这些命令在最后一行以s1.hoc开头并以s750.hoc结尾。

提前感谢您的建议。

3 个答案:

答案 0 :(得分:3)

除了你的程序将生成一个相对无用的长输出列表,其中750个几乎相同的脚本,你可能需要在750个单独的脚本文件中进行切换,有一些具体问题导致它出现故障:

    printf("#!/bin/sh");
  1. 因为这个语句在循环内部,所以它将是
  2. printf不会自行添加换行符,如果您希望下一个printf在新行开头,请添加\n

    printf("\NEURONHOME="${NRNHOME}/share/nrn"");
    
  3. 非常确定\N一开始是无意的

  4. 字符串中的双引号应该被转义,例如\"

    printf("cd "\${NRNHOME}/i386/bin"");
    
  5. 不确定为什么你似乎逃脱了这里的$ ...

        printf("./nrngui.sh "/Applications/NSD2013/s%d.hoc"\n\n",x);
    }
    

    无论如何,在纯shellcript中执行此操作非常容易,并且在1次打击中将输出发送到750个不同的脚本文件:

    for i in {1..750}  ; do
    cat << EOT > the_output_script$i.sh
    #!/bin/sh
    NRNHOME="/Applications/NEURON-7.3/nrn"
    ...      
    # If you want bash to ignore variables that should be evaluated later on
    # you just need to escape the dollarsign
    NRNBIN="\${NRNHOME}/i386/bin/"
    ./nrngui.sh "/Applications/NSD2013/s$i.hoc
    EOT
    # we're after the end of the heredoc, so here we can add other stuff that
    # needs to be done in the loop, like changing the file's access:
    chown ug+x the_output_script$i.sh
    done
    

    将生成750个名为the_output_script1.sh的文件到the_output_script750.sh,这是我认为你真正想要的。顺便说一句,这个脚本中使用的核心技巧叫做heredoc or here document

答案 1 :(得分:1)

好的,保持原意(仅限教育用途):

#include <stdio.h>

int main ()
{
    int x;

    for(x=1;x<751;x++) {
        printf("#!/bin/sh\n");
        printf("NRNHOME=\"/Applications/NEURON-7.3/nrn\"\n");
        printf("NEURONHOME=\"${NRNHOME}/share/nrn\"\n");
        printf("CPU=i386");
        printf("NRNBIN=\"${NRNHOME}/i386/bin/\"\n");
        printf("PATH=\"${NRNHOME}/i386/bin:${PATH}\"\n");
        printf("export NRNHOME");
        printf("export NEURONHOME");
        printf("export NRNBIN");
        printf("export PATH");
        printf("export CPU");
        printf("nrncarbon=yes");
        printf("export nrncarbon");
        printf("cd \"${NRNHOME}/i386/bin\"\n");
        printf("./nrngui.sh \"/Applications/NSD2013/s%d.hoc\"\n\n",x);
    }
}

现在,另一种方法是(my_script.sh):

#!/bin/sh
NRNHOME=/Applications/NEURON-7.3/nrn
NEURONHOME=${NRNHOME}/share/nrn
CPU=i386
NRNBIN=${NRNHOME}/i386/bin/
PATH=${NRNHOME}/i386/bin:${PATH}
export NRNHOME
export NEURONHOME
export NRNBIN
export PATH
export CPU
nrncarbon=yes
export nrncarbon
cd ${NRNHOME}/i386/bin
./nrnqui.sh /Applications/NSD2013/s*.hoc // the lazy way

OR:my_script.sh://带有for循环

#!/bin/sh
NRNHOME=/Applications/NEURON-7.3/nrn
NEURONHOME=${NRNHOME}/share/nrn
CPU=i386
NRNBIN=${NRNHOME}/i386/bin/
PATH=${NRNHOME}/i386/bin:${PATH}
export NRNHOME
export NEURONHOME
export NRNBIN
export PATH
export CPU
nrncarbon=yes
export nrncarbon
cd ${NRNHOME}/i386/bin
for i in {1..750}
do
    ./nrnqui.sh /Applications/NSD2013/s{$i}.hoc
done

答案 2 :(得分:0)

这可以做到,

#include <stdio.h>

int main ()
{
    int x;
    for(x=1;x<751;x++){
        printf("#!/bin/sh\n");
        printf("NRNHOME=\"/Applications/NEURON-7.3/nrn\"\n");
        printf("NEURONHOME=\"${NRNHOME}/share/nrn\"\n");
        printf("CPU=i386\n");
        printf("NRNBIN=\"${NRNHOME}/i386/bin/\"\n");
        printf("PATH=\"${NRNHOME}/i386/bin:${PATH}\"\n");
        printf("export NRNHOME\n");
        printf("export NEURONHOME\n");
        printf("export NRNBIN\n");
        printf("export PATH\n");
        printf("export CPU\n");
        printf("nrncarbon=yes\n");
        printf("export nrncarbon\n");
        printf("cd \"${NRNHOME}/i386/bin\"\n");
        printf("./nrngui.sh \"/Applications/NSD2013/s%d.hoc\"\n\n",x);
    }
}

我强烈建议您学习printf here。请参阅here给出的来源了解语言