Creating Users from a text file using Bash - linux

I've been looking at some of the other answers for this question but it hasn't solved my issue.
Basically, I need to create a script that reads from a text file, that is given by a user input, and creates users from the contents of the file. I've managed to get it to create the first user but it doesn't seem to be creating the other users in the file.
My text document is literally:
user1
user2
user3
And heres the code I have:
echo -n "Enter name of text file "; read text
while read USER;
do
USERNAME=$(cut -d$'\n' -f $text)
echo $USERNAME
useradd -m "${USERNAME}"
done < $text
It seems to only be reading the very first entry in the text file but I thought using the \n would mean it cut the other lines and use them next? I tried using the 'cat' command instead but wasn't having much luck with it and this is the furthest I've managed to get but I was hoping someone would help me find where I've gone wrong.
Thanks :)

First, you should start your script with
#!/usr/bin/env bash
to ensure that it is interpreted as bash.
Second, the value you need is already available in the USER variable. There is no need to use cut.
#!/usr/bin/env bash
echo -n "Enter name of text file: "; read FILENAME
while read USER; do
echo "$USER"
useradd -m "${USER}"
done < "${FILENAME}"

You can use awk to print and pipe to shell
$ awk '{print "useradd -m "$1}' <file> | sh

Related

Use text file as input? What am I doing wrong?

Hello I am trying to create multiple users from a text file
Here:
tony romo
drew brees
laura smith
bob jones
joe hones
kelly clarkson
hank hill
michael scott
and to do that I am using the script
while read first last; do
name="$first $last"
username="$last${first:0:1}"
n=‘egrep –c $username /etc/passwd‘
n=$((n + 1))
username=$username$n
password=‘tr –cd '[:alpha:]' < /dev/urandom | head –c8‘
echo $password | passwd --stdin $username
echo “$username $password” >> /root/tempPasswords
useradd –c “$name” –m $username
done
I then execute the commmand
./name_of_script accounts.txt
Yet nothing happens, any idea why? Thanks for any help!
EDIT
I made the following changes and now I get the useradd command errors any idea why?
I made the changes but now there are errors of the useradd command any reason why?
#!/bin/bash
while read first last; do
name="$first $last"
username="$last${first:0:1}"
n=`egrep –c $username /etc/passwd`
n=$((n + 1))
username=$username$n
password=`tr –cd `[:alpha:]` < /dev/urandom | head –c8`
echo $password | passwd --stdin $username
echo “$username $password” >> /root/tempPasswords
useradd –c "$name" –m $username
done < "$1"
Your quotes are hosed. Where you have a curly quote, you should have a backtick (ASCII 96) which however in this millennium is better written with the modern syntax $(command).
With that out of the way, just add < "$1" after the done to read the first argument as the input for the while read loop.
The script you wrote reads from stdin. Nothing happens because it's waiting for you to type the names out. You can either run your script with a redirection:
./name_of_script < accounts.txt
Or change the script to read from the file on its command-line, $1. To do that, add the redirection to the loop like so:
while read first last; do
...
done < "$1"
You'll also want to get rid of the smart quotes. ‘ should be ` (backtick) and “...” should be "..." (plain double quotes). Avoid editing code in a fancy word processor like MS Word. You'll want to stick to plain text editors like Vim or Notepad++, ones designed to edit code rather than documents.

How to use select with awk in bash script?

I have to write a bash script for university, the text says:
Write a bash script that allows root user, to get a list of all users
of the machine. Selecting a user, using select, will be required to
indicate a directory (indicate the absolute path). At this point in
the output will have be shown a list of all files folder owned by the
user, ranked in ascending order according to the size of file.
To check if the user is root i used:
if[ "$(id -u)" = 0 ]; then
To get the list of users of the machine I was thinking of using awk:
awk -F':' '{ print$1}' /etc/passwd
How can I use select with awk?
Is there another way without using awk?
Thank you so much in advance
Here is the way to use awk in select statement, you need finish the rest for your homework (for example, sort the result)
#!/usr/bin/env bash
select user in $(awk -F ":" '{print $1}' /etc/passwd )
do
read -p "input the absolute directory: " path
find $path -type f -user "$user" -ls
done
Another way to test the UID by arithmetic (smarter!?) is :
if((UID==0)); then
...
else
...
fi
Check http://wiki.bash-hackers.org/syntax/arith_expr

