c_header: system()(linux; <stdlib.h>)
一、源码
1 [root@rocky c]# cat stdlib_header.c 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 6 #include<unistd.h> // sleep() 7 #include<time.h> // time_t, time() 8 9 10 11 void 12 msg1() 13 { 14 int size = 5; 15 char *cmd[] = {"pwd", "ls", "date", "uname -a", "cd ~; ls -l"}; 16 17 for(int i=0; i<size; i++) 18 { 19 printf("\n"); 20 printf("\n\t== run_cmd_start: [ %s ]\t==\n", cmd[i]); 21 system(cmd[i]); 22 printf("\t-- run_cmd_end: [ %s ]\t--\n", cmd[i]); 23 printf("\n"); 24 } 25 } 26 27 28 void 29 msg2(int size, char **cmd) 30 { 31 32 for(int i=1; i<size; i++) 33 { 34 printf("\n"); 35 printf("\n\t== run_cmd_start: [ %s ]\t==\n", cmd[i]); 36 system(cmd[i]); 37 printf("\t-- run_cmd_end: [ %s ]\t--\n", cmd[i]); 38 printf("\n"); 39 } 40 } 41 42 43 void 44 msg3() 45 { 46 const int SIZE = 10; 47 time_t t; 48 int rand_dig = 0; 49 printf("\n\trand_max = %d\n\n", RAND_MAX); 50 for(int i=1; i<= SIZE; i++) 51 { 52 printf("\twait[ 2 ] seconds...\n"); 53 sleep(2); 54 srand( (unsigned)time(&t) ); 55 rand_dig = ( rand()%100 ); 56 printf("\t\trand_number<rang:0~100>[time_%d] = %d\n", i, rand_dig ); 57 } 58 printf("\n\n"); 59 } 60 61 62 63 64 int 65 main(int argc, char *argv[], char *envp[]) 66 { 67 68 // msg1(); 69 msg2(argc, argv); 70 // msg3(); 71 72 73 return 0; 74 } 75 [root@rocky c]# 76 [root@rocky c]#
二、运行结果:
1 [root@rocky c]# alias 2 alias cp='cp -i' 3 alias egrep='egrep --color=auto' 4 alias fgrep='fgrep --color=auto' 5 alias grep='grep --color=auto' 6 alias l.='ls -d .* --color=auto' 7 alias lcc='gcc -Wall -g -o' 8 alias ll='ls -l --color=auto' 9 alias ls='ls --color=auto' 10 alias mv='mv -i' 11 alias rm='rm -i' 12 alias xzegrep='xzegrep --color=auto' 13 alias xzfgrep='xzfgrep --color=auto' 14 alias xzgrep='xzgrep --color=auto' 15 alias zegrep='zegrep --color=auto' 16 alias zfgrep='zfgrep --color=auto' 17 alias zgrep='zgrep --color=auto' 18 [root@rocky c]# 19 [root@rocky c]# 20 [root@rocky c]# cat stdlib_header.c 21 #include<stdio.h> 22 #include<stdlib.h> 23 #include<string.h> 24 25 #include<unistd.h> // sleep() 26 #include<time.h> // time_t, time() 27 28 29 30 void 31 msg1() 32 { 33 int size = 5; 34 char *cmd[] = {"pwd", "ls", "date", "uname -a", "cd ~; ls -l"}; 35 36 for(int i=0; i<size; i++) 37 { 38 printf("\n"); 39 printf("\n\t== run_cmd_start: [ %s ]\t==\n", cmd[i]); 40 system(cmd[i]); 41 printf("\t-- run_cmd_end: [ %s ]\t--\n", cmd[i]); 42 printf("\n"); 43 } 44 } 45 46 47 void 48 msg2(int size, char **cmd) 49 { 50 51 for(int i=1; i<size; i++) 52 { 53 printf("\n"); 54 printf("\n\t== run_cmd_start: [ %s ]\t==\n", cmd[i]); 55 system(cmd[i]); 56 printf("\t-- run_cmd_end: [ %s ]\t--\n", cmd[i]); 57 printf("\n"); 58 } 59 } 60 61 62 void 63 msg3() 64 { 65 const int SIZE = 10; 66 time_t t; 67 int rand_dig = 0; 68 printf("\n\trand_max = %d\n\n", RAND_MAX); 69 for(int i=1; i<= SIZE; i++) 70 { 71 printf("\twait[ 2 ] seconds...\n"); 72 sleep(2); 73 srand( (unsigned)time(&t) ); 74 rand_dig = ( rand()%100 ); 75 printf("\t\trand_number<rang:0~100>[time_%d] = %d\n", i, rand_dig ); 76 } 77 printf("\n\n"); 78 } 79 80 81 82 83 int 84 main(int argc, char *argv[], char *envp[]) 85 { 86 87 // msg1(); 88 msg2(argc, argv); 89 // msg3(); 90 91 92 return 0; 93 } 94 [root@rocky c]# 95 [root@rocky c]# 96 [root@rocky c]# lcc stdlib_header stdlib_header.c && ./stdlib_header "pwd" "ls -l" "mkdir folder" "ls -l" 97 98 99 == run_cmd_start: [ pwd ] == 100 /root/user/lidawei/tmp/c 101 -- run_cmd_end: [ pwd ] -- 102 103 104 105 == run_cmd_start: [ ls -l ] == 106 total 80 107 -rw-r--r-- 1 root root 625 Dec 28 03:11 arg_test.c 108 -rw-r--r-- 1 root root 900 Dec 31 01:04 asm_test.c 109 -rw-r--r-- 1 root root 539 Dec 29 01:25 char_test.c 110 -rw-r--r-- 1 root root 66 Jan 4 00:28 me_backup.txt 111 -rw-r--r-- 1 root root 107 Jan 4 01:16 me.txt 112 -rw-r--r-- 1 root root 2352 Dec 28 23:56 pointer_array.c 113 -rw-r--r-- 1 root root 490 Dec 29 17:09 preprocessor.c 114 -rw-r--r-- 1 root root 410 Jan 4 01:16 stdio_header.c 115 -rwxr-xr-x 1 root root 28736 Jan 5 01:00 stdlib_header 116 -rw-r--r-- 1 root root 1158 Jan 5 00:57 stdlib_header.c 117 -rw-r--r-- 1 root root 154 Jan 4 21:08 template.c 118 -rw-r--r-- 1 root root 536 Dec 29 18:34 thread_test.c 119 -rw-r--r-- 1 root root 1166 Jan 4 23:38 time_header.c 120 -- run_cmd_end: [ ls -l ] -- 121 122 123 124 == run_cmd_start: [ mkdir folder ] == 125 -- run_cmd_end: [ mkdir folder ] -- 126 127 128 129 == run_cmd_start: [ ls -l ] == 130 total 80 131 -rw-r--r-- 1 root root 625 Dec 28 03:11 arg_test.c 132 -rw-r--r-- 1 root root 900 Dec 31 01:04 asm_test.c 133 -rw-r--r-- 1 root root 539 Dec 29 01:25 char_test.c 134 drwxr-xr-x 2 root root 6 Jan 5 01:00 folder 135 -rw-r--r-- 1 root root 66 Jan 4 00:28 me_backup.txt 136 -rw-r--r-- 1 root root 107 Jan 4 01:16 me.txt 137 -rw-r--r-- 1 root root 2352 Dec 28 23:56 pointer_array.c 138 -rw-r--r-- 1 root root 490 Dec 29 17:09 preprocessor.c 139 -rw-r--r-- 1 root root 410 Jan 4 01:16 stdio_header.c 140 -rwxr-xr-x 1 root root 28736 Jan 5 01:00 stdlib_header 141 -rw-r--r-- 1 root root 1158 Jan 5 00:57 stdlib_header.c 142 -rw-r--r-- 1 root root 154 Jan 4 21:08 template.c 143 -rw-r--r-- 1 root root 536 Dec 29 18:34 thread_test.c 144 -rw-r--r-- 1 root root 1166 Jan 4 23:38 time_header.c 145 -- run_cmd_end: [ ls -l ] -- 146 147 [root@rocky c]# 148 [root@rocky c]# 149 [root@rocky c]#
标签:rw,alias,--,system,Jan,header,linux,root From: https://www.cnblogs.com/lnlidawei/p/17026432.html