No not yet. I think its in the next chapter. Ill try ur syntax now.
Now you tell me
how and why it works and
what each part is actually doing, and
any problems with it.
Hmmmmmmmm.... files called filex and the script that im creating now and where syntax is called firstline. But still dont knw what to reference after $'s
so you should have put my code in a file called "firstline"
Then run it as follows...
chmod +x firstline
echo "original line" >testfile
cat testfile
./firstline "another line" testfile
cat testfile
Not working
get a error after ./
Post the contents of your file and the results from your screen.
Eg,
bash-2.05$ cat firstline
#!/bin/sh
echo "$1" | cat - "$2" >$$
mv $$ "$2"
bash-2.05$ chmod +x firstline
bash-2.05$ echo "original line" >testfile
bash-2.05$ cat testfile
original line
bash-2.05$ ./firstline "another line" testfile
bash-2.05$ cat testfile
another line
original line
bash-2.05$
#!/bin/bash
file=$HOME/file1
{read $1
} < $2
echo "this first line in $2 is "$1".
then running it - sh script_name $1 $2
Iv been tryna play around with this...
So where in your program do you write out the new file with the new first line inserted?
Why not run it
./script_name .....
otherwise adding the "#!/bin/bash" as the first line was a waste of time.
I am running it but dont seem to function ryte.
Can you explain how you think it should be behaving, talk us through it.
well from my understanding the script (firstline) needs to take on 2 arguments which can be done through $. So $1 could be argument 1 and $2 can be argument 2. The script needs to take the first argument and insert it into the very top - teh first line of my second file. I believe this can done by using the echo function by getting the line from $1 and then inserting it into $2 bu cant quite put my finger on it.
P.S. Sorry for all the bother matey......
That is the essential nub of the problem. For instance, lastline would be a piece of cake
#!/bin/sh
echo "$1" >>"$2"
because the >> operator appends the data. But this problem says we have to put the data as a new first line.
So my solution says, okay I know what the contents of the new file should look like
echo "$1" | cat - "$2"
but that output just dissappears, so we need to put it somewhere, like a temporary file, here $$ will be used as a temporary file as it's a unique number, ie the pid of the shell.
echo "$1" | cat - "$2" >$$
but the problem says that this new output should be in the original file, so what we do is rename this temporary file to the original file name, which as a byproduct is (a) atomic (b) unlinks the original file
mv $$ $2
Does that help?
Now tell me any down sides of my solution... ![]()
I have the following in my script
#!/bin/sh
echo "$1" | cat - "$2" >$$
mv $$ "$2"
Once i have done this and i exit the editor i have done chmod +x firstline.
and then try to see if it wor by doing - sh firstline.
but i get no such file or directory message.
Look at how I ran it
./firstline TEXT FILENAME
hence
$1 is TEXT
and
$2 is FILENAME
Got it thanks a million matey. 
These kind of posts are not supposed to be in this technical forum.
I don't think its posted by mistake.
Moderators, please !!!
Sorry, im new to this forum, thogught it was supposed to go here.
No, he was refering to some advert somebody posted into this thread.
You're doing well.
Thats alright then.