当前位置: 技术问答>linux和unix
system函数的疑问
来源: 互联网 发布时间:2016-04-23
本文导语: /* * system.c - Demonstrate the system() call */ #include #include int main(void) { int retval; retval = system("ls -l"); if(retval == 127) { fprintf(stderr, "/bin/sh not availablen"); exit(127); } else if(retval == -1) { perror("system"); exit(EXIT_FA...
/*
* system.c - Demonstrate the system() call
*/
#include
#include
int main(void)
{
int retval;
retval = system("ls -l");
if(retval == 127) {
fprintf(stderr, "/bin/sh not availablen");
exit(127);
} else if(retval == -1) {
perror("system");
exit(EXIT_FAILURE);
} else if(retval != 0) { //这句为什么这么判断不理解。原型:int system(const char *string);
fprintf(stderr, "command returned %dn", retval); //当string为NULL的时候system才返回0,如果正常执行,那么retval !=0
perror("ls"); //岂不是永远成立,这里的语句不是应该执行的么?
} else {
puts("command successfully executed");
}
exit(EXIT_SUCCESS);
}
运行结果:
[root@localhost process]# ./system
总用量 18
-rwxrwxrwx 1 root root 180 2000-07-28 abort.c
-rwxrwxrwx 1 root root 756 2000-07-31 block.c
-rwxrwxrwx 1 root root 530 2000-07-28 child.c
-rwxrwxrwx 1 root root 319 2000-07-28 execve.c
-rwxrwxrwx 1 root root 859 2000-07-31 fkill.c
-rwxrwxrwx 1 root root 5367 11月 4 16:30 getname
-rwxrwxrwx 1 root root 829 11月 4 16:30 getname.c
-rwxrwxrwx 1 root root 5311 11月 4 15:55 ids
-rwxrwxrwx 1 root root 331 2000-07-28 ids.c
-rwxrwxrwx 1 root root 899 2000-07-28 killer.c
-rwxrwxrwx 1 root root 543 2000-07-31 Makefile
-rwxrwxrwx 1 root root 784 2000-07-31 mkset.c
-rwxrwxrwx 1 root root 813 2000-07-31 pending.c
-rwxrwxrwx 1 root root 217 2000-07-28 prpids.c
-rwxrwxrwx 1 root root 889 2000-07-28 resusg1.c
-rwxrwxrwx 1 root root 1423 2000-07-28 resusg2.c
-rwxrwxrwx 1 root root 5445 11月 5 13:08 system
-rwxrwxrwx 1 root root 470 2000-07-28 system.c
-rwxrwxrwx 1 root root 686 2000-07-28 waiter.c
command successfully executed
|
NAME
system - issue a command
SYNOPSIS
#include
int system(const char *command);
RETURN VALUE
If command is a null pointer, system() shall return non-zero to indicate that a command processor is available, or zero if none is available. [CX] The system() function shall always return non-zero when command is NULL.
[CX] If command is not a null pointer, system() shall return the termination status of the command language interpreter in the format specified by waitpid(). The termination status shall be as defined for the sh utility; otherwise, the termination status is unspecified. If some error prevents the command language interpreter from executing after the child process is created, the return value from system() shall be as if the command language interpreter had terminated using exit(127) or _exit(127). If a child process cannot be created, or if the termination status for the command language interpreter cannot be obtained, system() shall return -1 and set errno to indicate the error.
|
The system() function shall always return non-zero when command is NULL.
当 string 为 NULL 的时候,system 返回不是 0
当 string 为 NULL 的时候,system 返回不是 0
|
当参数为空时,如果sh环境正常,则返回0,否则返回-1.system使用这一特性检查系统的shell环境是否正常.
当传入参数非空时返回值:
为0则shell执行成功.
最低一个字节回shell的返回值,其他为system接受到的信号值.
当传入参数非空时返回值:
为0则shell执行成功.
最低一个字节回shell的返回值,其他为system接受到的信号值.
|
man system
SYSTEM(3) Linux Programmer’s Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c command, and returns after the
command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and
SIGQUIT will be ignored.
RETURN VALUE
The value returned is -1 on error (e.g. fork() failed), and the return status of the command other‐
wise. This latter return status is in the format specified in wait(2). Thus, the exit code of the
command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be
that of a command that does exit(127).
If the value of command is NULL, system() returns non-zero if the shell is available, and zero if
not.
system() does not affect the wait status of any other children.
SYSTEM(3) Linux Programmer’s Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c command, and returns after the
command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and
SIGQUIT will be ignored.
RETURN VALUE
The value returned is -1 on error (e.g. fork() failed), and the return status of the command other‐
wise. This latter return status is in the format specified in wait(2). Thus, the exit code of the
command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be
that of a command that does exit(127).
If the value of command is NULL, system() returns non-zero if the shell is available, and zero if
not.
system() does not affect the wait status of any other children.
|
平时还真没留意到command为NULL的情况。