linux 셸 파이프 명령(pipe)사용 및 셸 리 셋 과 구별
파이프 명령 조작 자 는"|"입 니 다.앞의 명령 을 통 해 전 달 된 정확 한 출력 정보,즉 standard output 의 정보 만 처리 할 수 있 습 니 다.stdandard 에 대해 서 는...
error 정 보 는 직접 처리 능력 이 없습니다.그리고 다음 명령 에 표준 입력 standard input 을 전달 합 니 다.
파이프 명령 사용 설명:
먼저 아래 그림 을 보 세 요.
command 1 이 정확하게 출력 되 었 습 니 다.command 2 의 입력 으로 comand 2 의 출력 은 comand 3 의 입력 으로 comand 3 출력 이 화면 에 바로 표 시 됩 니 다.
파 이 프 를 통과 한 후:comand 1,comand 2 의 정확 한 출력 은 화면 에 표시 되 지 않 습 니 다.
주의:
1.파이프 명령 은 이전 명령 의 정확 한 출력 만 처리 하고 오류 출력 은 처리 하지 않 습 니 다.
2.파이프 명령 오른쪽 명령 은 표준 입력 스 트림 명령 을 받 아야 합 니 다.
실례:
[chengmo@centos5 shell]$ cat test.sh | grep -n 'echo'
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
# test.sh , grep
[chengmo@centos5 shell]$ cat test.sh test1.sh | grep -n 'echo'
cat: test1.sh:
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
#cat test1.sh , , grep
[chengmo@centos5 shell]$ cat test.sh test1.sh 2>/dev/null | grep -n 'echo'
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
# test1.sh /dev/null , grep
[chengmo@centos5 shell]$ cat test.sh | ls
catfile httprequest.txt secure test testfdread.sh testpipe.sh testsh.sh testwhile2.sh
envcron.txt python sh testcase.sh testfor2.sh testselect.sh test.txt text.txt
env.txt release sms testcronenv.sh testfor.sh test.sh testwhile1.sh
# test.sh , ls , ls ,
이 실례 는 위의 두 가지 주의 에 대한 검증 이다.표준 입력 명령 을 받 아야 파이프 오른쪽 에 사용 할 수 있 습 니 다.그렇지 않 으 면 전달 과정 에서 데이터 가 버 려 집 니 다.수신 데이터 파이프 명령 으로 자주 사용 되 는 것 은 sed,awk,cut,head,top,less,more,wc,join,sort,split 등 이 있 습 니 다.모두 텍스트 처리 명령 입 니 다.파이프 명령 과 재 설정 차이
차이 점 은:
1.왼쪽 명령 은 표준 출력 이 있어 야 합 니 다.|오른쪽 명령 은 표준 입력 을 받 아야 합 니 다.
왼쪽 명령 은 표준 출력 이 있어 야 합 니 다>오른쪽 은 파일 만 가능 합 니 다.
왼쪽 명령 은 표준 입력 이 필요 합 니 다<오른쪽 은 파일 만 가능 합 니 다.
2.파이프 가 두 개의 키 프로 세 스 를 촉발 하여"|"양쪽 프로그램 을 실행 합 니 다.재 설정 은 프로 세 스 내 에서 실 행 됩 니 다.
이것들 은 모두 인터넷 에서 많은 정 리 를 한 것 인 데 사실은 용법 을 더 명확 하 게 하기 만 하면 반드시 자신의 다른 묘사 가 있 을 것 이다.
실례:
#
#
[chengmo@centos5 shell]$ cat test.sh| grep -n 'echo'
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
#"|" shell
[chengmo@centos5 shell]$ grep -n 'echo' <test.sh
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
#" " , ( , , )
[chengmo@centos5 shell]$ mail -s 'test' [email protected] <test.sh
[chengmo@centos5 shell]$ cat test.sh|mail -s 'test' [email protected]
# 2 , test.sh 。
[chengmo@centos5 shell]$ (sed -n '1,$p'|grep -n 'echo')<test.sh
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
# 。 , test.sh sed , sed , grep. "()" 。 , 。 test.sh grep 。
#
[chengmo@centos5 shell]$ sed -n '1,$p'<test.sh | grep -n 'echo'
5: echo "very good!";
7: echo "good!";
9: echo "pass!";
11: echo "no pass!";
# , shell , ( , , , 0,1,2 ),
[chengmo@centos5 shell]$ sed -n '1,10p'<test.sh | grep -n 'echo' <testsh.sh
10:echo $total;
18:echo $total;
21: echo "ok";
# , grep , testsh.sh , 2 。 "<" , ,grep testsh.sh , sed 。
#
[chengmo@centos5 shell]$ cat test.sh>test.txt
[chengmo@centos5 shell] cat test.sh|tee test.txt &>/dev/null
# , tee, test.txt , (stdout), /dev/null
#">" , , , , 。 。
[chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh 2>err.txt | grep 'test'
test.sh
testsh.sh
# :test,testsh ,test1.sh , ls err.txt , grep 。
[chengmo@centos5 shell]$ ls test.sh test1.sh testsh.sh &>err.txt | grep 'test'
# ,& err.txt, ,
# ">" , , , 。 , ,
다시 요약 하면:위의 예 를 들 어 볼 수 있 듯 이 방향 을 바 꾸 고 파 이 프 를 사용 할 때 자주 사용 할 수 있 습 니 다.사실은 셸 안에 서 는 항상[모든 큰길 이 로마 로 통한다]입 니 다.일반적으로 명령 간 에 파 라 메 터 를 전달 하 는 것 이 좋 습 니까?아니면 파이프 가 좋 습 니까?출력 결 과 를 처리 하려 면 파일 로 방향 을 바 꾸 는 것 이 좋 습 니 다.다시 방향 을 바 꾸 어 출력 하 는 것 이 좋 습 니 다.
명령 실행 순 서 를 볼 수 있 습 니 다:Linux Shell 어댑터,메타 문자,전의 자 사용 인 스 턴 스 소개
셸 스 크 립 트 수신 파이프 입력
재 미 있 는 질문:
파이프 가 명령 을 받 는 이상 표준 입력 을 받 을 수 있어 야 합 니 다.셸 스 크 립 트 는 이러한 기본 프로그램 을 개발 할 수 있 습 니까?(흔히 볼 수 있 는 것 은 파이프 수신 자로 서 시스템 명령 이다)
인 스 턴 스(testpipe.sh):
#!/bin/sh
if [ $# -gt 0 ];then
exec 0<$1;
# : , ,
fi
while read line
do
echo $line;
done<&0;
#
exec 0&-;
#
실행 결과:
[chengmo@centos5 shell]$ cat testpipe.txt
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.txt
[chengmo@centos5 shell]$ cat testpipe.txt | sh testpipe.sh
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
# cat testpipe.txt testpipe.sh
[chengmo@centos5 shell]$ sh testpipe.sh testpipe.txt
1,t,est pipe
2,t,est pipe
3,t,est pipe
4,t,est pipe
#testpipe.sh
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
용감한 바로 가기 및 우분투 응용 프로그램안녕하세요 여러분, 이 기사에서는 모든 사이트에서 pwa를 생성하고 실행기 응용 프로그램으로 추가하는 방법을 설명하고 싶습니다. 일부 웹사이트는 PWA로 설치를 허용하지 않지만 유사한 애플리케이션을 원합니다. 1. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.