Mrdogkick
asked on
basic unix help....
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
ASKER
I put this in
$file=/home/hussain.ahmed/ coursework /chapter7/ text
but it says this
firstline: line 3: =/home/hussain.ahmed/cours ework/chap ter7/text: No such file or directory
$file=/home/hussain.ahmed/
but it says this
firstline: line 3: =/home/hussain.ahmed/cours
Can you show the script and how you run it?
ASKER
the code is below and I am running it by doing the following:
sh firstline
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
Remove the $ from $file=/home/hussain.ahmed/ coursework /chapter7/ text
#!/bin/bash
file=/home/hussain.ahmed/c oursework/ chapter7/t ext
function addText()
{
echo $text > /tmp/f.$$
cat $file >> /tmp/f.$$
mv /tmp/f.$$ $file
}
echo "Enter some text:"
read text
addText
#!/bin/bash
file=/home/hussain.ahmed/c
function addText()
{
echo $text > /tmp/f.$$
cat $file >> /tmp/f.$$
mv /tmp/f.$$ $file
}
echo "Enter some text:"
read text
addText
ASKER
perfect, thanks a lot mate! One more thing. Is there a way I could append the text to the middle of the file??
Yes
length=`cat $file | wc -l`
mid=`expr $length / 2`
ed $file << END
$mid
a
$text
.
w
q
END
length=`cat $file | wc -l`
mid=`expr $length / 2`
ed $file << END
$mid
a
$text
.
w
q
END
ASKER
where would that fit into the script?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect, thank you very much!
ASKER
could you please explain to me what you did in the solution. ie what does each line mean?
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
- 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
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