Problem running Linux Bash Script and adding password same as username

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question


















  • 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










  • 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











  • 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














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.







share|improve this question


















  • 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










  • 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











  • 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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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 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










  • 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










  • 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












  • 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










  • 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











  • 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







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















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Trouble downloading packages list due to a “Hash sum mismatch” error

How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

How do I move numbers in filenames, in a batch renaming operation?