在bash中使用grep时,将带有交互的命令的输出放在变量中

时间:2022-03-31 19:30:57

This program I use has it's own variables to set when you run it, so I want to set those variables and then greping the output then storing it inside a variable. However, I don't know how to go about this the correct way. The idea I have doesn't work. The focus is on lines 7 through 14.

我使用的这个程序在运行时有自己设置的变量,所以我想设置这些变量然后greping输出然后将它存储在变量中。但是,我不知道如何以正确的方式解决这个问题。我的想法不起作用。重点是第7到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

3 个解决方案

#1


Putting together everyone's answers, I came across a working solution myself. 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!

#2


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

我将您的程序运行在一个单独的.sh脚本文件中,然后从您的第一个文件运行该脚本,将您想要传递的参数作为命令行参数传递。这样你就可以单独测试它们。

You could also do it in a function, but I like the modularity of the second script. I don't udnerstand exactly 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

#3


If sflist needs interaction, I'd try something like this:

如果sflist需要互动,我会尝试这样的事情:

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

Unfortunately, you have to type exit as part of the interaction.

不幸的是,您必须输入exit作为交互的一部分。

#1


Putting together everyone's answers, I came across a working solution myself. 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!

#2


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

我将您的程序运行在一个单独的.sh脚本文件中,然后从您的第一个文件运行该脚本,将您想要传递的参数作为命令行参数传递。这样你就可以单独测试它们。

You could also do it in a function, but I like the modularity of the second script. I don't udnerstand exactly 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

#3


If sflist needs interaction, I'd try something like this:

如果sflist需要互动,我会尝试这样的事情:

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

Unfortunately, you have to type exit as part of the interaction.

不幸的是,您必须输入exit作为交互的一部分。