Bash脚本没有通过命令管道

时间:2016-06-17 21:19:07

标签: bash macos awk grep

我在Mac El Capitan上运行了以下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 个答案:

答案 0 :(得分:0)

您的脚本不会将其参数转发给code23。 bash中的函数不会继承父脚本的命令行参数($1$2等)。你必须调用一个bash函数,明确地为它提供参数(好像它是一个单独的程序):

code23 "$1"