0

我的 Bash 脚本出现问题,当我想使用我的脚本时,它在第 8 行和第 43 行显示“模糊重定向”。我编写了脚本来处理 GPS 数据。

我的脚本如下:

#! /bin/bash

# Change 'Raw' to 'csv' for output file name
outf=${1/Raw/csv}

# Print current input- and output file name
echo  \> $outf

# Remove 'Carriage Returns' and get GGA and VTG strings from file
tr -d '\r' < | egrep "GGA|VTG" | awk '
BEGIN {
# Field separator is ','
FS="," 
}

# Convert deg min sec to decimal degrees
function degAngl(minAngl) {
pos = index(minAngl, ".") - 3
deg = substr(minAngl, 1, pos)
min = substr(minAngl, pos + 1) / 60
return deg + min
}

{
# if it is a VTG dump all stored data
if (substr() == "VTG") {
    head=$2
    velo=$8
    printf ("%08.1f,%1.8f,%1.8f,%1.3f,%d,%d,%1.1f,%1.1f,%1.1f\n", time, long, lati, alti, qual, nsat, hdop, head, velo)
}

# it is not a VTG but a GGA, store data, correct for different altitude definitions
else {
    time = 
    lati = degAngl()
    long = degAngl()
    qual = 
    nsat = 
    hdop = 
    alti = ( < 0) ?  +  : 
}
}
' >$outf

当我运行它时,我得到:

process.sh: Line 10: : ambiguous redirect
process.sh: Line 43: 1: ambiguous redirect

关于以下几行:

line 10: tr -d '\r' < | egrep "GGA|VTG" | awk '
line 43: ' >$outf

这是什么以及如何解决这个问题?

4

1 回答 1

1

在heredoc中使用 awk 脚本。

#! /bin/bash

# Change 'Raw' to 'csv' for output file name
outf=${1/Raw/csv}

# Print current input- and output file name
echo $1 \> $outf

awk_script=$(cat << 'EOS'
BEGIN {
# Field separator is ','
FS=","
}

# Convert deg min sec to decimal degrees
function degAngl(minAngl) {
pos = index(minAngl, ".") - 3
deg = substr(minAngl, 1, pos)
min = substr(minAngl, pos + 1) / 60
return deg + min
}

{
# if it is a VTG dump all stored data
if (substr($1,4) == "VTG") {
    head=$2
    velo=$8
    printf ("%08.1f,%1.8f,%1.8f,%1.3f,%d,%d,%1.1f,%1.1f,%1.1f\n", time, long, lati, alti, qual, nsat, hdop, head, velo)
}

# it is not a VTG but a GGA, store data, correct for different altitude definitions
else {
    time = $2
    lati = degAngl($3)
    long = degAngl($5)
    qual = $7
    nsat = $8
    hdop = $9
    alti = ($10 < 0) ? $10 + $12 : $10
}
}
EOF
)

# Remove 'Carriage Returns' and get GGA and VTG strings from file
tr -d '\r' <$1 | egrep "GGA|VTG" | awk "${awk_script}" >$outf
于 2013-05-29T09:34:06.827 回答