?
Solved

basic unix help....

Posted on 2007-12-07
12
Medium Priority
?
332 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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