Is possible "merge" command? This is full and long "version":
#!/bin/bash
function test {
docker_container_proxy_name="cloudflared"
real_domain_of_cloudflared="https://([a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60}).trycloudflare.com"
docker_container_1_uri_outside="$(docker logs ${docker_container_proxy_name}1 2>&1 | grep -Eo $real_domain_of_cloudflared | tail -n 1)"
docker_container_2_uri_outside="$(docker logs ${docker_container_proxy_name}2 2>&1 | grep -Eo $real_domain_of_cloudflared | tail -n 1)"
docker_container_3_uri_outside="$(docker logs ${docker_container_proxy_name}3 2>&1 | grep -Eo $real_domain_of_cloudflared | tail -n 1)"
}
test
into like this:
#!/bin/bash
function test {
docker_container_proxy_name="cloudflared"
real_domain_of_cloudflared="https://([a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60}).trycloudflare.com"
command="$(docker logs ${docker_container_proxy_name} 2>&1 | grep -Eo $real_domain_of_cloudflared | tail -n 1)"
docker_container_1_uri_outside="${command}1"
docker_container_2_uri_outside="${command}2"
docker_container_3_uri_outside="${command}3"
}
test
command simplification
CodePudding user response:
Almost correct:
Suggesting:
#!/bin/bash
docker_container_proxy_name="cloudflared"
real_domain_of_cloudflared="https://([a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60}).trycloudflare.com"
function command {
echo "$(docker logs "${docker_container_proxy_name}$1" 2>&1 | grep -Eo "$real_domain_of_cloudflared" | tail -n 1)"
}
function test {
docker_container_1_uri_outside=$(command 1)
docker_container_2_uri_outside=$(command 2)
docker_container_3_uri_outside=$(command 3)
}
test
CodePudding user response:
Variables are for storing data, not executable code. Generally the best way to store executable code is in a function:
#!/bin/bash
get_container_uri() {
docker_container_proxy_name="cloudflared"
real_domain_of_cloudflared="https://([a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60})(-[a-z0-9]{1,60}).trycloudflare.com"
docker logs "${docker_container_proxy_name}${1}" 2>&1 | grep -Eo "$real_domain_of_cloudflared" | tail -n 1
}
docker_container_1_uri_outside="$(get_container_uri 1)"
docker_container_2_uri_outside="$(get_container_uri 2)"
docker_container_3_uri_outside="$(get_container_uri 3)"
Note that the function just runs the docker logs ...
command directly rather than capturing its output; that way that command's output becomes the function's output, which is exactly what's wanted.
Also, some general scripting recommendations: you should (almost) always put variable references in double-quotes (e.g. grep -Eo "$real_domain_of_cloudflared"
) to keep the shell from parsing them in unexpected ways. The function
keyword is nonstandard; the standard way to define a function is by putting ()
after the name (as I did here). Don't use test
as the name of a function, since that's the name of a rather important built-in command, and overriding it might break other things.
shellcheck.net will point out some of these (and many other common mistakes). I recommend running your scripts through it and fixing what it points out.