在網上看到了一個把 C語言和bash雜並起來的例子,這個示子如下所示。在下面這個例子中,我們把腳本用#if 0這個預編譯給起來,這樣就不會讓其編譯到C語言中了。
#if 0
echo "Hello from bash!"
exit
#endif
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
puts("Hello from C!");
return EXIT_SUCCESS;
}
下面,讓我看看如果來使用這樣的程序:
$ sh test.sh.c Hello from bash! $ gcc test.sh.c -o test $ ./test Hello from C!
你甚至還可以做一個自我編譯,並自我運行的源代碼。如下所示:
#if 0
file=`mktemp`
gcc -o $file $0
$file
rm $file
exit
#endif
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
puts("Hello from C!");
return EXIT_SUCCESS;
}
運行:
$ sh test.sh.c Hello from C! $
當然,我並不建議你在真正的開發環境中這樣使用,我只不過是在介紹一個比較有趣的用法,僅此而已