Unix Programming - Absolute newcomer #2

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2006 > Absolute newcomer #2





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Absolute newcomer #2
nickthebubble29@hotmail.com

2006-07-11, 8:10 am

Hi again,

The second exersice follows

========================================
===
Pre-requisites

Run the command's below

env >> xx
env >> xx
env >> xx
env >> xx
env >> xx

You will now have a file called XX with the env redirected into it 5
times

Script2

Create a script that takes 1 argument being a file, in this instance we
will use the newly created file above xx read the inputted file, in
this case xx and look for occurrences of the current user who is
executing the script. On finding an occurrence of the username take
that line and append it to a file and display a line number and a
bracket against the saved line.

The newly created file should look similar to this

1) PWD=/home/user.name
2) USER=user.name
3) MAIL=/var/spool/mail/user.name
4) LOGNAME=user.name
5) HOME=/home/user.name
6) PWD=/home/user.name
7) USER=user.name
8) MAIL=/var/spool/mail/user.name
9) LOGNAME=user.name
10) HOME=/home/user.name

Note! The use of a temporary file is not allowed
========================================
============

I understand that the command $ ls -l will list all the files in
/home/user.name but how can i loop through the files in the
/home/user.name directory.

Also how can i extract the user.name out of every line of the listed
files so that i can append it to the newly created file.

Many thanks.

Fred Kleinschmidt

2006-07-11, 1:33 pm


<nickthebubble29@hotmail.com> wrote in message
news:1152613781.554301.95650@s13g2000cwa.googlegroups.com...
> Hi again,
>
> The second exersice follows
>
> ========================================
===
> Pre-requisites
>
> Run the command's below
>
> env >> xx
> env >> xx
> env >> xx
> env >> xx
> env >> xx
>
> You will now have a file called XX with the env redirected into it 5
> times
>
> Script2
>
> Create a script that takes 1 argument being a file, in this instance we
> will use the newly created file above xx read the inputted file, in
> this case xx and look for occurrences of the current user who is
> executing the script. On finding an occurrence of the username take
> that line and append it to a file and display a line number and a
> bracket against the saved line.
>
> The newly created file should look similar to this
>
> 1) PWD=/home/user.name
> 2) USER=user.name
> 3) MAIL=/var/spool/mail/user.name
> 4) LOGNAME=user.name
> 5) HOME=/home/user.name
> 6) PWD=/home/user.name
> 7) USER=user.name
> 8) MAIL=/var/spool/mail/user.name
> 9) LOGNAME=user.name
> 10) HOME=/home/user.name
>
> Note! The use of a temporary file is not allowed
> ========================================
============
>
> I understand that the command $ ls -l will list all the files in
> /home/user.name but how can i loop through the files in the
> /home/user.name directory.
>
> Also how can i extract the user.name out of every line of the listed
> files so that i can append it to the newly created file.


You don't need to - it is already included on the line.
You just need to determine who the current user is, so you can
extract lines that contain that string.

What do you think the value of $USER will be?

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


Chris F.A. Johnson

2006-07-11, 1:33 pm

On 2006-07-11, nickthebubble29@hotmail.com wrote:
> Hi again,
>
> The second exersice follows
>
> ========================================
===
> Pre-requisites
>
> Run the command's below
>
> env >> xx
> env >> xx
> env >> xx
> env >> xx
> env >> xx
>
> You will now have a file called XX with the env redirected into it 5
> times


No, you will not. You will have a file called xx. If the file xx
already existed, the output of the env command will be appended to
the existing file.

