Bash的未终止字符串/语法错误

时间:2013-01-11 22:09:36

标签: linux bash awk grep

我正在尝试使用以下代码来读取用户的变量;要搜索的文件,搜索字符串,输出所需的空格以及要输出的字段数量。

第一个问题是使用AWK命令。如果我输入一个有效的空格,例如“”(单个空格“或”\ t“),我会收到未终止的字符串和语法错误,只有在我请求输出多个字段时才会出现错误(否则不会添加空格)上)。

其次GREP在使用搜索字符串时似乎有点挑剔。我必须在变量的开头和结尾添加双引号,以便使用整个字符串。

#!/bin/bash
#****************************************************
#Name:          reportCreator.sh
#Purpose:       Create reports from log files
#Author:        
#Date Written:      11/01/2013
#Last Updated:      11/01/2013
#****************************************************

clear

#Determine what to search for
printf "Please enter input file name(s): "
read inputFile
printf "Please enter your search query: "
read searchQuery
printf "Please enter the whitespace character: "
IFS= read whitespace
printf "Please enter the amount of fields to be displayed: "
read fieldAmount

#Add quotation marks to whitespace and searchQuery
whitespace=\""$whitespace"\"
searchQuery=\""$searchQuery"\"

#Declare variables
declare -i counter=0
declare -a fields[$fieldAmount]
declare -a fieldInsert[$fieldAmount]

#While loop for entering fields
while [[ "$counter" -ne "$fieldAmount" ]]
do
        #Ask for field numbers
        printf "Please enter number for required field $((counter+1)): "
        read fields[$counter]
        ((counter++))
done

#Create function to add '$' before every field and the whitespace characters
function fieldFunction 
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi  
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | less #| awk '{print $(fieldFunction)}'

exit 0

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

  1. Grep不理解引号,因此请删除将它们添加到$ searchQuery的行。
  2. 对awk使用双引号而不是单引号,因此$(fieldFunction)将展开。
  3. 解决这个问题(当然还有取消注释awk),它可以工作:

    user@host 15:00 ~ $ cat script
    #!/bin/bash
    #****************************************************
    #Name:          reportCreator.sh
    #Purpose:       Create reports from log files
    #Author:        
    #Date Written:      11/01/2013
    #Last Updated:      11/01/2013
    #****************************************************
    
    clear
    
    #Determine what to search for
    printf "Please enter input file name(s): "
    read inputFile
    printf "Please enter your search query: "
    read searchQuery
    printf "Please enter the whitespace character: "
    IFS= read whitespace
    printf "Please enter the amount of fields to be displayed: "
    read fieldAmount
    
    #Add quotation marks to whitespace and searchQuery
    whitespace=\""$whitespace"\"
    
    #Declare variables
    declare -i counter=0
    declare -a fields[$fieldAmount]
    declare -a fieldInsert[$fieldAmount]
    
    #While loop for entering fields
    while [[ "$counter" -ne "$fieldAmount" ]]
    do
            #Ask for field numbers
            printf "Please enter number for required field $((counter+1)): "
            read fields[$counter]
            ((counter++))
    done
    
    #Create function to add '$' before every field and the whitespace characters
    function fieldFunction
    {
        for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
        do
            fieldInsert[$fieldAmount]="$""${fields[$counter]}"
            if (( counter!=($fieldAmount-1) ))
            then
                printf "${fieldInsert[*]}$whitespace"
            else
                printf "${fieldInsert[*]}"
            fi
        done
    }
    printf "%b\n"
    
    tac $inputFile | grep "$searchQuery" | awk "{print $(fieldFunction)}"
    
    exit 0
    user@host 15:01 ~ $ cat file
    foo two three four
    foo two2 three2 four2
    bar two three four
    user@host 15:01 ~ $ bash script
    Please enter input file name(s): file
    Please enter your search query: foo
    Please enter the whitespace character:
    Please enter the amount of fields to be displayed: 2
    Please enter number for required field 1: 4
    Please enter number for required field 2: 2
    
    four2   two2
    four    two
    user@host 15:01 ~ $
    

答案 1 :(得分:0)

让我们来看看这部分代码:

#Create function to add '$' before every field and the whitespace characters
function fieldFunction
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | awk "{print $(fieldFunction)}"

它可以更简单,更健壮地重写(未经测试):

tac "$inputFile" |
awk -v fieldsStr="${fields[*]}" -v searchQuery="$searchQuery" -v OFS="$whitespace" '
   BEGIN{ numFields = split(fieldsStr,fieldsArr) }
   $0 ~ searchQuery {
      for ( i=1; i <= numFields; i++ )
         printf "%s%s", (i==1?"":OFS), $(fieldsArr[i])
      print "\b"
   }
'

我不知道为什么你需要“tac”打开文件,但我认为你有理由让我把它留下来。