Search code examples
bashunixbash-function

Bash function with formatting


I'm using the following in my .bashrc file as a function to grep info from an external LDAP but would love for it to output a couple of values, but each on their own line

function ldaps() { ldapsearch -x -H ldaps://ldap-server.example.com -b ou=People,dc=exampe,dc=com uid=$1 | grep uidNumber: ; }

Ideally, it'd output something like:

% ldaps jsixpack
uidNumber: 9255
loginShell: /bin/bash
displayName: Joe Sixpack

Stuff like that.

Ideas, suggestions appreciated!


Solution

  • Use the -E flag with grep for extended regex:

    function ldaps() {
      ldapsearch -x -H ldaps://ldap-server.example.com -b\
      ou=People,dc=example,dc=com uid=$1 |
      grep -E '(uidNumber|displayName|loginShell):'
    }
    

    This will return matches of either uidNumber, displayName or loginShell each that are followed by a :.

    Hope this helps