Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

csh shell script problem

Posted on 2008-10-02
4
Medium Priority
?
699 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 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 85

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 85

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
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.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

773 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