当前位置: 技术问答>linux和unix
linux下分号的问题
来源: 互联网 发布时间:2015-12-24
本文导语: #include #include #include #include #include #include #include #define BUFFER 255 int main(int argc,char **argv) { char buffer[BUFFER+1]; int fd[2]; if(argc!=2) { fprintf(stderr,"Usage:%s stringna",argv[0]); exit(1); } ...
#include
#include
#include
#include
#include
#include
#include
#define BUFFER 255
int main(int argc,char **argv)
{
char buffer[BUFFER+1];
int fd[2];
if(argc!=2)
{
fprintf(stderr,"Usage:%s stringna",argv[0]);
exit(1);
}
if(pipe(fd)!=0)
{
fprintf(stderr,"Pipe Error:%sna",strerror(errno));
exit(1);
}
if(fork()==0)
{
close(fd[0]);
printf("Child[%d] Write to pipena",getpid());
snprintf(buffer,BUFFER,"%s",argv[1]);
write(fd[1],buffer,strlen(buffer));
printf("Child[%d] Quitna",getpid());
exit(0);
}
else
{
close(fd[1]);
printf("Parent[%d] Read from pipena",getpid());
memset(buffer,'',BUFFER+1);
read(fd[0],buffer,BUFFER);
printf("Parent[%d] Read:%sn",getpid(),buffer);
exit(1);
}
}
用的是FC4,上面是一个管道应用的例子.编译成功之后,运行如
[root@localhost linux_c]# ./pipe addfasdf
Child[14654] Write to pipe
Child[14654] Quit
Parent[14653] Read from pipe
Parent[14653] Read:addfasdf
[root@localhost linux_c]# ./pipe bkjadl;sf
Child[14656] Write to pipe
Child[14656] Quit
Parent[14655] Read from pipe
Parent[14655] Read:bkjadl
bash: sf: command not found
第二个例子中为什么无法读取;与;号后面的字符?
#include
#include
#include
#include
#include
#include
#define BUFFER 255
int main(int argc,char **argv)
{
char buffer[BUFFER+1];
int fd[2];
if(argc!=2)
{
fprintf(stderr,"Usage:%s stringna",argv[0]);
exit(1);
}
if(pipe(fd)!=0)
{
fprintf(stderr,"Pipe Error:%sna",strerror(errno));
exit(1);
}
if(fork()==0)
{
close(fd[0]);
printf("Child[%d] Write to pipena",getpid());
snprintf(buffer,BUFFER,"%s",argv[1]);
write(fd[1],buffer,strlen(buffer));
printf("Child[%d] Quitna",getpid());
exit(0);
}
else
{
close(fd[1]);
printf("Parent[%d] Read from pipena",getpid());
memset(buffer,'',BUFFER+1);
read(fd[0],buffer,BUFFER);
printf("Parent[%d] Read:%sn",getpid(),buffer);
exit(1);
}
}
用的是FC4,上面是一个管道应用的例子.编译成功之后,运行如
[root@localhost linux_c]# ./pipe addfasdf
Child[14654] Write to pipe
Child[14654] Quit
Parent[14653] Read from pipe
Parent[14653] Read:addfasdf
[root@localhost linux_c]# ./pipe bkjadl;sf
Child[14656] Write to pipe
Child[14656] Quit
Parent[14655] Read from pipe
Parent[14655] Read:bkjadl
bash: sf: command not found
第二个例子中为什么无法读取;与;号后面的字符?
|
; is commands separator in bash shell. If you want to use the ; itself, you have to escape it with ;
Regards
Regards
|
如楼上所说的,转义就可以了,因为‘;’是命令分隔符啊,所以把后面的sf又当成是一个命令了。所以会报bash: sf: command not found