Home > Archive > Unix Programming > July 2006 > Absolute newcomer #1
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 #1
|
|
| nickthebubble29@hotmail.com 2006-07-11, 4:00 am |
| Hi all,
I am an absolute newcomer to UNIX and i've been given 4 scripting
excercises to have ready by coming Monday. This is part of an
assessment in order to enter an SQL/Java course so please heeelp. To be
fair I've been given a tutorial but my problem is that i don't know how
to do what i want to do.
========================================
=================
Pre-requisites
Create a file with x amount of lines in it, the content of your choice.
Script 1
Write a script that takes two arguments. The first being a line of
text, the second being your newly created file. The script should take
the first argument and insert it into the middle of the file.
Note! The file must retain the original name
========================================
=================
I am using the vi editor since my tutorial covers it and came up with
$ vi script2
line="$1"
file="$2"
# find number of lines in a given file
total_lines=wc -l file
# find middle of file
half_way_line='expr $total_lines / 2'
#place cursor in the middle of file
vi :half_way_line file
#insert blank line
o
#append the line of text
a line
ZZ
When i run the script
$ ./script2 'Inserted line in the middle' file1
all i get is the redirection > sign.
Thanks in advance.
| |
| Barry Margolin 2006-07-11, 8:00 am |
| In article <1152609338.183229.300130@p79g2000cwp.googlegroups.com>,
nickthebubble29@hotmail.com wrote:
> Hi all,
>
> I am an absolute newcomer to UNIX and i've been given 4 scripting
> excercises to have ready by coming Monday. This is part of an
> assessment in order to enter an SQL/Java course so please heeelp. To be
> fair I've been given a tutorial but my problem is that i don't know how
> to do what i want to do.
Sounds like you need to take a scripting course before trying to take
the SQL/Java course.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Chris F.A. Johnson 2006-07-11, 7:01 pm |
| On 2006-07-11, nickthebubble29@hotmail.com wrote:
> Hi all,
>
> I am an absolute newcomer to UNIX and i've been given 4 scripting
> excercises to have ready by coming Monday. This is part of an
> assessment in order to enter an SQL/Java course so please heeelp. To be
> fair I've been given a tutorial but my problem is that i don't know how
> to do what i want to do.
>
> ========================================
=================
> Pre-requisites
>
> Create a file with x amount of lines in it, the content of your choice.
>
> Script 1
>
> Write a script that takes two arguments. The first being a line of
> text, the second being your newly created file. The script should take
> the first argument and insert it into the middle of the file.
>
> Note! The file must retain the original name
> ========================================
=================
>
> I am using the vi editor since my tutorial covers it and came up with
>
> $ vi script2
> line="$1"
> file="$2"
>
> # find number of lines in a given file
> total_lines=wc -l file
>
> # find middle of file
> half_way_line='expr $total_lines / 2'
>
> #place cursor in the middle of file
> vi :half_way_line file
>
> #insert blank line
> o
>
> #append the line of text
> a line
>
> ZZ
>
> When i run the script
>
> $ ./script2 'Inserted line in the middle' file1
line="$1"
file="$2"
half_way_line=$(( $( wc -l < "$file" ) / 2 ))
{
head -n $half_way_line "$file"
printf "%s\n" "$line"
tail -n $(( $half_way_line + 1 )) "$file"
} > tempfile
mv tempfile "$file"
--
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
| |
| Ian Bell 2006-07-11, 7:01 pm |
| Chris F.A. Johnson wrote:
> _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
I have seen your book at Amazon but there is no contents list or reviews. Is
it any good?
Ian
| |
| Jim Cochrane 2006-07-11, 7:01 pm |
| On 2006-07-11, nickthebubble29@hotmail.com <nickthebubble29@hotmail.com> wrote:
> Hi all,
>
> I am an absolute newcomer to UNIX and i've been given 4 scripting
> excercises to have ready by coming Monday. This is part of an
> assessment in order to enter an SQL/Java course so please heeelp. To be
> fair I've been given a tutorial but my problem is that i don't know how
> to do what i want to do.
A couple tips:
Tip #1: Learn how to construct a good subject line for a usenet post. A
good one for your post would have been something like:
"Beginner needing help with scripting assignment". (Hint: the subject
should be a short, concise summary of the topic of the post - This allows
people in a hurry who can help you to decide to read your post; otherwise,
they might not take the time.)
....
> $ vi script2
> line="$1"
> file="$2"
>
> # find number of lines in a given file
> total_lines=wc -l file
Tip #2: Study the `cmd` construct and/or (for ksh/bash), the $(cmd)
construct.
Tip #3: have fun.
--
| |
| Robert Gamble 2006-07-11, 7:01 pm |
| Ian Bell wrote:
> Chris F.A. Johnson wrote:
>
>
> I have seen your book at Amazon but there is no contents list or reviews. Is
> it any good?
The USA Amazon site (I assume you were looking at the UK version) for
the book has 6 reviews, all positive. Additionally, see
http://www.unixreview.com/documents/s=9884/ur0509c/ for another review
and http://www.bookpool.com/sm/1590594711 for a 22-page excerpt.
Robert Gamble
| |
| nobody@nowhere.nonet 2006-07-20, 4:01 am |
| nickthebubble29@hotmail.com <nickthebubble29@hotmail.com> spewed this unto
the Network:
> Hi all,
>
> I am an absolute newcomer to UNIX and i've been given 4 scripting
> excercises to have ready by coming Monday. This is part of an
> assessment in order to enter an SQL/Java course so please heeelp. To be
> fair I've been given a tutorial but my problem is that i don't know how
> to do what i want to do.
>
> ========================================
=================
> Pre-requisites
>
> Create a file with x amount of lines in it, the content of your choice.
>
> Script 1
>
> Write a script that takes two arguments. The first being a line of
> text, the second being your newly created file. The script should take
> the first argument and insert it into the middle of the file.
>
> Note! The file must retain the original name
> ========================================
=================
>
> I am using the vi editor since my tutorial covers it and came up with
>
> $ vi script2
> line="$1"
> file="$2"
>
> # find number of lines in a given file
> total_lines=wc -l file
In Bash, this would generate the error "-l: Command not found", and
leave $total_lines blank. You probably meant to write:
total_lines=$( wc -l file )
....which won't work because the output of 'wc' includes the file's name.
Also, you don't seem to realize that you're counting the lines in
a file that is literally named "file". To use the variable that
you assigned to at the top of the script, call it $file.
The following would fix all of those problems. By using redirection,
'wc' reads the file from its standard input, without knowing its name.
The $ in front of file causes the shell to use the value of that variable
instead of the string "file":
total_lines=$( wc -l < $file )
Or, if the instructor wants you to use backticks instead:
total_lines=`wc -l < $file`
> # find middle of file
> half_way_line='expr $total_lines / 2'
This assigns the string 'expr $total_lines / 2' to the variable
$half_way_line. It does not execute the expr command. You probably
meant to use the backtick character (`) instead of the single quote
('). Many shell programmers prefer to enclose the command in parentheses
preceded by a dollar sign, which does the same thing:
half_way_line=$(expr $total_lines / 2)
It is, however, more common to use the $((math expression)) syntax:
half_way_line=$(($total_lines / 2))
> #place cursor in the middle of file
> vi :half_way_line file
This calls 'vi' and tells it to edit a file called :half_way_line, and
to edit another file called "file" when the user gives the :next command.
The shell will wait for the user to exit from 'vi' with ":q" before it
continues processing the following:
> #insert blank line
> o
This doesn't insert a blank line, but merely runs "o" as a shell command.
You will probably get the error message "command not found." The same
problem will affect the next two lines as well.
> #append the line of text
> a line
>
> ZZ
>
> When i run the script
>
> $ ./script2 'Inserted line in the middle' file1
> all i get is the redirection > sign.
> Thanks in advance.
I'm surprised you got that much. Your script has many problems, as explained
above. The following script would work:
#!/bin/bash
file="$1"
line="$2"
# Note the use of a $ in front of file, so that the shell
# replaces it with its value. Also notice the use of redirection
# with the 'wc' command, to prevent the file name from being
# included in the value.
length=$(wc -l < $file)
# If $length is odd, the remainder is dropped. 5 divided by 2 is 2,
# not 2.5.
middle=$(($length / 2))
# Because of that, $middle+$middle does not necessarily equal $length.
# This is so that files with odd numbers of lines will be processed
# correctly.
rest=$(($length - $middle))
# Put the first half of the file into a temporary file. 'head'
# outputs the first n lines of the file, if n is the number following
# the -n option.
head -n $middle $file > TEMPORARY
# Add the line in.
echo $line >> TEMPORARY
# Add the rest of the original file. 'tail' is the opposite of 'head'.
tail -n $rest $file >> TEMPORARY
# Replace the original file with the new one
mv TEMPORARY $file
--
All your .signature are belong to us.
Take off every 'sig' for great justice.
|
|
|
|
|