在shell脚本中获取该行

我试图让所有的“RESP:0,成功” input命令:RESP:0,成功; – 这里是我的代码,我会采取吗?

**for i in {2..4} do date CMDCount="CMD"$i eval CMD="$"$CMDCount echo "server CMD "${CMD}""${CMDCount}""; echo "" (sleep 1;echo $login; sleep 1;echo ${CMD}; sleep 1;echo $logout;sleep 1)|telnet localhost 9685 sleep 1 done** 

输出是这样的:

 server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxCMD2 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. CONNECTING TO xxx... PROCESS CPL CONNECTED... Enter command:RESP:0,sucess; Enter command: Enter command: Enter command:Connection closed by foreign host. Sat Aug 3 12:18:21 CST 2013 server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCMD3 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. CONNECTING TO xxx... PROCESS CPL CONNECTED... Enter command:RESP:0,sucess; Enter command: Enter command: Enter command:Connection closed by foreign host. Sat Aug 3 12:18:26 CST 2013 server CMD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;CMD4 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. CONNECTING TO xxx... PROCESS CPL CONNECTED... Enter command:RESP:0,sucess; Enter command: Enter command: Enter command:Connection closed by foreign host. 

您可能会发现这些链接有用:

  • 字符串操作
  • 常用表达

而且,要从整个字符串中提取响应部分,可以使用如下所示的内容:

 echo `expr "${CMD}" : '\.*?(RESP)(:)(\\d)\'`