将程序从c转换为bash脚本

将程序从c转换为bash脚本,c,bash,C,Bash,我用c语言创建了一个小程序。这个程序使用fork()函数在中创建了一些子进程。创建的进程数量作为控制台的第一个参数。我想有人帮我把这个程序从c转换成bash脚本 /* The first argument is the amount of the procceses to be created*/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> main(int argc, char **a

我用c语言创建了一个小程序。这个程序使用fork()函数在中创建了一些子进程。创建的进程数量作为控制台的第一个参数。我想有人帮我把这个程序从c转换成bash脚本

/* The first argument is the amount of the procceses to be created*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main(int argc, char **argv)
{
    int  pid,i;
    int pnumber;
    pnumber=atoi(argv[1]);//Converting the first arg to int so i can put in for loop
    for(i=1;i<=pnumber;i++){
        pid=fork();// Creating the child procceses with fork

        if(pid!=0)  { //The child procces prints its id and then exit
             printf("The id the created proccess is:%d  and it is a child proccess \n",pid);
             printf("RETURN\n");
             exit(1);
        }                    
    }
}
/*第一个参数是要创建的进程数量*/
#包括
#包括
#包括
主(内部argc,字符**argv)
{
int-pid,i;
整数;
pnumber=atoi(argv[1]);//将第一个arg转换为int,以便输入for循环
for(i=1;ifork()是编译程序用来创建另一个进程的系统调用。在shell脚本中不需要它。您只需使用

myscript.sh &

在脚本中启动新流程。

您知道
seq
的可移植性不强。或者至少它不在我的系统中。:-)@Donal Fellows-
seq
是GNU核心实用程序的一部分。
bash
是GNU外壳。同样的论点也适用于
tr
,或者适用于任何安装了bash作为附加组件的系统。我同意,测试您打算调用和处理的任何可执行文件或由于其缺失而失败的良好实践,但大多数默认情况下打包GNU的现代发行版提供了一致的
seq
。事实上,除非您只考虑Linux,否则您的信息有些错误。还有许多其他Unix,GNU核心实用程序并没有广泛传播。
bash
更为常见(例如:OSX Leopard)
seq
?(而不是在while循环中递减arg)
for((i=0;如果您删除代码,这并不重要,但是检查
fork()
的返回看起来不正确,对于孩子来说应该是
(pid==0)
myscript.sh &