执行如下命令,操作系统可以判断第一条语句是否执行成功:
--- Intern/les3 » gcc main.c -o main.out && ./main.out
hello word
--- Intern/les3 » echo $?
0
当执行命令之后 echo $?
输出 0
表示执行成功
错误码,如果 main
函数 返回不是 0
, 不会执行下一句。
--- Intern/les3 » ./main.out && ls
hello word
main函数中的参数 int main(int argv, char* argc[])
可以控制,argv 表示参数的个数,argc[] 表示接受的参数:
--- Intern/les3 » gcc main2.c -o main2.out && ./main2.out
argv is 1
--- Intern/les3 » ./main2.out -l
argv is 2
--- Intern/les3 » ./main2.out -l -a
argv is 3
--- Intern/les3 » ./main3.out -l -a -mks -lll -ppp
argv is 6
argc[0] is ./main3.out
argc[1] is -l
argc[2] is -a
argc[3] is -mks
argc[4] is -lll
argc[5] is -ppp
linux 把所有外设 当作文件。 执行程序,系统启动进程,包含stdio.h
封装,stdio.h
引入一系列指针,定义默认输入输出错误,可以重定向操作:
stdin
stdout
stderr
fprintf(stdout,"please input : \n");
fscanf(stdin, "%d",&a);
fprintf(stderr,"error \n");
重定向命令(默认是终端):
--- Intern/les5 » ./a.out 1>> a.txt
5
3
--- Intern/les5 » ls
a.out a.txt main.c
--- Intern/les5 » cat a.txt
input the int value i
input the int value j
i+j=8
1>> a.txt
表示把标准输出流重定向输出到 a.txt
文件.
>>
不覆盖,只追加。
>
覆盖原文件。
--- Intern/les5 » ls /etc >> etc.txt
--- Intern/les5 » ls
a.out a.txt etc.txt main.c
--- Intern/les5 » cat etc.txt
--- Intern/les5 » ls /etc > etc.txt
--- Intern/les5 » cat etc.txt
< input.txt
表示标准输入流重定向
1>t.txt
表示标准输出流
2>f.txt
表示标准错误流
(方向与c++ cin
cout
相反)
--- Intern/les5 » ./a.out 1>t.txt 2>f.txt
4
0
--- Intern/les5 » cat input.txt
6
8
--- Intern/les5 » cat f.txt
--- Intern/les5 » cat t.txt
input the int value i
input the int value j
i/j=0
|
表示管道,把前一个命令输出转化为后一个命令的输入grep
查询包含指定字符的行ps
查看当前进程--- ~ » ls /etc/ | grep ab
fstab.hd
gettytab
krb5.keytab
rmtab
xtab
--- ~ » ps
PID TTY TIME CMD
11166 ttys000 0:01.30 -zsh
20572 ttys001 0:00.69 -zsh
10915 ttys002 0:01.07 -zsh
15765 ttys003 0:00.85 -zsh
16411 ttys004 0:00.81 -zsh
16757 ttys006 0:00.75 -zsh
--- ~ » ps -e | grep ssh
20732 ttys001 0:00.00 grep -- color=auto --exclude-dir=.bzr --exclude-dir=.cvs --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn
--- Intern/les6 » ./input.out | ./avg.out
8000
7500
1200
1500
0
v = 4550.000000