Help with shell script errors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with shell script errors
# 1  
Old 02-24-2010
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 :

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

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

Last edited by pludi; 02-24-2010 at 01:00 PM.. Reason: code tags, please...
# 2  
Old 02-24-2010
try changing,
Code:
echo `expr $n + 1` >> numbers # add 1 to it and append it back

please use codes tags next time.

Last edited by clx; 02-24-2010 at 12:47 PM..
# 3  
Old 02-24-2010
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.

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

# 4  
Old 02-24-2010
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.

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

Last edited by methyl; 02-24-2010 at 12:11 PM.. Reason: Layout
# 5  
Old 02-25-2010
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.
# 6  
Old 02-25-2010
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Errors in if condition validations-Shell Script

Hello All, i am facing difficulty in validating the values, kindly help me in resolving the issue. Thanks a lot in advance. -Chandra Script:Test.sh #! /bin/sh # *************************************************************************** # Function to display help function usage()... (1 Reply)
Discussion started by: duddukuri
1 Replies

2. Shell Programming and Scripting

How to turn off ora errors in shell script?

I have a shell script which select total count from a table and use its value in a if condition like below connect_string="username/password@tnsname" tot=`sqlplus -s $connect_string << EOF set echo off set feedback off set head off select count(*) from test_table; EOF ` if then echo... (2 Replies)
Discussion started by: vel4ever
2 Replies

3. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

4. UNIX for Dummies Questions & Answers

Invalid option errors running shell script

The script below fails with the following error messages: gzip: invalid option -- 'w' Try `gzip --help' for more information. mysqldump: Got errno 32 on write cp: invalid option -- 'w' Try `cp --help' for more information. rm: invalid option -- 'w' Try `rm --help' for more information. ... (1 Reply)
Discussion started by: ANNACTION
1 Replies

5. Shell Programming and Scripting

shell script for conky getting errors....

So I have a script that does a greater than or less than with a variable. My problem is that when I checked the script from the command line I was getting this error.. I am not really familiar with shell scripting. I am trying to get this script to work with conky so that it will do the... (8 Replies)
Discussion started by: krisdeniseriley
8 Replies

6. Emergency UNIX and Linux Support

Seek help on shell script syntax errors

I want to delete archivelog files that has been archived and applied from primary database to standby database. This piece of script is working in Linux server. However, I copy it to Unix server with tiny modification. It won't work and generate the error message. I have checked code carefullt... (8 Replies)
Discussion started by: duke0001
8 Replies

7. Shell Programming and Scripting

Capture makefile errors in shell script

Hi, I have a bash script which calls a few "make". I would like to know whether the makefile failed with any errors. How do I do that in the script? Thanks, S (2 Replies)
Discussion started by: suryaemlinux
2 Replies

8. Shell Programming and Scripting

Bad substitution errors in shell script

Hello, I was hoping for a second pair of eyes or a little bit of help figuring out what my error is in a script. I did some searching in the forums and didn't find anything so please forgive me if it a similar problem has been discussed before. My script accepts normal user arguments; however,... (2 Replies)
Discussion started by: Jackinthemox
2 Replies

9. Shell Programming and Scripting

Parse for errors shell script

All, I have a shell script which parses the /var/adm/messages file for errors every 15 minutes as a cron job. The script runs at 01, 16, 31, and 46 minutes every hour. The problem is if the error is encountered any time during the beginning of hour I can get paged three times. I would like to... (2 Replies)
Discussion started by: bubba112557
2 Replies

10. Shell Programming and Scripting

How to get ORA errors in alertlog file using shell script.

Hi, Can anyone tell me how to get all ORA errors between two particular times in an alertlog file using shell script. Thanks (3 Replies)
Discussion started by: suman_dba1
3 Replies
Login or Register to Ask a Question