4

In the middle of the script, I have a command that exposes the local port with ssh -R 80:localhost:8080 localhost.run I need to execute this command in the background, parse the output and save it into a variable.

The output returns:

Welcome to localhost.run!
...
abc.lhrtunnel.link tunneled with tls termination, https://abc.lhrtunnel.link

Need to capture this part:

https://abc.lhrtunnel.link

As a result something like this:

...
hostname=$(command)
echo $hostname
...
user16965639
  • 176
  • 11

1 Answers1

1

Try this Shellcheck-clean code:

#! /bin/bash -p

hostname=$(ssh ...  \
            | sed -n 's/^.*tunneled with tls termination, //p')
declare -p hostname
  • I'm assuming that you don't really want to background the command that generates the output. You just want to run it in a way that allows its output to be captured and filtered. See How do you run multiple programs in parallel from a bash script? for information about how "background" processes are used for parallel processing.
  • The -n option to sed means that it doesn't print lines from the input unless explicitly instructed to print.
  • s/^.*tunneled with tls termination, //p works on input lines that contain anything followed by the string tunneled with tls termination, . It deletes everything on the line up to the end of that string and prints the result, which hopefully will be the URL that you want.
  • declare -p varname is a much more reliable and useful way to show the value of a variable than using echo.
pjh
  • 6,388
  • 2
  • 16
  • 17
  • Thanks for the reply, actually I want to keep this process `ssh ...` in the background and only capture that part of the output. The reason I want it to be in the background is that the main script will not continue until that process is active. So if I execute your code, for example, it exposes the server and outputs everything, I interrupt it with `ctrl+c` and then I see the output of `declare -p hostname` but the server itself is not running(because of SIGINT signal). I want server running and keeping the variable hostname as well for further usage in the script. – user16965639 Mar 04 '22 at 13:48
  • @chingchong, thanks for the clarification. Unfortunately, I'm still not sure what you are trying to do. One way to run it in the background and capture its output would be to have it send its output to a file: `ssh ... >outfile &`. The program could then do other stuff and later get the required information from `outfile` after the `ssh ...` process finished. (See [ProcessManagement - Greg's Wiki](https://mywiki.wooledge.org/ProcessManagement) for relevant information.) Would that work for you? If you can't use a file, you could use a coprocess if you are running Bash 4.0 or later. – pjh Mar 04 '22 at 14:09
  • Unfortunately, `ssh ... >outfile &` captures everything but that part which I need. – user16965639 Mar 04 '22 at 14:23
  • @chingchong, the only ways I can think that `outfile` wouldn't capture all of the output is if `sss ...` isn't finished when the file is read, `ssh ...` died before it completed, or `ssh ...` is sending some of its output to standard error. Try `ssh ... &>outfile &` to send both standard output and standard error to the file. Run `wait` before checking `outfile` to ensure that the background process has completed. – pjh Mar 04 '22 at 14:33
  • `ssh ...` will never finish unless I manually stop/kill that process. – user16965639 Mar 04 '22 at 14:42
  • @chingchong, I think I understand a bit better what is going on now: you run the command, it produces the output that you need after some time, and you then need to kill it before you continue and use the information that it produced. One way to do it would be to poll the output file looking for the required line (e.g. `grep` it in a loop with a `sleep`) and kill the background process when you've got what you need. If the output is always produced within a known time than you could just run it with `timeout` and avoid backgrounding and killing etc. – pjh Mar 04 '22 at 14:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242598/discussion-between-ching-chong-and-pjh). – user16965639 Mar 04 '22 at 15:33