Solved

csh shell script problem

Posted on 2008-10-02
4
672 Views
Last Modified: 2013-12-26
My script can handle all positive integer input, but crash when the input is a decimal, string, negative value etc. I thought the if statement should be able to test against all the printable characters except from 0 to 9. Then why I got the output if I enter -10? Lastly, I run this script on another machine and the OS is ubuntu 2.6.24-19-386. I got all the expected output. Weird eh!!  What can I do to fix this problem? Please show your working code. Thanks!

e.g. machine one
Thor:~> ./myScript
Please enter a positive integer 100
10% of 100 is 10
Thor:~> ./myScript
Please enter a positive integer -10
10% of -10 is -1
Thor:~> ./myScript
Please enter a positive integer 10.5
@: Badly formed number.
Thor:~> ./myScript
Please enter a positive integer abc
@: Expression Syntax.

e.g. machine two
% ./myScript
Please enter a positive integer 10
10% of 10 is 1
% ./myScript
Please enter a positive integer -10
Sorry positive integer only
% ./myScript
Please enter a positive integer 10.5
Sorry positive integer only
% ./myScript
Please enter a positive integer abc
Sorry positive integer only
%





#!/bin/csh

echo -n "Please enter a positive integer "

set input=$<

if ( $input =~ *[\!-/:-~]* ) then

     echo Sorry positive integer only

else

     @ answer = (($input * 10) / 100)

     echo 10% of $input is $answer

endif

Open in new window

0
Comment
Question by:alfredng1
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Hanno Schröder
ID: 22632435
Wich Unix or Linux variant are you having problems with?
#!/bin/csh

echo -n "Please enter a positive integer "

set input=$<

if ( $input =~ [0-9]* ) then

     @ answer = (($input * 10) / 100)

     echo 10% of $input is $answer

else

     echo Sorry positive integer only     

endif

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 22632496
You should probably quote "$<" and "$input", but that doesn't explain the problem
different versions of csh seem to have strange problems with =~ *[
#!/bin/csh -xv
may or may not provide some clues to what is going wrong
you might want to try a different approach, such as
set input="$<"
if ( "${input:as/0//:as/1//:as/2//:as/3//:as/4//:as/5//:as/6//:as/7//:as/8//:as/9//}" != "" ) then

0
 
LVL 84

Expert Comment

by:ozo
ID: 22632513
$input =~ [0-9]*
doesn't catch 10.5
0
 

Accepted Solution

by:
alfredng1 earned 0 total points
ID: 22633921
ok, in bash shell, if I do -
something=`echo $input | grep -E ABC`
then I can have a result stored in the variable something. Please tell me how to do this in csh shell! I might be able to solve my problem. Then I will post back here. Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now