Jay Jay - 1 year ago 92
Linux Question

Pass content from .txt file as argument for bash script?

I need to "Write a script to add users to your system in which the names of the users are given to script as an argument n (in?, spell error by professor) a text file of the format

Last_Name First_Name Middle_Initial Group

(rest of instructions FYI) Your script should create unique user names of up to 8 characters, and generate
random passwords for the users. The users should be assigned home directories
depending on the group they are in. You can assume that the users will belong to
Management (“mgmt”), Employee (“empl”) or Temporary (“temp”) and that their
respective directories are under these group names in /home. For e.g., if John Doe is
in “mgmt.”, and his user name is jdoe1234, then his home directory should be in
/home/mgmt/jdoe1234. Lock the home directories such that no one else has
permissions to the home directories than the users themselves.
Your script should generate a file called users.txt that contains the following columns:

Last Name First Name UID Password

which can be used to distribute the user names and passwords to the users.

The first part (not in italics) is what confuses me. If I understand his wording correctly, he wants me to take text from a separate
file and use it as the arguments for my script? With the idea of

./file.sh arg1 arg2 arg3 arg4

except those args are going to be the first four words in the .txt file? Say, if the .txt file contains "Doe John A empl", then it would be like typing

./file.sh arg1 arg2 arg3 arg4

Here's my attempt so far (I've actually tried other things but this is what I have on screen right now, sort of what I started out with):

echo "$(cat file.txt)"
lname=$(echo $1|head -c 3)//to make username use first 3 letters of last name
fname=$(echo $2|head -c 1)//to make username use first letter of first name
echo "$lname$fname$mname$group"

As of right now, anything below the first line doesn't do anything. I got the first line from command line arguments from a file content and I used it because I thought it would let me use the content from a .txt file as arguments but it's clearly not doing that. It's just outputting the content of it, not using letting me using each word as an argument. Not sure what to do. Any help? I thought this wasn't going to be very difficult as I started writing the script assuming the user would provide the arguments but I reread the first part of the instructions and assume he wants them to be taken from a separate .txt file.

Answer Source

$(command) returns the output of the command. If you do $(cat some_file) it will return the text of the file. You can use it to give the content of a file as an argument doing:

cmd1 $(cat args_file)

So when you use echo $(cat file.txt), you get the same output as if you were using cat file.txt because cat sends the content of the file to echo which displays it to the standard output.

$n means argument n passed to the script ($0 being the name of the script). Here you simply have to provide one argument, the name of the file. So $2, $3 and $4 will not contain anything.

So, from the file you can only get a string with the names with $names=$(cat $1). In order to get each field separately, you can use cut:

lname=$(cut -d \  -f 1 $1)
fname=$(cut -d \  -f 2 $1)
mname=$(cut -d \  -f 3 $1)
group=$(cut -d \  -f 4 $1)


The symbol for comments in shell is # NOT //.

head displays the first lines of a file, head -c the first bytes. It does not cut the file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download