Search code examples
bashshelltelnet

Vertical bar in shell script?


I'm dealing with inherited shell scripts, and have encountered a syntax I've never seen before.

host = 192.[mask]
port = 2[mask]

( echo open ${host} ${port}
    ...about six other commands
  echo exit ) | telnet

Now, this makes sense up to the vertical bar. Just commands to open and run commands on a server.

But what's the telnet call actually doing? What does this syntax (of parens and bar) mean? My best guess from this implies that it opens telnet, then runs the following commands from within telnet, but I'd like to be sure.


Solution

  • The parentheses are there to group the output of all the commands that are inside them into one Unix pipe | which goes to the input of the telnet command.

    But the parentheses create a subshell and you don't need any, so you can do:

    {
        telnet_input_line_1
        telnet_input_line_2
        telnet_input_line_3
        (...)
    } | telnet 
    

    You can even replace telnet by netcat; it's better designed for scripting purposes.