0

我正在尝试用 ac 程序构建一个 shell 程序。基本上,我需要一个文本文件,其中包含一遍又一遍地重复的相同 shell 命令,但其中一部分已更改。我试图构建一个 C 程序从 1 计数到 750 并打印出更改了一位数的段落,但它试图读取 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 ) 我想要的只是让它打印出更改最后一行中以 s1.hoc 开头并以 s750.hoc 结尾的 .hoc 文件的命令。

提前感谢您的建议。

4

3 回答 3

3

除了您的程序将生成一个相对无用的长输出列表,该列表包含 750 倍几乎相同的脚本,您可能不得不将其拆分为 750 个单独的脚本文件,还有一些具体问题会导致它发生故障:

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

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

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

    printf("cd "\${NRNHOME}/i386/bin"");
    

不知道为什么你似乎在这里逃避 $...

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

无论如何,在纯 shellscript 中执行此操作非常容易,并且只需 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 或 here 文档

于 2013-07-03T17:39:08.463 回答
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
于 2013-07-03T17:39:05.753 回答
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 这里学习。请参阅此处提供的资源以了解语言

于 2013-07-03T17:36:46.343 回答