1

I have a simple ldapsearch bash script to return the user email when searched by ID. I made it take and argument as its input since at the time I only needed to run it once or twice.

I'm wondering can I adapt it and take input from a file like .txt and append the outputs to another file.

This is what i have:

#!/bin/bash

if [ "$1" = "" ]; then
    echo "how to: searchID.sh <userID>"
    exit 1
fi
ldapsearch -x -b '' -LLL -h ldaphost.com -p 255 uid=$1 mail >> outputs.txt

Instead of running it manually like: ./searchID.sh I0FT45

I want it to take input from a file with many ID's like:

I0001F
IGLFK7
I37462
I4593N

And run it for all those entries. Any help is very much appreciated

looktfiu
  • 13
  • 3
  • See [How can I read a file/stream line-by-line](https://mywiki.wooledge.org/BashFAQ/001) – Jetchisel Sep 22 '22 at 10:24
  • Maybe this one, https://stackoverflow.com/questions/73668255/bash-looping-through-lines-in-a-file-and-using-the-index-of-each-line-enumera/73668345#73668345 – Jetchisel Sep 22 '22 at 10:25

1 Answers1

0

if your usernames are xargs "safe" (no space, no quote) then you can do something like this:

xargs -I {} \
    ldapsearch -x -b '' -LLL -h ldaphost.com -p 255 uid={} mail \
    < file.in \
    >> file.out
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • Thanks, this seems to work. Is there any way to output the whole ldap result, inlcuding the ` Search succeeded. Found 1 Entries ` when there is a match and ` Search succeeded. Found 0 Entries ` when there isn't? This is only showing on the command line. The file is only getting the ldap info output – looktfiu Sep 22 '22 at 12:20
  • @looktfiu remove the `-LLL` option of `ldapsearch` – Fravadona Sep 22 '22 at 12:49