Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

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 .txt 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):

#!/bin/bash
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
mname=$3
group=$4
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.

like image 219
Jay Avatar asked Mar 09 '15 07:03

Jay


2 Answers

$(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)

NOTES:

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.

like image 180
Math Avatar answered Oct 20 '22 20:10

Math


What I understood as the problem is that:

Firstly: You want to pass the contents of a text file as an input/argument to a shell script.

There could be other ways, but I suggest the following:

./YourScriptFile.sh $(cat YourInputFile.txt)

Secondly: You want to use its contents.

I would suggest to use the following inside your script file:

$1, $3, $4, ..., $n

to access the:

1st, 2nd, 3rd, ..., nth

tokens from the input file (irrespective of new lines).

Finally: You want to make username use first 3 letters of last name or in other words you want to extract characters from a string.

Once you have tokens it's just simple. I would suggest the following:

FirstTwoChars_of_FirstToken=${1:0:2}
FirstTwoChars_of_SeventhToken=${7:0:2}

Or

LastTwoChars_of_FirstToken=${1:7:9} 
# if the first token is "FirstName" it would return you "me"

Hope this would help you to improve your code.

As a footnote: Your code will become: (# is used for comments here)

#!/bin/bash
lname=$(1:0:3) #to make username use first 3 letters of last name
fname=$(2:0:1) #to make username use first letter of first name
mname=$3
group=$4
echo "$lname$fname$mname$group"

Now you will have to execute your shell script as mentioned above.

like image 36
tod Avatar answered Oct 20 '22 19:10

tod