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
...

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

7 Comments

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.
@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 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.
Unfortunately, ssh ... >outfile & captures everything but that part which I need.
@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.
ssh ... will never finish unless I manually stop/kill that process.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.