?
Solved

Basic Unix help

Posted on 2007-12-05
15
Medium Priority
?
379 Views
Last Modified: 2010-04-21
Hello all,
I am attempting to teach myself Unix from a book and have come upon my first script question. How would you go about this?? (SED and AWK are NOT to be used)

Pre-requisites
Create a file with x amount of lines in it, the content of your choice.

Write a script named 'firstline' 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 very top (the first line) of the file named in your second argument.
0
Comment
Question by:Mrdogkick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20412047
Try the following

count=$#
line=$1
while test $count -ge 2
do
   shift
   line=$line" "$1
   count=`expr $count - 1`
done
echo $line > /tmp/f.$$
if test -f $1
then
      cat $1 >> /tmp/f.$$
fi
mv /tmp/f.$$ $1
0
 

Author Comment

by:Mrdogkick
ID: 20412157
do u think u could explain what u have done there a little?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20412201
ok

$# is the number or args passed to the script
so $# -1 args are the line and the last arg is the file name
so I keep constructing the line and shifting the args to the left
once I reach to the last arg which will be in $1 I create a temp file with line at the top (1st line) then I check if the file exists and if exits I concatenate to the end of temp file then I move temp file to the file name given.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mrdogkick
ID: 20413206
so i made a file called lines with numerous amounts of lines in it. Then I madea file called firstLine to put the script in. Looking at the script, I can not see where it indicates the name of the text file (lines)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413266
OK,

Try this create a file call it myfile that contain 10 lines (any 10 lines)
now put the code posted earlier in a script call it myscript
make the script executable: chmod +x myscript
run it as

./myscript this text will be added to myfile

nof if you cat myfile you will see 11 lines with " this text will be added to" as the fist line
0
 

Author Comment

by:Mrdogkick
ID: 20413310
I did as you said and ran the script and typed in hello! the did ctrl d to end it and got the following error

 mv: missing destination file operand after `/tmp/f.8066'
> Try `mv --help' for more information
0
 

Author Comment

by:Mrdogkick
ID: 20413317
does this mean I need to change the paths somewhere?
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 20413438
Hi,

I have modified the script a little bet:

count=$#
line=$1
while test $count -gt 2
do
   shift
   line=$line" "$1
   count=`expr $count - 1`
done
echo $line > /tmp/f.$$
if test -f $2
then
      cat $2 >> /tmp/f.$$
fi
mv /tmp/f.$$ $2


- put the above code in a file called myscript
- run
chmod +x myscript
- run
./myscript add this text to myfile1

You should see that a file called myfile1 is created and contains

 add this text to

- run

./myscript add this 2nd text line to myfile1

You should see that  myfile1 contains

add this text to
add this 2nd text line to

I could not understand what you mean by "ran the script and typed in hello! the did ctrl d to end it"


0
 

Author Comment

by:Mrdogkick
ID: 20413481
forget what I said about ctrl D, was being stupid!!

I have added the code and it works fine. Only problem is, the text input by the user should be added to the first line of the file myfile (which I have already created and contains lines of text already). The filename should also stay the same, so in effect the script needs to add this line of user input to the begining of 'myfile' and save it.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413524
The file name does not change
and the line is added on top of the file
0
 

Author Comment

by:Mrdogkick
ID: 20413564
doh, thanks very much for your help mate!
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20413607
Welcome :)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20415082
A more basic solution would be:

#!/bin/bash
(echo "$1";cat $2) >/tmp/$$ && mv /tmp/$$ $2

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20415134
Hi Tintin  :)

How this will take care of the dynamic number of args where the last one is the file name and the rest are the line to add to the file?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20415494
My solution is based purely on the original question.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question