user5999889
user5999889

Reputation:

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"

Upvotes: 1

Views: 1440

Answers (2)

John Kugelman
John Kugelman

Reputation: 361889

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.

Upvotes: 1

tripleee
tripleee

Reputation: 189679

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.

Upvotes: 1

Related Questions