이것은 stdin에서 사용자의 입력으로 문자열을받는 쉘 스크립트입니다.awk는 인수와 입력 파일을 어떻게 구분합니까?
#!bin/sh
printf "Enter your query\n"
read query
cmd=`echo $query | cut -f 1 -d " "`
input_file=`echo $query | cut -f 2 -d " "`
printf $input_file
if [ $input_file = "" ]
then
printf "No input file specified\n"
exit
fi
if [ $cmd = "summary" ]
then
awk -f summary.awk $input_file $query
fi
그가
summary a.txt /o foo.txt
지금
cmd
변수 값
summary
을하고
input_file
이
a.txt
걸릴 것입니다 입력 말할 수 있습니다. 그렇지 않나요?
에있는 내용을 토대로 summary.awk이 $input_file
에서 작업하기를 원합니다. 다음과 같이
나의 이해는 다음과 같습니다
입력 파일로 처리됩니다 전달 된 첫 번째 명령 행 인수. 예 : : 입력 파일이 파이프 경우
awk 'code' file arg1 arg2 arg3
만file
가 입력 파일로 처리됩니다, 그것은 입력 파일로 인수가 표시되지 않습니다. 예 : :
cat file | awk 'code' arg1 arg2 arg3
arg1
은 입력 파일로 처리되지 않습니다.
맞습니까?
문제는 왜 summary
을 열려고 내가 awk: cannot open summary (No such file or directory)
을 얻을 입니까? $input_file
다음의 단어입니다.
이 문제를 어떻게 해결합니까?
''query '를''query'를''cut ''으로 구문 분석 한 다음에''read cmd input_file remainder' (IFS가 적당한 값인지 확인하십시오) –
[이 답변] (http : //stackoverflow.com/q/18825007/812102). –