在bash中传递多个参数(不带getopt)

时间:2014-06-17 01:30:00

标签: bash

我正在处理一个bash脚本,我需要将两个参数传递给一个函数。当我运行脚本时,一切正常,除非它解析我传递给它的文件,我总得到的结果是Association(这是写在文本文件中)。你能告诉我需要做什么,以便它解析正确的信息,这应该是我输入的uid吗?我在输入123456789中写道,因为它在文本文件中,但我再次得到Association作为结果。

echo "Please enter the UID: ";
read uid

echo "Please enter server file: ";
read server
uidAssoc(){
            arg1=$1
            arg2=$2
            for arg1 in $(cat ~/jlog/"$2"); do awk 'match($i, $2){ print substr($i, RSTART, RLENGTH)} ' ~/jlog/"$2";done;}
    uidAssoc "$uid" "$server"
    exit

1 个答案:

答案 0 :(得分:1)

函数的右括号需要前面的空格(reference)。更可读地格式化代码将有所帮助。

uidAssoc(){
    arg1=$1
    arg2=$2
    for arg1 in $(cat ~/jlog/"$2"); do
        awk 'match($i, $2){ print substr($i, RSTART, RLENGTH)} ' ~/jlog/"$2"
    done
}

有些问题:

  • 为什么要指定arg1arg2但从不使用它们?
  • awk脚本中的$i是什么? (因为i未设置,最终将评估整行)
  • 您是否知道单引号内的$2与单引号外的$2不同?
相关问题