Solved

basic unix help....

Posted on 2007-12-07
12
312 Views
Last Modified: 2010-04-21
I have started the code below and am stuck. What I want it to do is in the function, tell it to add the 'text' (which the user inputs) to the begining of a file I have already created called 'text' which  already contains numerous lines of text. Make any sense?


#!/bin/bash
 
function addText()
{
 
}
 
echo "Enter some text:"
read text

Open in new window

0
Comment
Question by:Mrdogkick
  • 6
  • 6
12 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426555
#!/bin/bash
 
function addText()
{
 echo $text >  /tmp/f.$$
cat $file  >>  /tmp/f.$$
mv /tmp/f.$$  $file
}
 
echo "Enter some text:"
read text
addTex

Here file is a variable that contains file name
0
 

Author Comment

by:Mrdogkick
ID: 20426609
I put this in

$file=/home/hussain.ahmed/coursework/chapter7/text


but it says this

firstline: line 3: =/home/hussain.ahmed/coursework/chapter7/text: No such file or directory
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426627
Can you show the script and how you run it?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Mrdogkick
ID: 20426657
the code is below and I am running it by doing the following:

sh firstline
#!/bin/bash
 
$file=/home/hussain.ahmed/coursework/chapter7/text
 
function addText()
{
 echo $text >  /tmp/f.$$
cat $file  >>  /tmp/f.$$
mv /tmp/f.$$  $file
}
 
echo "Enter some text:"
read text
addText

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426668
Remove the $ from $file=/home/hussain.ahmed/coursework/chapter7/text

#!/bin/bash
 
file=/home/hussain.ahmed/coursework/chapter7/text
 
function addText()
{
 echo $text >  /tmp/f.$$
cat $file  >>  /tmp/f.$$
mv /tmp/f.$$  $file
}
 
echo "Enter some text:"
read text
addText
0
 

Author Comment

by:Mrdogkick
ID: 20426680
perfect, thanks a lot mate! One more thing. Is there a way I could append the text to the middle of the file??
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426733
Yes

length=`cat $file | wc -l`
mid=`expr $length / 2`
ed $file << END
$mid
a
$text
.
w
q
END
0
 

Author Comment

by:Mrdogkick
ID: 20426743
where would that fit into the script?
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 125 total points
ID: 20426758
#!/bin/bash
 
file=/home/hussain.ahmed/coursework/chapter7/text
 
function addText()
{
length=`cat $file | wc -l`
mid=`expr $length / 2`
ed $file << END
$mid
a
$text
.
w
q
END
}
 
echo "Enter some text:"
read text
addText

Be careful, no space before the END word
0
 

Author Comment

by:Mrdogkick
ID: 20426792
perfect, thank you very much!
0
 

Author Closing Comment

by:Mrdogkick
ID: 31413364
could you please explain to me what you did in the solution. ie what does each line mean?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20426818
The code:

- calculate file length
- divide length by 2
- use ed editor to edit the file. The commands to ed are between << END and END.

You may refer to man ed to know how to use ed
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now