Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a text file that is formatted like this
LDAP ad-group1
LDAP ad-group2
LDAP ad-group3
... etc
I am trying to run this command against all the lines of the text file using this:
Code:
for name in $(cat /tmp/adgroup_file.txt); do mon query ls hosts -c name contact_groups -ct $name; done > output.txt
Nothing is being sent to the output.txt file it is just blank. I tried to add '' around the command 'mon query ls host -c name contact_groups -ct' $name but then I get
-bash: mon query ls hosts -c name contact_groups -ct: command not found
If I manually run
Code:
mon query ls hosts -c name contact_groups -ct 'LDAP ad_group'
I get the information I want. Just trying to automate this a bit as I have over 100 AD groups I need to query.
Ideally it would be nice to have the export of each AD group to have a contact_groups listing in a separate file but just trying to get the basics working first.
Last edited by bkone; 08-17-2021 at 10:18 AM.
Reason: Adding CODE tags
The shell does word-splitting and filename generation here.
The word splitting splits by $IFS that normally is space or tab or newline, and $name gets
LDAP
ad-group1
LDAP
ad-group2
You can split on only newline with
Code:
IFS="
"
set -f # also disable filename generation
for name in $(cat /tmp/adgroup_file.txt); do mon query ls hosts -c name contact_groups -ct $name; done > output.txt
Also this prevents a further word-splitting and filename generation on the $name. Of course quotes can easily prevent this: "$name"
But I recommend a "while read" loop, because the read command defaults to a newline
Code:
while read name; do mon query ls hosts -c name contact_groups -ct "$name"; done < /tmp/adgroup_file.txt > output.txt
The shell does word-splitting and filename generation here.
The word splitting splits by $IFS that normally is space or tab or newline, and $name gets
LDAP
ad-group1
LDAP
ad-group2
You can split on only newline with
Code:
IFS="
"
set -f # also disable filename generation
for name in $(cat /tmp/adgroup_file.txt); do mon query ls hosts -c name contact_groups -ct $name; done > output.txt
Also this prevents a further word-splitting and filename generation on the $name. Of course quotes can easily prevent this: "$name"
But I recommend a "while read" loop, because the read command defaults to a newline
Code:
while read name; do mon query ls hosts -c name contact_groups -ct "$name"; done < /tmp/adgroup_file.txt > output.txt
This provided me with what I was needing the only problem I have with the output is
it the data doesn't tell me what AD group the output belongs to. I was going to try to add something like echo ----"$name"-----; before the done to see if that would at least add the AD group and then under it display the command output so I can easily review it.
I tried using what the other member suggested with breaking out the files so each AD group would have a separate file that listed the output within the individual files but after running nothing was presented.
I tried using what the other member suggested with breaking out the files so each AD group would have a separate file that listed the output within the individual files but after running nothing was presented.
Unfortunately I have no way of actually testing the script so you would need to be to do some rudimentary debugging as suggested by chrism01. About the only obvious syntax error would be to add quotes around
Code:
/some/where/"$name"_output.txt
to prevent word splitting. You did not indicate there were any errors when you tried my script so it is difficult for me to fix. I assume that you realized that /some/where is not an exact path and would you would see an error message...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.