Inputting the output of a command with interaction within a variable when using grep in bash

This program that I am using has its own variables that need to be set when it starts up, so I want to set those variables and then flatten the output and then store it inside a variable. However, I don't know how to do this correctly. The idea doesn't work for me. The focus is on lines 7 through 14.

1  #!/usr/local/bin/bash
2  source /home/gempak/NAWIPS/Gemenviron.profile
3  FILENAME="$(date -u '+%Y%m%d')_sao.gem"
4  SFFILE="$GEMDATA/surface/$FILENAME"
5  echo -n "Enter the station ID: "
6  read -e STATION
7  OUTPUT=$(sflist << EOF
8  SFFILE = $SFFILE
9  AREA = @$STATION
10 DATTIM = all
11 SFPARM = TMPF;DWPF
12 run
13 exit
14 EOF)
15 echo $OUTPUT

      

But I get this:

./listweather: line 7: unexpected EOF while looking for matching `)'
./listweather: line 16: syntax error: unexpected end of file

      

0


a source to share


3 answers


Combining all the answers I came across a working solution. This code works for me:

#!/usr/local/bin/bash
source /home/gempak/NAWIPS/Gemenviron.profile
FILENAME="$(date -u '+%Y%m%d')_sao.gem"
SFFILE="$GEMDATA/surface/$FILENAME"
echo -n "Enter the station ID: "
read -e STATION

OUTPUT=$(sflist << EOF
SFFILE = $SFFILE
AREA = @$STATION
DATTIM = ALL
SFPARM = TMPF;DWPF
run
exit
EOF
)
echo $OUTPUT | grep $STATION

      



Thanks everyone!

+1


a source


I would put your program in a separate .sh script file, and then run the script from your first file, passing in the arguments you want to pass as command line arguments. This way you can test them separately.

You can also do this in a function, but I like the modularity of the second script. I don't understand what you are trying to do above, but something like:



runsflist.sh:
#!/bin/bash

FILENAME="$(date -u '+%Y%m%d')_sao.gem"
SFFILE="$GEMDATA/surface/$FILENAME"
AREA = @$STATION
DATTIM = all
SFPARM = TMPF;DWPF
grep $STATION | sflist

main.sh:
#!/bin/bash

echo -n "Enter the station ID: "
read -e STATION
OUTPUT=`runsflist.sh`
echo $OUTPUT

      

0


a source


If sflist requires interaction, I would try something like this:

SFFILE=$(
  ( echo SFFILE = "$SFFILE"
    echo AREA = "@$STATION"
    echo DATTIM = all
    echo SFPARM = TMPF;DWPF
    echo run
    cat
  ) | sflist)

      

Unfortunately, you need to enter exit

as part of the interaction.

0


a source







All Articles