bash - Find a port in a configuration file with regex -
in configuration file (nginx default.conf) got strings this:
server { listen 80; server_name sudomain1.somewhere.com ; location / { proxy_set_header host sudomain1.somewhere.com; proxy_pass http://127.0.0.1:8999/; } }
in bash script, catch port number "8999" in variable use after in script.
if possible using entire string above.
because in file have several times sequence, , variable mark "subdomain1"
does know how this?
you can single line:
( ln=$(grep -a 1 sudomain1 nginxconf.txt | tail -n1); port=${ln##*:}; port=${port%/*}; echo "port: $port" )
or script:
#!/bin/bash ln=$(grep -a 1 sudomain1 nginxconf.txt | tail -n1) port=${ln##*:} port=${port%/*} echo "port: $port"
you first locate sudomain1
, line follows using grep -a 1
use tail -n
keep last line. simple parameter matching/substring extraction isolate 8999
.
output
port: 8999
note: spell subdomain1
sudomain1
in conf file.
Comments
Post a Comment