Thursday, October 27, 2022

[SOLVED] How to change username via Shell Script

Issue

I'm doing a script to change the name of a user just using variables so the user doesn't see the actual command. I have done other things like change the folder of the user but for some reason tryng the same method with this isn't working, I hope you understand my mistakes and give me a hand.

echo "Give me the old username"
read name
echo "Give me the new username"
read new
echo "$new" | usermod -l --stdin "$name"

For some reason stdin isn't working :C I'm getting the next output usermod: invalid user name '--stdin'.

Note: I have used stdin to get the new names before and it worked perfectly just this way so I don't know what's wrong.


Solution

I don't know any usermod that knows --stdin ... try this:

echo "Give me the old username"
read name
echo "Give me the new username"
read new
usermod -l "$new" "$name"


Answered By - tink
Answer Checked By - Clifford M. (WPSolving Volunteer)