2016-06-17 4 views
-2

올바른 출력을 생성하지 못하는 Mac El Capitan에서 실행되는 다음 bash 스크립트를 가지고 있지만 명령 행에서 동일한 명령을 실행하면 제대로 동작합니다. 나는 bash 스크립트를 만들려고하고 있기 때문에 특정 오류 코드를 생성하는 로그 파일의 상위 디렉토리를 찾을 때마다 이러한 명령을 수행 할 필요가 없습니다. 여기 Bash 스크립트가 명령을 통해 파이핑되지 않습니다.

로그 파일의 편집 됨 발췌 한 것입니다 : 여기

[2016/06/16 18:11:38] [12.34.56.789] [username] [R] D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/001 Drafts/ 
[2016/06/16 18:11:39] [12.34.56.789] [username] [R] D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/ 
-- 
-- 
[2016/06/17 15:57:20] [12.34.56.789] [username] [R] [Upload] [D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/12-15-13 IW CCTV Inspect/My Pane/3637/2013.08.14_0922/SnapShots/36_37_MGP_101.4.jpg] [182.27 KB] 
[2016/06/17 15:57:26] [12.34.56.789] [username] [R] [Upload] [D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/12-15-13 IW CCTV Inspect/My Pane/3637/2013.08.14_0922/SnapShots/36_37_MGP_330.2.jpg] [174.96 KB] 
[2016/06/17 15:57:26] [12.34.56.789] [username] [R] Number of files considered for upload: 999 
[2016/06/17 15:57:26] [12.34.56.789] [username] [R] Number of files uploaded: 135 
[2016/06/17 15:57:27] [12.34.56.789] [username] [R] Number of files in sync: 864 
[2016/06/17 15:57:27] [12.34.56.789] [username] [R] idevs error: some files could not be transferred (code 23) at main.c(1234) [generator=1.0.19] 

[2016/06/17 09:37:08] [12.34.56.789] [username] [R] receiving file list 
[2016/06/17 09:37:17] [12.34.56.789] [username] [R] D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/12-15-13 IW CCTV Inspect/My Pane/3738/ 
[2016/06/17 09:37:17] [12.34.56.789] [username] [R] D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/12-15-13 IW CCTV Inspect/My Pane/3738/2013.08.14_0941/ 
[2016/06/17 09:37:17] [12.34.56.789] [username] [R] D/Data/Shared/Files/ABC Real Estate - Not the bridge Project/Formal Discovery/2016-04-27 Docs from OC (to be processed)/12-15-13 IW CCTV Inspect/My Pane/3738/2013.08.14_0941/Reports/ 

이다 나는에 많은 것들을 시도 내 현재 스크립트 :

내가 실행 명령의 시리즈에 있었다
#!/bin/bash 

code23() { 

egrep 'code\ 23' -C5 $1 | egrep '\[[A-Z]\/.*' | awk -F\] '{print $6}' | tee ${1}_code_error_files.txt 

awk -F\/ '{print $1 "/" $2 "/" $3 "/"}' ${1}_code_error_files.txt | sed 's/\[//g' | sed 's/^\s*\[//g' | sort -u | tee ${1}_code23_errors_dirs.txt 

} 


if [[ -z $1 ]]; then 
    echo "Usage: $ bash get_errors logfile.txt" 
    echo "Please use a file containing the IDrive or IBackup command logs." 
else 
    code23 
fi 

위에서 언급 한 함수 code23()

내 UI에서 rtf 파일로 로그를 다운로드합니다. 내가 cat 그 파일을 하나의 파일로. Bash는 이것을 텍스트 파일로 읽는 데 아무런 문제가 없습니다. 나는이 파일에 대한 다음 일련의 명령을 실행하고 그것은 나에게 코드 23 오류가있는 라인의 부모 디렉토리를 얻을 수 있습니다 원하는 출력을 가지고 :

`egrep 'code\ 23' -C5 error_file.txt egrep '\[[A-Z]\/.*' | awk -F\] '{print $6}' | sort -u > output_file.txt` 
# This successfully printed the full file path of the file that caused the error. 

`awk -F\/ '{print $1 "/" $2 "/" $3 "/" }' output_file.txt | sort -u` 
# This successfully prints the first three directories of the folders that contain errors. 

'sed 's/\[//g' output_file.txt 
# I edited the output file with vim to clean it up, by removing the spaces in front of each line, and remove the `[` in front of each directory. 

날 내가 뭐하는 거지 알려주세요 틀리게. 다음과 같이 당신의 도움의 모든

덕분에, 나는 코드를 편집하고 지금은 잘 작동합니다 :

#!/bin/bash 

code23() { 

egrep 'code\ 23' -C5 $1 | egrep '\[[A-Z]\/.*' | awk -F\[ '{print $7}' | sort -u | tee $1_code23_error_files.txt | awk -F\/ '{print $1 "/" $2 "/" $3 "/"}' | sed 's/^\s*\[//g' | sort -u | tee $1_code23_error_dirs.txt 

} 

if [[ -z "$1" ]]; then 
    echo "Usage: $ bash get_errors logfile.txt" 
    echo "Please use a file containing the ABC Company or XYZ Company command logs." 
else 
    code23 "$1" 
fi 

감사합니다!

+1

모든 backticks는 무엇입니까? 그 문자 그대로 있습니까? –

+3

http://stackoverflow.com/help/mcve를 준수하십시오. –

+0

샘플 로그 파일에서 예상되는 결과를 추가하십시오. 마지막으로'egrep'과'awk' 라인을 둘러싼 백틱을 원하지 않습니까? 줄 앞의 4 칸을 사용하여 code/data/errMsg/etc로 나타나게하십시오. 그리고 편집에 대한'help' 절을 읽으십시오. 행운을 빕니다. – shellter

답변

0

스크립트가 인수를 code23으로 전달하지 않습니다. bash의 함수는 상위 스크립트의 명령 행 인수 ($1, $2 등)를 상속하지 않습니다. 명시 적으로 인수를 제공하는 bash 함수를 호출해야합니다 (별도의 프로그램처럼) :

code23 "$1" 
+0

수정 된 스크립트를 자유롭게 살펴보십시오. 문제없이이 두 줄을 제 기능에 연결할 수 있었고 이로 인해 도움이되었습니다. 또한, 인수'$ 1 '을 전달하는 것은 대단히 도움이되었습니다. 고맙습니다! – Debug255

관련 문제