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
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 :)
您可以使用awk
进行打印并将其通过管道传递给外壳
$ awk '{print "useradd -m "$1}' <file> | sh
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}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.