Solved

csh shell script problem

Posted on 2008-10-02
4
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Hanno P.S.
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

632 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