A little help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A little help please
# 29  
Old 11-14-2007
Quote:
Originally Posted by immyakram
#!/bin/bash

...

then running it - sh script_name ....
Why not run it

./script_name .....

otherwise adding the "#!/bin/bash" as the first line was a waste of time.
# 30  
Old 11-14-2007
I am running it but dont seem to function ryte.
# 31  
Old 11-14-2007
Quote:
Originally Posted by immyakram
I am running it but dont seem to function ryte.
Can you explain how you think it should be behaving, talk us through it.
# 32  
Old 11-14-2007
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......
# 33  
Old 11-14-2007
Quote:
Originally Posted by immyakram
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.
That is the essential nub of the problem. For instance, lastline would be a piece of cake

Code:
#!/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

Code:
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.

Code:
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

Code:
mv $$ $2

Does that help?

Now tell me any down sides of my solution... Smilie
# 34  
Old 11-14-2007
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.
# 35  
Old 11-14-2007
Quote:
Originally Posted by immyakram
and then try to see if it wor by doing - sh firstline.
Look at how I ran it

Code:
./firstline TEXT FILENAME

hence

$1 is TEXT

and

$2 is FILENAME
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question