Help with shell script errors

hey watsup guys

i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error).

Code 1 :

#! /bin/sh
# count1 appends an increment to a file 200 times
# note that a file called numbers must be initialised to contain 0

count=0
while [ $count -lt 200 ] # loop 200 times 
do
count=`expr $count + 1` # increment counter
n=`tail -1 numbers` # get last line from numbers file
expr $n + 1 >> numbers # add 1 to it and append it back
done

Code 2:

#! /bin/sh
# count2 also increments and appends a value to the numbers file
# but only when it can successfully create a new hard link to the numbers file

count=0
while [ $count -lt 200 ] # still loop 200 times
do
count=`expr $count + 1` # increment the loop counter
if ln numbers numbers.lock # try to enter the critical region
then # in this case, ln is similar to TSL
n=`tail -1 numbers` # get the last number to increment
expr $n + 1 >> numbers # increment it and append it to the file
rm numbers.lock # exit the critical region
fi # Note that if the ln was unsuccessful, we don't
# do busy waiting, but just continue looping
done

These syntax errors are killing me. i would apreciate if someone could help as soon as possible

Much love

try changing,

echo `expr $n + 1` >> numbers # add 1 to it and append it back

please use codes tags next time.

unixfile / dosfile, eof line char ?
/bin/sh ? Some other sh, not BourneSh. It's more safety to use
ksh, bash, dash, zsh, then you know exactly which shell you are using.

Ex. case 1 is correct.

If your file numbers is empty, in first time, n is not number = expr not like so much and it is null = not enough arguments.
expr "$variable" + 1 is better than expr $variable + 1.

#! /bin/ksh  or bash or dash or ...
# count1 appends an increment to a file 200 times
# note that a file called numbers must be initialised to contain 0
echo 1 > numbers # empty file 1st and put the 1st value.
count=0
while (( count < 200 )) # loop 200 times
do
   ((count+=1)) # increment counter
   n=$(tail -1 numbers) # get last line from numbers file
   # why not use count ?
   ((n+=1))
   echo $n  >> numbers # add 1 to it and append it back
done

I agree with kshji. The clue is in the specification.
It is necessary to initialise the file "numbers" before entering the loop or the first time through we find that $n contains error messages "tail: cannot open input error: No such file or directory on file numbers" instead of a number. This is what caused the second "expr" to fail.

For minimal change to your script and to conform to the specification, try these two additional lines before the while loop. Purists will note that it can be done with one line.

count=0
touch numbers
echo $count > numbers
while [ $count -lt 200 ] # loop 200 times 

Tip: I used some "sleep 3" and "echo $variable" commands to slow the script down and provide enough diagnostics to work out which "expr" was going wrong.

Hi surubi,

I just copied your two scripts and I ran it in my bash shell of linux environment.It worked correctly and added the two hundred extra lines into the file.I ran your script by giving the input file numbers with integers only.

For ex,the content of the numbers file as follows.

1
2

After I ran the script,it appended another two hundred lines into the file.

I think your input file may contain string.If it contains string,your scripts won't work.If your input file contains string,then add the following line after the count=0 line in your both scripts.

declare -i n

The above statement converts the character into integer which is required by the expr command.

Thankx so much people, i have been able to solve my problem bcoz of you !!! you were right i had to create the file numbers, i was getting errors coz i didn't have one

I love you nerds