Solved

for-loop for creating users in linux

Posted on 2007-12-07
4
263 Views
Last Modified: 2013-12-16
Dear Sir/Madam:

i have to create many users in the linux hence thought of using the for-loop but getting the following error :

# for USER in u1 u2 u3 u4 u5 do useradd $USER echo password | passwd --stdin $USER done

syntax error near unexpected token "|"

for ex:
for your reference i used the special character  |
the one which use for the commands like
getsebool -a | grep ftp





0
Comment
Question by:D_wathi
[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
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 20426803
The script below should work

for USER in u1 u2 u3 u4 u5
do
    useradd $USER
    echo password | passwd --stdin $USER
done

If this script does not work, then please post your script

0
 

Author Comment

by:D_wathi
ID: 20426929
Sir thanks for the reply , following is the error iam getting

[root@parrot ~]# for USER in u1 u2 u3 u4 u5 do useradd $USER echo password | passwd --stdin $USER done
bash: syntax error near unexpected token `|'
[root@parrot ~]#
0
 

Author Comment

by:D_wathi
ID: 20426952
i got it now i did exactly as per the format
[root@parrot ~]# for USER in u1 u2 u3 u4 u5
> do
>    useradd $USER
>    echo password | passwd --stdin $USER
> done
Changing password for user u1.
passwd: all authentication tokens updated successfully.
Changing password for user u2.
passwd: all authentication tokens updated successfully.
Changing password for user u3.
passwd: all authentication tokens updated successfully.
Changing password for user u4.
passwd: all authentication tokens updated successfully.
Changing password for user u5.
passwd: all authentication tokens updated successfully.

thanks for the support

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426960
Are you running the for loop as one line?

try

for USER in u1 u2 u3 u4 u5 ; do ; useradd $USER ; echo password | passwd --stdin $USER ; done
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

705 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