2013-02-09 47 views
0

我目前正在研究一个旨在为任何基本编辑器添加新项目代的脚本。 我才能产生正确的基本程序根据用户选择的语言中使用以下结构(你好,世界):

#!/bin/sh 
#this is a short example in the case the user selected C as the language 
TXTMAIN="\$TXTMAIN_C" 
$TXTMAIN_C="#include <stdlib.h> 
#include <stdio.h> 
int main(int argc, char const* argv[]) 
{ 
    printf(\"hello, world\n\"); 
    return EXIT_SUCCESS; 
}" 
MAIN="./main.c" 
touch MAIN 
echo -n "$(eval echo $TXTMAIN)" >> "$MAIN" 
gedit MAIN 

这段代码使您在编辑的main.c以下输出:

#include <stdlib.h> #include <stdio.h> int main(int argc, char const* argv[]) { printf("hello, world\n"); return EXIT_SUCCESS; } 

然而,通过更换线13时回声-n “$ TXTMAIN_C” >> “$ MAIN”,它给出正确的输出:

#include <stdlib.h> 
#include <stdio.h> 
int main(int argc, char const* argv[]) 
{ 
    printf("hello, world\n"); 
    return EXIT_SUCCESS; 
} 

我还是不知道这是一个回声或eval问题,或者是否有解决指针类问题的方法。 任何建议都非常欢迎!

+1

单引号是你的朋友。写起来容易多了:'TXTMAIN_C ='#include ...'因为你不需要转义“或\。 – 2013-02-09 13:43:09

回答

4

脚本中有一些错误,它比它应该更复杂。

如果你想使用间接变量那样,使用${!FOO}语法,并提出适当报价:

#!/bin/sh 
#this is a short example in the case the user selected C as the language 
TXTMAIN=TXTMAIN_C       # don't force a $ here 
TXTMAIN_C="#include <stdlib.h> 
#include <stdio.h> 
int main(int argc, char const* argv[]) 
{ 
    printf(\"hello, world\n\"); 
    return EXIT_SUCCESS; 
}" 
MAIN="./main.c" 
echo "${!TXTMAIN}" > "$MAIN"    # overwrite here, if you want to 
              # append, use >>. `touch` is useless 
+0

那么,那是很快......谢谢,你回答我的需要! – Aserre 2013-02-09 13:42:26