Solved

csh shell script problem

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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