Problem running Linux Bash Script and adding password same as username

Clash Royale CLAN TAG#URR8PPP up vote
0
down vote
favorite
With the following script I'm trying to create users,groups and password from a txt/cvs file.
Here is the script:
#!/bin/bash
set -xv
filein="file.csv"
IFS=$'n'
if [ ! -f "$filein" ]
then
echo "Cannot find file $filein"
else
groups=$(cut -d: -f 6 "$filein" | sed 's/ //')
fullnames=$(cut -d: -f 1 "$filein")
usernames=$(cut -d: -f 1 "$filein" | tr '[:upper:]' '[:lower:]' | awk 'print substr($1,1,1) $2')
fi
for group in $groups[*]
do
grep -q "^$group" /etc/group ; (( x=$? ))
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
x=0
created=0
for user in $usernames[*]
do
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
if [ $? -eq 0 ]
then
(( created=created+1 ))
fi
done
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."
This morning I used shellchek on my script and I modify some of the code but I still get 2 problems.
One of the problem is that I'm getting an error from shellcheck saying:
In test.sh line 29:
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.
The other problem I'm getting is when I'm trying to create passwords for the usernames( same as the username).
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
Ubuntu doesn't find the command --stdin and It's been a while that Im looking for a solution but without result.
I'm just starting creating shell scripts, If you guys can please help me running this script would be amazing.
Thanks in advance.
command-line bash scripts password
 |Â
show 2 more comments
up vote
0
down vote
favorite
With the following script I'm trying to create users,groups and password from a txt/cvs file.
Here is the script:
#!/bin/bash
set -xv
filein="file.csv"
IFS=$'n'
if [ ! -f "$filein" ]
then
echo "Cannot find file $filein"
else
groups=$(cut -d: -f 6 "$filein" | sed 's/ //')
fullnames=$(cut -d: -f 1 "$filein")
usernames=$(cut -d: -f 1 "$filein" | tr '[:upper:]' '[:lower:]' | awk 'print substr($1,1,1) $2')
fi
for group in $groups[*]
do
grep -q "^$group" /etc/group ; (( x=$? ))
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
x=0
created=0
for user in $usernames[*]
do
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
if [ $? -eq 0 ]
then
(( created=created+1 ))
fi
done
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."
This morning I used shellchek on my script and I modify some of the code but I still get 2 problems.
One of the problem is that I'm getting an error from shellcheck saying:
In test.sh line 29:
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.
The other problem I'm getting is when I'm trying to create passwords for the usernames( same as the username).
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
Ubuntu doesn't find the command --stdin and It's been a while that Im looking for a solution but without result.
I'm just starting creating shell scripts, If you guys can please help me running this script would be amazing.
Thanks in advance.
command-line bash scripts password
1
Shellcheck is telling you to do the same thing forfullnamesanduserthat you did forgroupsi.e."$fullnames[$x]"and"$user". As for the other error, AFAIKpasswdcan only be used interactively - however you may be able to do what you want usingchpasswd
â steeldriver
May 25 at 0:24
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
You just supply colon-separatedusername:passwordpairs to its standard input - see for example How to set user passwords using passwd without a prompt?
â steeldriver
May 25 at 0:43
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
No, if you already have the values in shell variables you can use"$user:$password"for example. However I can't follow your code logic (what is$xfor example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a singlewhileloop. You could also set the password directly in theuseraddcommand provided you encrypt it.
â steeldriver
May 25 at 1:58
 |Â
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
With the following script I'm trying to create users,groups and password from a txt/cvs file.
Here is the script:
#!/bin/bash
set -xv
filein="file.csv"
IFS=$'n'
if [ ! -f "$filein" ]
then
echo "Cannot find file $filein"
else
groups=$(cut -d: -f 6 "$filein" | sed 's/ //')
fullnames=$(cut -d: -f 1 "$filein")
usernames=$(cut -d: -f 1 "$filein" | tr '[:upper:]' '[:lower:]' | awk 'print substr($1,1,1) $2')
fi
for group in $groups[*]
do
grep -q "^$group" /etc/group ; (( x=$? ))
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
x=0
created=0
for user in $usernames[*]
do
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
if [ $? -eq 0 ]
then
(( created=created+1 ))
fi
done
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."
This morning I used shellchek on my script and I modify some of the code but I still get 2 problems.
One of the problem is that I'm getting an error from shellcheck saying:
In test.sh line 29:
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.
The other problem I'm getting is when I'm trying to create passwords for the usernames( same as the username).
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
Ubuntu doesn't find the command --stdin and It's been a while that Im looking for a solution but without result.
I'm just starting creating shell scripts, If you guys can please help me running this script would be amazing.
Thanks in advance.
command-line bash scripts password
With the following script I'm trying to create users,groups and password from a txt/cvs file.
Here is the script:
#!/bin/bash
set -xv
filein="file.csv"
IFS=$'n'
if [ ! -f "$filein" ]
then
echo "Cannot find file $filein"
else
groups=$(cut -d: -f 6 "$filein" | sed 's/ //')
fullnames=$(cut -d: -f 1 "$filein")
usernames=$(cut -d: -f 1 "$filein" | tr '[:upper:]' '[:lower:]' | awk 'print substr($1,1,1) $2')
fi
for group in $groups[*]
do
grep -q "^$group" /etc/group ; (( x=$? ))
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
x=0
created=0
for user in $usernames[*]
do
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
if [ $? -eq 0 ]
then
(( created=created+1 ))
fi
done
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."
This morning I used shellchek on my script and I modify some of the code but I still get 2 problems.
One of the problem is that I'm getting an error from shellcheck saying:
In test.sh line 29:
useradd -n -c $fullnames[$x] -g "$groups[$x]" $user 2> /dev/null
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.
The other problem I'm getting is when I'm trying to create passwords for the usernames( same as the username).
echo "$usernames[$x]" | passwd --stdin "$user" > /dev/null
Ubuntu doesn't find the command --stdin and It's been a while that Im looking for a solution but without result.
I'm just starting creating shell scripts, If you guys can please help me running this script would be amazing.
Thanks in advance.
command-line bash scripts password
edited May 25 at 0:30
asked May 25 at 0:02
Elio Basciani
183
183
1
Shellcheck is telling you to do the same thing forfullnamesanduserthat you did forgroupsi.e."$fullnames[$x]"and"$user". As for the other error, AFAIKpasswdcan only be used interactively - however you may be able to do what you want usingchpasswd
â steeldriver
May 25 at 0:24
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
You just supply colon-separatedusername:passwordpairs to its standard input - see for example How to set user passwords using passwd without a prompt?
â steeldriver
May 25 at 0:43
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
No, if you already have the values in shell variables you can use"$user:$password"for example. However I can't follow your code logic (what is$xfor example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a singlewhileloop. You could also set the password directly in theuseraddcommand provided you encrypt it.
â steeldriver
May 25 at 1:58
 |Â
