每天学点Linux--(Linux下execl函数的使用)
2013-10-23 19:06
387 查看
execl()函数声明如下:
extern int execl(_const char *_path,const char *_argv[],...,NULL)
简单解释:函数execl()返回值定义为整形,如果执行成功将不返回!执行失败返回-1。
参数列表中char *_path为所要执行的文件的绝对路径,从第二个参数argv开始为执行新的文件所需的参数,最后一个参数必须是控指针(我为了简便用NULL代替)。
举个例子:
一 先来个新程序不带参数的简单例子:
//execl.c
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int test;
if((test=execl("/home/crosslandy/Linux/exec/hello",argv[1],NULL))==-1)
printf("error\n");
return 0;
}
//hello.c
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
printf("hello world\n");
for(i=0;i<argc;i++)
{
printf("parameter %d is:%s\n",i,argv[i]);
}
return 0;
}
解释一下:
execl.c文件:
1定义的整形test用来判断execl是否执行成功,如果失败返回-1输出error;
2第一个参数"/home/crosslandy/Linux/exec/hello"为我所要执行的文件的绝对路径(记住在绝对路径后要写上所要执行的文件的文件名);
3第二个参数为所要执行的新文件的第一个参数,本例子即为./hello
4第三个参数为空指针(因为hello这个程序不需要参数所以直接写最后一个参数空指针);
hello.c文件:
地球人都知道的hello world。只不过在后面我加上了执行该文件是在终端所输入的参数argv[]并对其进行输出。
在终端执行的时候结果如下:
[crosslandy@localhost exec]$ ./execl ./hello
hello world
parameter 0 is:./hello
二 再来个带参数的例子:
//execl.c
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int test;
if((test=execl("/home/crosslandy/Linux/exec/hello",argv[1],argv[2],NULL))==-1)
printf("error\n");
return 0;
}
//hello.c
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
printf("hello world\n");
for(i=0;i<argc;i++)
{
printf("parameter %d is:%s\n",i,argv[i]);
}
return 0;
}
解释一下:
新的例子里我只对execl.c进行了小修改即红色部分argv[2],表示在调用execl函数执行新文件时多添加了一个参数。
在终端执行的时候结果如下:
[crosslandy@localhost exec]$ ./execl ./hello first_parameter
hello world
parameter 0 is:./hello
parameter 1 is:first_parameter
extern int execl(_const char *_path,const char *_argv[],...,NULL)
简单解释:函数execl()返回值定义为整形,如果执行成功将不返回!执行失败返回-1。
参数列表中char *_path为所要执行的文件的绝对路径,从第二个参数argv开始为执行新的文件所需的参数,最后一个参数必须是控指针(我为了简便用NULL代替)。
举个例子:
一 先来个新程序不带参数的简单例子:
//execl.c
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int test;
if((test=execl("/home/crosslandy/Linux/exec/hello",argv[1],NULL))==-1)
printf("error\n");
return 0;
}
//hello.c
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
printf("hello world\n");
for(i=0;i<argc;i++)
{
printf("parameter %d is:%s\n",i,argv[i]);
}
return 0;
}
解释一下:
execl.c文件:
1定义的整形test用来判断execl是否执行成功,如果失败返回-1输出error;
2第一个参数"/home/crosslandy/Linux/exec/hello"为我所要执行的文件的绝对路径(记住在绝对路径后要写上所要执行的文件的文件名);
3第二个参数为所要执行的新文件的第一个参数,本例子即为./hello
4第三个参数为空指针(因为hello这个程序不需要参数所以直接写最后一个参数空指针);
hello.c文件:
地球人都知道的hello world。只不过在后面我加上了执行该文件是在终端所输入的参数argv[]并对其进行输出。
在终端执行的时候结果如下:
[crosslandy@localhost exec]$ ./execl ./hello
hello world
parameter 0 is:./hello
二 再来个带参数的例子:
//execl.c
#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int test;
if((test=execl("/home/crosslandy/Linux/exec/hello",argv[1],argv[2],NULL))==-1)
printf("error\n");
return 0;
}
//hello.c
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
printf("hello world\n");
for(i=0;i<argc;i++)
{
printf("parameter %d is:%s\n",i,argv[i]);
}
return 0;
}
解释一下:
新的例子里我只对execl.c进行了小修改即红色部分argv[2],表示在调用execl函数执行新文件时多添加了一个参数。
在终端执行的时候结果如下:
[crosslandy@localhost exec]$ ./execl ./hello first_parameter
hello world
parameter 0 is:./hello
parameter 1 is:first_parameter
相关文章推荐
- linux中的execl函数使用
- [Linux进程]在父进程和子进程中分别使用execl函数
- 每天一个linux命令,包含最常用的几十个命令的详细使用,让您快速掌握Linux!
- 每天学点Linux之grep命令的使用
- linux下execl函数的使用和比较
- 每天一个linux命令(8):scp使用
- linux下实现每天自动发送服务器日志到邮箱(使用sendmail)
- 每天一个linux命令(1) :find命令的使用
- 每天学点Linux--(File_status_flag和File_description_flag的区别/atoi函数的使用
- 每天学点Linux之sed命令的使用
- 每天一个linux命令,包含最常用的几十个命令的详细使用,让您快速掌握Linux!
- linux下使用crontab如何实现mysql数据库每天自动备份定时备份
- linux下使用cron每天定时执行java程序
- Bash 使用技艺| 如果你每天都在使用linux系统,一定要看
- 每天学点Linux--(curses.h的安装和基本使用)
- [Linux进程]使用execl函数调用date命令
- 每天一个linux命令,包含最常用的几十个命令的详细使用,让您快速掌握Linux!
- 在Linux下安装和使用MySQL
- linux命令深度使用 – ls
- Windows终端使用FTP访问Linux