我的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
这是什么以及如何解决这个问题?
答案 0 :(得分: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