Link to home
Start Free TrialLog in
Avatar of Goutham
GouthamFlag for India

asked on

for-loop for creating users in linux

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





ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Goutham

ASKER

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 ~]#
Avatar of Goutham

ASKER

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

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