show 2 more comments
1
Shellcheck is telling you to do the same thing forfullnamesanduserthat you did forgroupsi.e."$fullnames[$x]"and"$user". As for the other error, AFAIKpasswdcan only be used interactively - however you may be able to do what you want usingchpasswd
â steeldriver
May 25 at 0:24
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
You just supply colon-separatedusername:passwordpairs to its standard input - see for example How to set user passwords using passwd without a prompt?
â steeldriver
May 25 at 0:43
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
No, if you already have the values in shell variables you can use"$user:$password"for example. However I can't follow your code logic (what is$xfor example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a singlewhileloop. You could also set the password directly in theuseraddcommand provided you encrypt it.
â steeldriver
May 25 at 1:58
1
1
Shellcheck is telling you to do the same thing for
fullnames and user that you did for groups i.e. "$fullnames[$x]" and "$user". As for the other error, AFAIK passwd can only be used interactively - however you may be able to do what you want using chpasswdâ steeldriver
May 25 at 0:24
Shellcheck is telling you to do the same thing for
fullnames and user that you did for groups i.e. "$fullnames[$x]" and "$user". As for the other error, AFAIK passwd can only be used interactively - however you may be able to do what you want using chpasswdâ steeldriver
May 25 at 0:24
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
You just supply colon-separated
username:password pairs to its standard input - see for example How to set user passwords using passwd without a prompt?â steeldriver
May 25 at 0:43
You just supply colon-separated
username:password pairs to its standard input - see for example How to set user passwords using passwd without a prompt?â steeldriver
May 25 at 0:43
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
No, if you already have the values in shell variables you can use
"$user:$password" for example. However I can't follow your code logic (what is $x for example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a single while loop. You could also set the password directly in the useradd command provided you encrypt it.â steeldriver
May 25 at 1:58
No, if you already have the values in shell variables you can use
"$user:$password" for example. However I can't follow your code logic (what is $x for example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a single while loop. You could also set the password directly in the useradd command provided you encrypt it.â steeldriver
May 25 at 1:58
 |Â
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1040029%2fproblem-running-linux-bash-script-and-adding-password-same-as-username%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Shellcheck is telling you to do the same thing for
fullnamesanduserthat you did forgroupsi.e."$fullnames[$x]"and"$user". As for the other error, AFAIKpasswdcan only be used interactively - however you may be able to do what you want usingchpasswdâ steeldriver
May 25 at 0:24
Got it Thanks a lot, how it would be using chpassw? I couldn't understand how to canalize it reading my usernames
â Elio Basciani
May 25 at 0:33
You just supply colon-separated
username:passwordpairs to its standard input - see for example How to set user passwords using passwd without a prompt?â steeldriver
May 25 at 0:43
Do I have to write manually the pairs in my file.cvs?
â Elio Basciani
May 25 at 1:46
No, if you already have the values in shell variables you can use
"$user:$password"for example. However I can't follow your code logic (what is$xfor example?). I suspect you are hugely over-complicating things - likely you can read all the information you need line-by-line (i.e. user-by-user) direct from the file in a singlewhileloop. You could also set the password directly in theuseraddcommand provided you encrypt it.â steeldriver
May 25 at 1:58