> Script2
>
> Create a script that takes 1 argument being a file, in this instance we
> will use the newly created file above xx read the inputted file, in
> this case xx and look for occurrences of the current user who is
> executing the script. On finding an occurrence of the username take
> that line and append it to a file and display a line number and a
> bracket against the saved line.
>
> The newly created file should look similar to this
>
> 1) PWD=/home/user.name
> 2) USER=user.name
> 3) MAIL=/var/spool/mail/user.name
> 4) LOGNAME=user.name
> 5) HOME=/home/user.name
> 6) PWD=/home/user.name
> 7) USER=user.name
> 8) MAIL=/var/spool/mail/user.name
> 9) LOGNAME=user.name
> 10) HOME=/home/user.name
>
> Note! The use of a temporary file is not allowed
> ========================================
============
>
> I understand that the command $ ls -l will list all the files in
> /home/user.name but how can i loop through the files in the
> /home/user.name directory.


Why do you want to do that? It's not part of the assignment.

> Also how can i extract the user.name out of every line of the listed
> files so that i can append it to the newly created file.


Read the grep man page.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
nobody@nowhere.nonet

2006-07-20, 7:51 am

nickthebubble29@hotmail.com <nickthebubble29@hotmail.com> spewed this unto
the Network:
> Hi again,
>
> The second exersice follows
>
> ========================================
===
> Pre-requisites
>
> Run the command's below
>
> env >> xx
> env >> xx
> env >> xx
> env >> xx
> env >> xx
>
> You will now have a file called XX with the env redirected into it 5
> times
>
> Script2
>
> Create a script that takes 1 argument being a file, in this instance we
> will use the newly created file above xx read the inputted file, in
> this case xx and look for occurrences of the current user who is
> executing the script. On finding an occurrence of the username take
> that line and append it to a file and display a line number and a
> bracket against the saved line.
>
> The newly created file should look similar to this
>
> 1) PWD=/home/user.name
> 2) USER=user.name
> 3) MAIL=/var/spool/mail/user.name
> 4) LOGNAME=user.name
> 5) HOME=/home/user.name
> 6) PWD=/home/user.name
> 7) USER=user.name
> 8) MAIL=/var/spool/mail/user.name
> 9) LOGNAME=user.name
> 10) HOME=/home/user.name
>
> Note! The use of a temporary file is not allowed
> ========================================
============
>
> I understand that the command $ ls -l will list all the files in
> /home/user.name but how can i loop through the files in the
> /home/user.name directory.
>
> Also how can i extract the user.name out of every line of the listed
> files so that i can append it to the newly created file.
>
> Many thanks.
>


ls -l is irrelevant. The commands you need are grep (print only lines that
match an expression), nl (number lines), sed (stream editor), and whoami
(displays the current user name). It is possible to write a working script
in one line. Extensive use of pipes is required. The following assumes you
have assigned the env file's name to a variable called ENVFILE, and the
output file's name is assigned to OUTFILE

grep $(whoami) $FILE | nl | sed 's/\([0-9]\)[[:space:]]/\1) /' >> $OUTFILE

Read the manual page on 'grep' for the basics on regular expressions.
Both grep and sed use them.

The assignment is probably trying to get you to understand pipes, which
are usually used to connect the standard output of one process to the
standard input of another. You are probably also expected to understand
that many UNIX programs are filters, designed to be used with pipes. All
the programs in the above pipeline except "whoami" are filters: They
read something on their standard input, do something to it, and send
the result to the standard output.

The most difficult part to understand, however, is the argument to sed.
An entire regular expression must be passed to it as one argument. Since
regular expressions may contain shell metacharacters that we're not
interested in expanding, single quotes are required.

The 's' at the beginning indicates that sed is going to substitute something.
[0-9] matches any number (as "nl" will put a number before each line),
while [[:space:]] matches all whitespace characters. \([0-9]\) means
that sed will remember the number as a subexpression. All this is
delimited by slashes (/). After the second slash, \1 recalls the subexpression
remembered by \([0-9]\), and ) instructs sed to put a right parenthesis
and a space after the number. The final slash completes the expression.

Since sed will only replace the first match it finds (unless you add
a 'g' after the last slash), numbers in the variables' values will not
cause the script to exhibit unwanted behavior.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com