Bash:尝试通过数组传递命令使函数起作用

时间:2019-06-13 20:11:37

标签: arrays bash function shell

我正在尝试将数组中的命令传递给bash(3.2.57)中的函数,但是它不起作用。

脚本内容:

head_num=20
get_logs=`ls -1 myfiles*`

function parse() {
    file="$1"
    commands="$2"
    awk="$3"
    summary_title="$4"
    touch $file
    for i in $get_logs; do
        echo "parsing logs for mytext in $i..."
        egrep "mytext" $i | awk -v var="$awk" '{print var}' | "$commands" >> "$file"
    done

    sorted_combined=`sort "$file" | uniq -c | sort -r`
    echo "$sorted_combined" > "$file"
    echo "user/comp/ip file is \"$file\""
    echo
    echo "" >> "$summary_file"
    echo "=============== $3 =============== " >> "$summary_file"
    head -n "$head_num" "$file" >> "$summary_file"

    echo "Complete!"
    echo "top $head_num for each category summarized in $summary_file"
}

summary_file="summary.out"

COMMAND=("sed s/://g | sed s/Username\=//g")
AWK_FIELDS=("$8, $11")

parse results.out "${COMMAND[@]}" "${AWK_FIELDS[@]}" "USERS, COMPUTERS, IPS"

我得到以下结果:

parsing logs for mytext in myfiles...
./events.sh: line 119: sed s/://g | sed s/Username\=//g: No such file or directory
user/comp/ip file is "results.out"

Complete!
top 20 for each category summarized in summary.out

我正在尝试将脚本中的重复代码块移入此功能,以提高效率并使脚本整洁。

0 个答案:

没有答案