LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-17-2021, 09:59 AM   #1
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Rep: Reputation: 15
Bash script assistance for loop


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
 
Old 08-17-2021, 10:14 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,121

Rep: Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371
would be nice to use code tags, that makes your post more readable. https://www.linuxquestions.org/quest...do=bbcode#code
also you can use shellcheck to find a lot of different issues in shell scripts like this
 
Old 08-17-2021, 10:20 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,382
Blog Entries: 3

Rep: Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773
Perhaps it is a matter of quoting so the space stays within the option:

Code:
... -ct "$name" ...
Double quotes make sure that the variable $name is expanded before getting passed to the program.
 
Old 08-17-2021, 11:54 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,844

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
Code:
for name in $(cat /tmp/adgroup_file.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
 
2 members found this post helpful.
Old 08-17-2021, 09:06 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,822

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
for a separate file for each group.
Code:
while read -r name
do
  mon query ls hosts -c name contact_groups -ct "$name" > /some/where/$name_output.txt
done < /tmp/adgroup_file.txt

Last edited by michaelk; 08-17-2021 at 09:09 PM.
 
Old 08-18-2021, 01:42 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,375

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
Another debugging trick: add
Code:
set -xv
before the first line of your script.
(& do use multiple code lines instead of packing it all onto one - much easier to read/debug)
 
Old 08-19-2021, 07:08 AM   #7
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by MadeInGermany View Post
Code:
for name in $(cat /tmp/adgroup_file.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.
 
Old 08-19-2021, 08:30 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,822

Rep: Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960Reputation: 5960
Quote:
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...
 
Old 08-20-2021, 12:12 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,844

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
You want to print the group name?
One way is
Code:
while read name; do echo ----"$name"-----; mon query ls hosts -c name contact_groups -ct "$name"; done < /tmp/adgroup_file.txt > output.txt
A multi-line script is better readable:
Code:
while read name
do
  echo "----$name-----"
  mon query ls hosts -c name contact_groups -ct "$name"
done < /tmp/adgroup_file.txt > output.txt
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
BASH assistance - loop/counter rickenbacherus Programming 6 03-12-2007 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration