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
Solved

basic unix help....

Posted on 2007-12-07
12
317 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.:
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…

860 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