?
Solved

Bash script looping two source files in a (for loop)

Posted on 2013-05-28
8
Medium Priority
?
485 Views
Last Modified: 2013-05-28
I need some help adding two source files to a for loop, below is what I have, essentially I'm using my hosts file to source the IP I need to connect to, I than have a file with a list of users I need to add in each of the nodes on the hosts file.


# Obtain IP from hosts file
NODES=`cat /etc/hosts | grep S****$1 |cut -f2 -d ' '`
USERS=`cat users.txt`
ERRLOG=send_USERS_err.log
RUNLOG=send_UDERS_run.log


#Perform a ping test to each IP validating system is up

for i in $NODES
do
      ping -c 1 $i >> /dev/null
if [ $? != 0 ]; then
      echo " $i is currently DOWN " >> $ERRLOG
else
ssh $i useradd -d /home/$USER -p salSp1wOPp6fk $USER
0
Comment
Question by:atorex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39201293
Like this?

NODES=`cat /etc/hosts | grep S****$1 |cut -f2 -d ' '`
USERS=`cat users.txt`
ERRLOG=send_USERS_err.log

for i in $NODES
do
      ping -c 1 $i >> /dev/null
if [ $? -ne 0 ]; then
      echo " $i is currently DOWN " >> $ERRLOG
else
for USER in $USERS
  do
ssh $i useradd -d /home/$USER -p salSp1wOPp6fk $USER
  done
fi
done
0
 

Author Comment

by:atorex
ID: 39201401
That's what I was thinking but it errors out with

useradd: Too many arguments.
Try `useradd --help' or `useradd --usage' for more information.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39201407
What's in "users.txt"?

Please note: it's

for USER in $USERS

not

for USER in "$USERS"

You can add

set -xv

just above the "ssh $i ..." line, to see what happens.
0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 

Author Comment

by:atorex
ID: 39201488
yup I have for USER in $USERS

what its doing is adding all users in the file in one command see below

+ set -xv
+ ssh 172.28.78.186 useradd -d /home/kham christab kcash rbryant kdelley janas beths charlie sboyle jcassel vcoleman vnajera slozano cshelton -p salSp1wOPp6fk kham christab kcash rbryant kdelley janas beths charlie sboyle jcassel vcoleman vnajera slozano cshelton
0
 

Author Comment

by:atorex
ID: 39201491
if I pass
ssh $i useradd -d /home/kham -p salSp1wOPp6fk kham
this works fine!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39201529
This can only happen if the usernames in users.txt are not correctly separated.

Again:

What's exactly in users.txt?

cat -v users.txt
0
 

Author Comment

by:atorex
ID: 39201801
E420S aga # cat -v users.txt
kham
christab
kcash
rbryant
kdelley
janas
beths
charlie
sboyle
jcassel
vcoleman
vnajera
slozano
cshelton
0
 

Author Closing Comment

by:atorex
ID: 39201835
Sorry, I have found the issue, I had a missing s in $users.

what you provide works perfectly my apologies for the typo!!
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question