Board Logo

Who knows shell script?
amazer - March 28th, 2004 at 11:55 AM

I want to create a simple little hit counter in shell. I figure I will store a value in a file. Read it when the page is accessed using SSI and write a new value over the top. Trouble is, I am useless at scripting. So far what I have is this:

#!/bin/sh
NUM=`cat counter.dat`
let "NUM=$NUM+1"
echo $NUM >> counter.dat

but that seems to be concatenating the file.
How do I overwrite?



Thanks in advance.
Chris.


NEVER MIND. seems to be working now. spooky.


[Edited on 28-3-2004 by amazer]


vanderaj - March 28th, 2004 at 12:22 PM

> is overwrite


amazer - March 28th, 2004 at 01:54 PM

freaky how I must have accidently deleted one of the > and stumbled upon the solution. bizarre. So now that is working fine when I execute the script manually from a terminal window. But it isnt working when I call it through my web???? Its not giving any errors, it just doesnt seem to be writing to the file. Its not to do with refresh as I have checked the datafile.

#!/bin/sh
NUM=`cat ../htdocs/mystuff/counter.dat`
let "NUM=$NUM+1"
echo $NUM > ../htdocs/mystuff/counter.dat

<body>
<b>Welcome to my world!</b><br>
You are visitor number: <br>
<!--#exec cgi="/cgi-bin/counter.cgi" -->
<!--#include file="counter.dat" -->
<!--#config timefmt="%d/%m/%y" -->Today's Date:
<!--#echo var="DATE_LOCAL"--><br>
</body>

IM guessing its something to do with pathnames

dont know how to represent this dir structure here. cgi-bin and htdocs are on the same level.

>cgi-bin dir > counter.cgi
>htdocs dir > my stuff dir > welcome.shtml, counter.dat


Mentat - March 28th, 2004 at 02:30 PM

What are the permissions on the counter.cgi.

Is the webserver user (normally apache or nobody) able to modify the .dat file and execute the counter.cgi?

Regards
- Adam


amazer - March 28th, 2004 at 05:13 PM

well that would have been part of the problem, thanks.
Changed the permissions to 755 for the cgi and 777 for the dat file but still no worky. this sucks.


amazer - March 28th, 2004 at 05:40 PM

i AM getting an error message.

Premature end of script headers: /spare0/csci399/clf02/cgi-bin/counter.cgi


modulus - March 28th, 2004 at 06:44 PM

I don't know Jack about *Shell* CGI, but in CGI programs I write in Perl, C etc., you need to give a valid response to the call,typically sending back an OK error code and some text output (tho' other options abound). Certainly yr. CGI program needs to respond, not just execute and exit.

hth


Mentat - March 28th, 2004 at 06:53 PM

Are you editing the files live on the server using vi or are you editing them in notepad/other windows programs...

windows uses CRLF as a new line charachter which can cause problems on unix type boxen.


amazer - March 28th, 2004 at 07:17 PM

editing with pico on the server.

got rid of the header error. Forgot to put the echo "Content-type: text/plain\n\n" line in.

But now!!! and I am sure I wasnt getting this before, I'm getting this...

/spare0/csci399/clf02/cgi-bin/counter.sh: let: not found

file now reads:

#!/bin/sh
echo "Content-type: text/plain\n\n"

NUM=`cat ../htdocs/mystuff/counter.dat`
let "NUM=$NUM+1"
echo $NUM > ../htdocs/mystuff/counter.dat


modulus - March 28th, 2004 at 07:27 PM

That looks awful; "let" used to be found in the most crippled BASICS.

Also I would think that "NUM" either needs a $ sign or doesn't....

surely those 'n's need to be escaped...


#!/bin/sh
echo "Content-type: text/plain\n\n"

$NUM=`cat ../htdocs/mystuff/counter.dat`
$NUM++
echo $NUM > ../htdocs/mystuff/counter.dat

but, I'm like a blind man in a dark room looking for a black cat that may not be there; I don't know Shell script syntax...

hth


amazer - March 28th, 2004 at 07:37 PM

I know it sucks.

$NUM++ is a no go.
it takes value of $NUM first
so it ends up 280++ and doesnt know what to do with it. Tried putting brackets everywhere to no avail. Not sure that it understands ++ at all.

Ive resorted to beer.


amazer - March 28th, 2004 at 07:50 PM

:beer
Never underestimate the power of beer and a Hunters and Collectors session on the CD player.

#!/bin/sh
echo "Content-type: text/plain\n\n"

NUM=`cat ../htdocs/mystuff/counter.dat`
NUM=`expr $NUM + 1`
echo $NUM > ../htdocs/mystuff/counter.dat

ITS ALL GOOD!
The horns, the percussion..

"Do you see what I see
Do you se what I seeee...."


modulus - March 28th, 2004 at 07:56 PM

Almost all good.

I'm guessing the point of having you write a 'counter' program is to highlight the need for file locking and atomic operations, and those aren't there yet; imagine you've got 5 users simultaneously wanting to read the value of the counter and increment it....last one to write wins....

hth


amazer - March 28th, 2004 at 08:02 PM

naaaaah, thats a different subject LOL.
Distributed systems, operating systems and databases was about locks. Now I just need to know different server shit.
PHP ASP CGI SSI PL SH all those acronyms :P
I'll move on to some harder liquor when I have to do any serious regex stuff. I have nightmares about that crap. This is my second attempt at this subject. Walked out of the exam with the shits 15 minutes into it last year.


Brad - March 28th, 2004 at 11:23 PM

few ??? for you ??

Do you have a text book ?
Can yu read it ?? :P:P

What shell r u using ? I am guessing Bourne by your sh directory.
You seem to have some weird ass metscharacters in there which is why I ask.
Shouldn't you have some mor $$ signs in order to echo $ Num > to file ??

Now I dunno exactly what you are trying to do but you don't appear to be writting to the file correctly. I would try adding $echo $Num

Echo is a shell command that displays all its arguemenst to standard output and $ exapands the value of the vaiable which is what you want .

ALso why do you have NUM=`expr $NUM + 1` to incriment count ? What is the expr in aid of ? I would think in bourne the ((Num = Num + 1 )) would be fine to incrimnet loop / local variable of Num each time. If this isn't fancy enough try

set Num =1
@ Num ++
echo $Num > file name etc etc .......

Am I making any sense ?


amazer - March 29th, 2004 at 10:21 AM

Brad, the last listing works.
I tried using more $ and less $ and all sorts of things for a whole day. This is the ONLY way I can get it to work.

I dont have a text book. Generally I dont read, the text books I do have , I dont read (much). They usually cost around $80-$100. Cant claim the books on tax if I dont have a job nor income so dont get them. Anything in a book can also be found on google and I dont need to know the alphabet to look up an index.

In contrast the perl script I wrote after that, took me about 1 hour. Bastards. All day to write 4 lines, 1 hour to write about 20.