How to change username via Shell Script - linux

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.

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"

Related

How do -s and -p alter the read command?

I'm trying to interpret this block of code. Searched google to see what these commands mean and no luck. I put my interpretation of what each line/block means to me. If I am wrong, please correct me. I am new to unix commands. Code:
#!/bin/bash
# input 1st command line argument for the version.
export VERSION=$1
# if user didn't input a version, print the echo message and exit (not sure what -n means but I am assuming)
if [[ ! -n "$VERSION" ]]; then
echo "Missing Version"
exit 1
fi
# creating variable UNAME that tells who the person is (their name)
export UNAME='whoami'
# no idea what -s and -p mean but i think this prints the message "enter password for $UNAME" and stores it in a new variable named PASSWORD. the $UNAME will print whatever whoami said.
read -s -p "Enter password for $UNAME: " PASSWORD
echo ""
The -p flag issues a prompt before reading input into a variable
The -s flag stop the typed response from being shown (i.e. for a sensitive password)
More information is available here:
https://linuxhint.com/bash_read_command/
-p
prompt output the string PROMPT without a trailing newline before
attempting to read.
-s
do not echo input coming from a terminal.

How do I search for a certain piece of text inside of a variable?

I am working on a script which prompts the user for their username. Once entered, the script uses the 'rwho' command to get a list of users who are logged into the network. It should crosscheck the text they entered (their username) with the results from the rwho command.
If a match is found then it displays a message saying so, if not then it also makes the user aware of this.
Here is the script and my attempt so far:
#!/bin/sh
#
# User network checking script
#
# Using rwho command to get user list
OUTPUT="$(rwho)"
echo "${OUTPUT}"
# Prompt for username
echo "Please enter your username: "
read username
# Input validation
if [ -z "$username"]
then
echo "No username supplied"
echo "Please enter your username: "
read username
fi
# Search for user
if `echo ${OUTPUT} | grep "${username}" 1>/dev/null 2>&1'
then
echo "$username is logged in."
else
echo "$username is not present."
fi
I consistently get errors with the Search for User part. I don't have outstanding knowledge of Linux so if anyone could fix this and help me I would be greatly appreciative.
Your usage of quotes is weird.
if echo "$OUTPUT" | grep -q "$username"
should work.
-q makes grep quiet (and is shorter than your redirections).

Linux script groupadd

I have a groupadd script:
#/bin/bash
echo -n "Enter new group name: "
read group
if egrep "^$group" /etc/group; then
cut -d: -f1 /etc/group
echo "!!Group $group already exists!!"
echo -n "Enter different group name: "
read name
groupadd $name
echo "Group $name was created."
else
groupadd $group
echo "Group $group was crated."
fi
This script works perfectly fine. But I run into small problem which I have trouble to figure it out how to deal with that problem.
The problem starts when I'm entering Hello and there is already group Hello. It says group Hello already exists. The line Enter different group name pops and I again enter Hello. It shows this which bothers me because I can't deal with it:
groupadd: group "Hello" already exists
Group Hello was created.
But I want to do another group check loop when entering for second time Hello and not ending the script like in the example above.
So if there will be someone to show me how to deal with this I'll be happy :)
Thanks :)
First of all you have to use getent group $group > /dev/null 2>&1 instead of manual grepping. Second, you may do a while loop like while read x; do; … ; done.

String Bash scripting if then statement fails

I am currently writing a script that will allow me to add groups via user input. I am on the portion of my script where the user types the group name in and it compares it against /etc/group and lets the user know if it needs to be added or not. I have tested this against a group that I know for a fact is not on my system and it only reads the first statement in my loop. Could someone tell me where I am going wrong?
#!/bin/bash
echo "This script will allow you to enter Groups and Users needed for new builds"
echo
echo
echo
echo
# Setting Variables for Group Section
Group=`cat /etc/group |grep "$group"`
echo -n "Please enter the group name that you would like to search for..press [ENTER] when done: " # Request User input to obtain group name
read group
echo "Searching /etc/group to see if the group "$group" exists." # Checking to see if the group exists
if [ "$group" != "$Group" ]; then
echo "The group already exist. Nothing more to do buddy."
else
echo "We gotta add this one fella..carry on."
If you're on Linux, and thus have getent available:
printf "Group to search for: "
read -r group
if getent group "$group" >/dev/null 2>&1; then
echo "$group exists"
else
echo "$group does not exist"
fi
Using getent uses the standard C library for directory lookups. Thus, it's good for not only /etc/passwd, /etc/group, etc., but also directory services such as Active Directory, LDAP, NIS, YP and the like.
Here's what you do:
Search for a group name
Input the group name to search for
Sadly, you can't search for the group name before you input it, as this would violate causality and the laws of spacetime as we know them. Try searching after you know what you search for instead:
echo -n "Please enter the group name that you would like to search for..press [ENTER] when done: " # Request User input to obtain group name
read group
if cat /etc/group | grep -q "^$group:"
then
echo "The group already exist. Nothing more to do buddy."
fi

Something wrong with my sh script dont know why?

I wrote this sh script here. What it suppose to be doing is it prompts the user to type in the old password, then checks the password with the password in the "PASSWORD.txt" file if not it would exit, else if it matches then it would ask the user to type in the new password twice. Then it checks if the two new passwords are the same if not it would exit, else i should put the input the user typed and replace the text in the "PASSWORD.txt" file.
Then when i ran the file where it askes me for the old password i got this error:
Please Enter teh old passsword:
test
cat: .txt: No such file or directory
The password doesn't match![root#guzzy ~]#
The thing is the input i typed doesn't match even though i typed the correct old password.
Here is the scirpt below:
#!/bin/sh
clear
echo -e "Please Enter the old password:"
read old
if [ "$old" != "$(cat $PASSWORD.txt)" ]
then
echo -n "The password doesn't match!"
exit
else
echo -n "The old password matches!"
echo -n "Please Enter New password:"
read new1
echo -n "Please Enter New password again:"
read new2
if [ "$new1" != "$new2" ]
then
echo -n "The new passwords don't match!"
exit
else
$new1 >> PASSWORD.txt
echo -n "The new password has been saved!"
fi
fi
Please help thanks!
This line:
$new1 >> PASSWORD.txt
should be like this:
echo "$new1" > PASSWORD.txt
You need to echo the value into the file. I'm assuming that you don't want to keep old values. In order to be able to run your script again on the same file, you should probably overwrite (>) rather than append (>>).
You haven't set the PASSWORD variable somewhere. If your file is named 'PASSWORD.txt', remove the $ before it.
It think you meant
if [ "$old" != "$(cat PASSWORD.txt)" ]
without the dollar sign.

Resources