Shell script for creating multiple users with password


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for creating multiple users with password
# 1  
Old 04-11-2016
Shell script for creating multiple users with password

Code:
for UserName in `cat users` ; do useradd -d /u02 -s /usr/libexec/openssh/sftp-server -G ftp-users $UserName ; 
PassWord=$( echo $( tr '[:lower:]' '[:upper:]' <<< ${UserName:0:1} )${UserName:1} ) ; 
echo "$PassWord@123" | passwd $UserName --stdin ; done

can some one explain what the bold text do
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 04-11-2016 at 04:14 PM.. Reason: Add CODE tags.
# 2  
Old 04-11-2016
It sets the variable "PassWord" to the first char of "UserName" translated to upper case followed by the rest of "UserName" in whatever case it may be.
# 3  
Old 04-11-2016
Code:
echo $( tr '[:lower:]' '[:upper:]' <<< ${UserName:0:1} )${UserName:1}

i'm little concerned about "<<<" redirection and :0:1 :1 over there


Moderator's Comments:
Mod Comment Please see moderator's comment in post#1



Moderator's Comments:
Mod Comment To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Last edited by RudiC; 04-11-2016 at 04:26 PM.. Reason: Added code tags (again)
# 4  
Old 04-11-2016
Those are
- Here Strings
- Parameter Substring Expansion

both described in your shell's man page.
# 5  
Old 04-11-2016
Quote:
Originally Posted by James0806
Code:
for UserName in `cat users` ; do useradd -d /u02 -s /usr/libexec/openssh/sftp-server -G ftp-users $UserName ; 
PassWord=$( echo $( tr '[:lower:]' '[:upper:]' <<< ${UserName:0:1} )${UserName:1} ) ; 
echo "$PassWord@123" | passwd $UserName --stdin ; done

can some one explain what the bold text do
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.
The <<< redirection operator and the ${var:start:count} and ${var:start} substring parameter expansions are extensions to the standards that are provided by some, but not all, shells.

The <<< "string" redirection operator feeds the string string followed by a <newline> character to the standard input of the command it follows.

The substring parameter expansion ${var:start[:count]} expands to the count characters (all characters if :count is not specified) starting with the character at the position specified by start (where 0 is the first character in the expansion ${var}). So, ${UserName:0:1} gives you the 1st character of $UserName and ${UserName:1} gives you the remaining characters in $UserName.

The the tr '[:lower:]' '[:upper:]' translates lowercase characters read from standard input to the corresponding uppercase letters on standard output. Characters read from standard input that are not lowercase letters are copied to standard output unchanged.

The outer command substitution using echo to print the output from the inner command substitution and substring parameter expansion is not needed. It could be more succinctly written as:
Code:
PassWord=$( tr '[:lower:]' '[:upper:]' <<< ${UserName:0:1} )${UserName:1}

to assign the variable PassWord the user's login name with the 1st character translated to an uppercase character if it started with a lowercase letter; or to the user's login name (unchanged) if it did not start with a lowercase letter.
# 6  
Old 04-11-2016
That was really help, and nice explanation
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Creating a group of users with script

Hi, I have a file with usernames, and the comment section, e.g : Data removed by request of sanchitadutta91, 20 May 2020 I need to add these users into a server. Is it possible to use a script to create the users, together with the comment ? From the commandline to add one user, the... (2 Replies)
Discussion started by: anaigini45
2 Replies

2. Red Hat

Script for Creating more users like 50 in one server

Hi friends could you please share me the script to create more users in one server except newusers, is there any option to change secondary group and how to print passwd and changing the age all these things how can i edit in newsers.. please help me (0 Replies)
Discussion started by: indiankrish
0 Replies

3. Shell Programming and Scripting

Creating new users using a text file as imput (using only shell script and/or awk)

I need somebody who could help with an exercise. You have a text file called users.txt with this info inside: users.txt: user1:1234:/home/homedir1 ; user2:1234:/home/homedir2 ; user3:1234:/home/homedir3 ; user4:1234:/home/homedir4 ; The script should create an user using the... (2 Replies)
Discussion started by: marcosruiz
2 Replies

4. Shell Programming and Scripting

Writing a script on creating and comparing users

Hello Everybody i am a newbie to the bash scripting.please can someone help me out here.i want to write a script on creating 10 users with passwords and comparing the users to the ones in /etc/passwd file.am i gonna use arrays in creating the 10 users or what?and again, how do i compare both... (1 Reply)
Discussion started by: bruno_brunt
1 Replies

5. UNIX for Dummies Questions & Answers

Script for creating users

Hi, I wants to create the users for multiple machine from single main server using the shell script ,can you please help me on this.... (2 Replies)
Discussion started by: Rahulne25
2 Replies

6. Shell Programming and Scripting

Script in bash wchich creating a new users...

Hi, I am a new on this forum but i like :) I need a script in bash which will be crating a new user with folder for websites. For example: I will run this program and he creating a new user(with my name) and folder whcich name like user and if i will localho/~user in browser, she show me files from... (1 Reply)
Discussion started by: puclavv
1 Replies

7. Shell Programming and Scripting

Shell script to change the password on multiple servers in linux

Can any one please let me know the shell script to change the password for a particular user on multiple linux servers. (2 Replies)
Discussion started by: s_madras
2 Replies

8. Shell Programming and Scripting

creating multiple sub-/directories using a shell script

0 Hi, I am looking for a way of creating multiple directories using the mkdir -p command in a shell script. I'm working with an Ubuntu machine and try to do something like that: #!/bin/sh ... (3 Replies)
Discussion started by: frymor
3 Replies

9. BSD

Creating an array out of users: shell script

I want to create a shell script for a menu selection consisting of users defined on the machine. To an administrator having the privileges, the selection menu will look as follows: Select the user you want to define the variables for: 1) my-username-1 2) my-username-2 etc Then there would be a... (7 Replies)
Discussion started by: figaro
7 Replies

10. Shell Programming and Scripting

Help with shell script - creating users

echo -e "Enter in a username : \c" read username grep "^$username:" /etc/passwdWhat I'm trying to do is take in a username from my script and I need to be able to check if that username already exists. If it does the script should display a message saying that the user already exists and exit. ... (2 Replies)
Discussion started by: shadowcat
2 Replies
Login or Register to Ask a Question