Operating System - HP-UX
1845943 Members
3642 Online
110250 Solutions
New Discussion

Getting script input from another file.

 
SOLVED
Go to solution
Scott E Smith
Frequent Advisor

Getting script input from another file.

Hello all, I am new to script programming and I have a question. I have an inherited script that gets information from a delimited file. Can anyone tell me what the signifigance of the folliwng lines are?
exec 5< /opt/security/os/eis2_fplus.data

IFS=":"
while read -u5 userid first_name last_name / user_role

The input data file looks like this...

qf71701:Joe:Blow:users:admin

Thanks.

I'm wanting to run a daily script to add new users and I want to put all of the user specific information into a delimited file that will gwt sent over each night. Thanks.
4 REPLIES 4
Alan Riggs
Honored Contributor
Solution

Re: Getting script input from another file.

1) exec 5< /opt/security/os/eis2_fplus.data

This line opens an input stream (5) from the file /opt/security/os/eis2_fplus.data

2) IFS=":"

This line replaces the IFS variable (internal field separator: normally set to {space, tab, newline}) with the ":". In other words, all input will be delimited by colon rather than whitespace.

3) while read -u5 userid first_name last_name / user_role

This line sets a while loop which reads the fields "userid" "first_name" etc. from the input stream denoted by identifier "5".
Scott E Smith
Frequent Advisor

Re: Getting script input from another file.

So the occurrance of "5" is just a reference and doesn't hold any other significance?
RikTytgat
Honored Contributor

Re: Getting script input from another file.

Scott,

Explained line by line:

exec 5< /opt/security/os/eis2_fplus.data
-------

The exec shell built-in in this form (without arguments) rearranges I/O redirection, In your case, it means that the file is attached to file descriptor 5.

IFS=":"
-------
Set the IFS (Internal Field Separator) to the character ':'. The default IFS is whitespace, and is used to determine the boundaries between commands an options and arguments, to determine fields for some shell built-ins as read (as in your case).

while read -u5 userid first_name last_name / user_role
------

Read the next line from file descriptor 5 (that is connected to your file, remember), assigning the first field to the variable userid, the second field to the variable first_name and so forth.

For more information on this and other shell magic, see the shell manpage (sh-posix(1)) or one of the O'Reilly books on shell scripting.


Hope this helps,
Rik.
James R. Ferguson
Acclaimed Contributor

Re: Getting script input from another file.

Scott:

In reference to your question "So the occurrance of "5" is just a reference and doesn't hold any other significance?":

'5' happened to be the file descriptor number chosen by the programmer for association with the file /opt/security/os/eis2_fplus.data.

A file descriptor is a small unique, per-process, nonnegative integer identifier that is used to refer to a file opened for reading and/or writing. Each file descriptor refers to exactly one open file description.

Remember '0' is standard-in (stdin), '1' is the file descriptor for standard-out (stdout), and '2' is the descriptor for standard-error (stderr). The shell allows you to use values 3-9 to associate with any file of your choice. A larger number of descriptors are available to C-programs.

...JRF...