skip to Main Content

I have a command lando info which has a fairly large multi-line output:

[ { service: 'appserver',
    urls:
     [ 'https://localhost:52836',
       'http://localhost:52837',
       'http://if-build-d9.lndo.site/',
       'https://if-build-d9.lndo.site/' ],
    type: 'php',
    healthy: true,
    via: 'apache',
    webroot: './web',
    config: { php: '/Users/runo/.lando/config/drupal9/php.ini' },
    version: '7.3',
    meUser: 'www-data',
    hasCerts: true,
    hostnames: [ 'appserver.ifbuildd9.internal' ] },
  { service: 'database',
    urls: [],
    type: 'mysql',
    healthy: true,
    internal_connection: { host: 'database', port: '3306' },
    external_connection: { host: '127.0.0.1', port: '52835' },
    healthcheck: 'bash -c "[ -f /bitnami/mysql/.mysql_initialized ]"',
    creds: { database: 'drupal9', password: 'drupal9', user: 'drupal9' },
    config: { database: '/Users/runo/.lando/config/drupal9/mysql.cnf' },
    version: '5.7',
    meUser: 'www-data',
    hasCerts: false,
    hostnames: [ 'database.ifbuildd9.internal' ] },
  { service: 'redis_primary',
    urls: [ 'http://if-build-d9-redis-primary.lndo.site/' ],
    type: 'redis',
    healthy: true,
    internal_connection: { host: 'redis_primary', port: '6379' },
    external_connection: { host: '127.0.0.1', port: '52838' },
    config: {},
    version: '6',
    meUser: 'www-data',
    hasCerts: false,
    hostnames: [ 'redis_primary.ifbuildd9.internal' ] },
  { service: 'mailhog',
    urls: [ 'http://localhost:52840', 'http://if-build-d9-mail.lndo.site/' ],
    type: 'mailhog',
    healthy: true,
    hogfrom: [ 'appserver' ],
    internal_connection: { host: 'mailhog', port: '1025' },
    external_connection: { host: '127.0.0.1', port: '52839' },
    config: {},
    version: 'v1.0.0',
    meUser: 'mailhog',
    hasCerts: false,
    hostnames: [ 'mailhog.ifbuildd9.internal' ] } ]

I want to capture the value http://if-build-d9.lndo.site in a bash variable.

What I have is this shell script, but it returns empty values:

 lando_info=$(lando info); 
 regex_pattern='/http://[wS]*.lndo.site/g'; 
 [[ "$lando_info" =~ $regex_pattern ]]; 
 echo "${BASH_REMATCH[0]}"
 echo "${BASH_REMATCH[1]}"
 echo "${BASH_REMATCH[2]}"

This outputs empty strings.

I know the regex itself should be valid as I tested it on https://www.regexpal.com/ with that command output and pattern.

It matches on

  • http://if-build-d9.lndo.site (this is the only match I care about)
  • http://if-build-d9-redis-primary.lndo.site
  • http://if-build-d9-mail.lndo.site

So it appears that my use of regex in Bash is not correct.

2

Answers


  1. Chosen as BEST ANSWER

    I ended up using jq with better results.


  2. Bash uses POSIX ERE regex flavor, [wS] matches a , w or S and not any char but whitespace as you expected.

    It looks like you just want to grep -out the URLs with a specific pattern, so use

    grep -oE 'http://[^[:space:]]*.lndo.site' <<< "$lando_info"
    

    See the online demo. Output:

    http://if-build-d9.lndo.site
    http://if-build-d9-redis-primary.lndo.site
    http://if-build-d9-mail.lndo.site
    

    The [^[:space:]]* is a negated bracket expression that contains a POSIX character class and matches any zero or more chars other than whitespace chars. -o option allows grep to extract matches only, not matched lines.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search