Solved

basic unix help....

Posted on 2007-12-07
12
302 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
Can you show the script and how you run it?
0
 

Author Comment

by:Mrdogkick
Comment Utility
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
Comment Utility
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
Comment Utility
perfect, thanks a lot mate! One more thing. Is there a way I could append the text to the middle of the file??
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
Yes

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

Author Comment

by:Mrdogkick
Comment Utility
where would that fit into the script?
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 125 total points
Comment Utility
#!/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
Comment Utility
perfect, thank you very much!
0
 

Author Closing Comment

by:Mrdogkick
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now