1
我想將shell變量作爲命令行參數傳遞給C程序。爲了實踐這一點,我寫了一個基本的C程序,該程序如下:如何在C程序中將shell變量作爲命令行參數傳遞
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv){
int i, a, prod=1 ;
if(argc==2)
a = atoi(argv[1]);
else
a = 8 ;
printf("value of cmd: %d\n",a);
for(i=1; i <= a ; i++)
prod *= i ;
printf("factorial %d!= %d\n",a, prod);
}
我們通過shell腳本變量的說法,我寫了一個小腳本如下:
#!/bin/bash
echo "Welcome to the small shell scripting!!!"
for ((i=4; i<9;i++))
do
"../programming/ctest/arg $i"
done
其中../programming/ctest/arg是上述C程序的可執行文件。但是當我運行的腳本輸出是../programming/ctest/arg 4沒有這樣的文件或目錄等等。而沒有shell變量運行它提供了正確的輸出。 請看看,如果這個問題解決了,我會用它來進一步自動化
@DanielFischer,我找不到你。 OP的意思是什麼,在問題出現15分鐘之前它不能接受答案?請解釋一下。我剛剛投票,因爲答案對我有用。 – user976754 2013-02-17 20:11:16
@ user976754如果你在'../programming/ctest/arg「$ i」'中引用變量$ i,如果參數中包含空格 – user000001 2013-02-28 12:18:58