How to read contents of a file into variable :(

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions How to read contents of a file into variable :(
# 1  
Old 03-06-2011
How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
I have to read the contents of each field of a file creating user accounts.
The file will be of format :

username : student information : default shell : student ID
Eg :

Code:
joeb:Joe Bennett:/bin/csh:1234
jerryd:Jerry Daniels:/bin/csh:2345
deaverm: Deaver Michelle:/bin/bash:4356
joseyg:Josey Guerra:/bin/bash:8767
michaelh:Michael Hall:/bin/ksh:1547

again i need group them to a group, depending on the first initial of their username, in the following manner: Username Group GID
A-E sce 601
F-J edu 602
K-N bus 603
O-S bio 604
T-Z math 605


2. Relevant commands, code, scripts, algorithms:
cat
touch
useradd
groupadd
passwd
read

3. The attempts at a solution (include all code and scripts):

i have created a file with the contents .. in such a way that , wach field is seperated by delimiters.

Now, the problem is, how should i read each field from every line.

After that i even have to know a way of determining the 1st letter of the 1st field,
so that i can send it into a case statement for deciding the group.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University of Houston , Clear Lake, US

Professor name : Krishani Abeysekera

Course number : CSCI 4634
Krishani Abeysekera - Lecturer and Systems Administrator



---------- Post updated at 07:19 PM ---------- Previous update was at 05:16 PM ----------

Small hint..will help me!! Smilie

---------- Post updated at 08:56 PM ---------- Previous update was at 07:19 PM ----------

Hint please!!!SmilieSmilieSmilieSmilieSmilie

Last edited by rbatte1; 10-07-2016 at 09:34 AM..
# 2  
Old 03-06-2011
What language? What OS? I can't read that Greek stuff. Smilie
Code:
#! /usr/bin/ksh
IFS=":"
exec < inputfile
while read loginname fullname shell id ; do
        echo loginname = $loginname
        echo fullname = $fullname
        echo shell = $shell
        echo id = $id
done
exit 0

This is a start if your ksh is in /usr/bin/ksh. It shows how to read a file and crack it into fields.
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 03-06-2011
@perderabo .. first of all thanks for the reply.. Smilie

i have installed fedora 14 on a virtual box on windows 7..

and actually i was doing a bash script..

TIA Smilie
# 4  
Old 03-07-2011
bash and ksh are pretty close. Just change the first line to #! /bin/bash
This User Gave Thanks to Perderabo For This Post:
# 5  
Old 03-07-2011
Thanku so much..will try this one!! Smilie Smilie

---------- Post updated at 01:31 AM ---------- Previous update was at 12:54 AM ----------

One last question ..

i got the account name into $loginname variable ..

Is there any way that .. i can just get the 1st letter from the variable "loginname" Smilie

---------- Post updated at 01:33 AM ---------- Previous update was at 01:31 AM ----------

Actually i have to use a case construct to assign a group to username, based on the 1st letter ..

So,. i just wanted a way to retrieve the 1st letter from the variable .. loginname

---------- Post updated at 02:01 AM ---------- Previous update was at 01:33 AM ----------

I almost completed the project .. just stuck at the group assigning section now . Smilie
# 6  
Old 03-07-2011
${VAR:0:1} will eval to the first letter in the VAR variable in BASH and newer versions of ksh (ksh88/pdksh is too old, which causes much consternation). Sorry for the slow answer but you ran afoul of time zones, it seems.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-07-2011
actually.. done with everything...

the only problem now is,

i can see all the accounts created in /etc/passwd file..

but in the login screen, its not showing the 1st two accounts created

---------- Post updated at 03:29 PM ---------- Previous update was at 03:11 PM ----------

problem got rectified..
Thank you every one..

The actual problem was.. i was giving the shells for the accounts as "ksh and csh " and that was the problem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

3. Programming

read() contents from a file

Hi, I'm trying to implement a C program on ubuntu which reads the contents of a file that is passed in as an argument and then displays it to the screen. So far I've cobbled together this from bits online but most of it is probably wrong as its all copied and pasted... #include <stdio.h>... (2 Replies)
Discussion started by: cylus99
2 Replies

4. Shell Programming and Scripting

how to read contents of file?

I have made a script something like this. I want it to read the contents of either file or directory but 'cat' and 'ls' is not working. Can anyone help me? I am a newbie in scripting so dont know much about it. I also dont know how can i put my code separatly on this forum #!/bin/bash echo... (9 Replies)
Discussion started by: nishrestha
9 Replies

5. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

6. Shell Programming and Scripting

Read contents from a file

Hi Friends, I am new to this forum. Just struck up with a logic. I have a csv file seperated by ":" (colons). This csv file contains hostname and groups as follows: HOSTNAME:VT Group SGSGCT2AVPX001:Team1 SGSGCT2AVPX003:Team2 SGSGCT2AVPX005:Team2 PHMNCTTAVPX001:Team3 I want to... (2 Replies)
Discussion started by: dbashyam
2 Replies

7. Shell Programming and Scripting

Read the contents of a file and store them in a variable

Hi Gurus, I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script: #!/bin/ksh while read line do id=`echo $line | cut -f1 -d |` name=`echo $line | cut -f2 -d |` echo $id ... (11 Replies)
Discussion started by: svajhala
11 Replies

8. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies

9. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

10. Shell Programming and Scripting

Read File and use contents to rename another

Hello guys, thank God that I found this forum. I hope that someone can help me because I don't have any idea on how to start it. I know that for some of you this is a very simple task but I'm not as advance on shell scripting like many people out there. I got this file with a permanent... (10 Replies)
Discussion started by: Shark Tek
10 Replies
Login or Register to Ask a Question