Replacing a string with a given text using Linux shell script

I have a file named testfile.It contains some information like
methun:x:500:500:comment:/home/methun:bin/bash
salahuddin:x:501:500:comment:/home/methun:bin/bash
Now implemented a following shell program:
echo "Enter a Name:"
read username
users='cat /mypractice/myfiles/testfile | awk -F ':' '{print $1}''
for user in $users
do
if [ "$user" == "$username" ]; then
echo "Name found and Enter a new name to change."
read newUsername
#need code to change text on my file --->testfile
fi
done
Now suppose I need to change methun to Moin. Comment to newcomment. I used
sed -i 's/"$user"/"$newuser"/g' /mypractice/myfiles/testfile
But it not working here. I test with it in my testfile singly it change and replace all.But i need to change only that position i want .
I also tried with usermod but it will not works here..
Can anyone give me the solution or correct my code...Thanks
You are using g flag in your sed command which means global substitution (will change all occurrences). Also, the variables although quoted are wrapped inside single quotes and hence are not interpolated.
Try this:
sed -i "s/^$user/$newuser/" /mypractice/myfiles/testfile
I have placed a ^ anchor in the substitution part which means only substitute if the word is at the beginning of the line. This protects you from making a change if the name of the user is not at the start but somewhere in the middle.

Why can't I run my shell script to list users?

users='awk '{print $1}' /etc/passwd | sort -u'
for user in $users
do
echo " - $user"
done
this is my shell script . Problem is that show's an error.
the error is ---> users: command not found
please give me the solution frinds
With the code the way it is now I see that you're not assigning the output of the awk|sort command to the variable (maybe you wanted to use ` instead of ' ?)
This works:
#!/bin/bash
users=$(awk '{print $1}' /etc/passwd | sort -u)
for user in $users
do
echo " - $user"
done
Although you should be aware that /etc/passwd is not separated by spaces, so awk '{print $1}' won't give you the user's name (which maybe is what you wanted)
Edit:
As per #Andy Lester's comment to your question: If you save this code in a file (let's say /tmp/myscript.bash) to run it you have to type in a terminal:
/bin/bash /tmp/myscript.bash
or, since it starts with #!/bin/bash (read here) you could make it executable (using chmod u+x /tmp/myscript.bash) and then call it, just typing /tmp/myscript.bash. You can also save it in one of the PATH directories (type echo $PATH to see which are they), make it executable and then you'll be able to call it from anywhere, but I don't really recommend doing that because you may end up overwriting juicy system's commands if you're not careful. For instance, let's say you call your script with the unfortunate name of ls, save it in the first directory of the $PATH (in my case, /usr/local/sbin) Every time you type ls, you won't be listing directories, but calling your script... Which is bad.

Bash script: how to replace text from user input

I need to make an interactive bash script where the user will input a word, for example 'xyz' and it will replace a word in a text file.
For example, I have a script called example.sh. When i run it, it should ask:
What is your name?:
and the user will input 'xyz'.
The script should take that input and replace 'username' inside a text file called userlist.txt to 'xyz'
to be more clear, I need user input to run
sed -i -e s/username/userinput/g userlist.txt
You can use the read builtin to read user input, and you can then use sed(1) to do the text replacement:
if read -p "What is your name? " name; then
sed -i~ -e "s/username/${name}/g" userlist.txt
else
# Error
fi
#!/bin/bash
echo "What is your name?"
read name
sed -i -e s/username/"$name"/g userlist.txt

Resources