簡體   English   中英

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

[英]Print shell command lines in C with printf()

我正在嘗試使用AC程序構建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);
    }
}

它告訴我所有目錄都未聲明,並且期望$之前有一個? 我想要的是讓它打印出最后一行中更改.hoc文件的命令,這些文件以s1.hoc開始,以s750.hoc結尾。

預先感謝您的建議。

除了您的程序將生成相對無用的長輸出列表(實際上是您可能必須在750個單獨的腳本文件中分割的相同腳本)的750倍的較長輸出列表這一事實之外,還有許多具體問題導致其無法正常運行:

    printf("#!/bin/sh");
  1. 因為該語句在循環內,所以它將
  2. printf本身不會添加換行符,如果希望下一個printf從新行開始,請添加\\n

     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做到這一點非常容易,只需一擊即可將輸出發送到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文檔

好吧,保持您的原始意圖(僅出於教育目的):

#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

或: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

可以做到的

#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 請參閱此處提供的資源以了解有關語言的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM