Solved

csh shell script problem

Posted on 2008-10-02
4
674 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

896 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

17 Experts available now in Live!

Get 1:1 Help Now