Solved

C Shell Read Numeric value only

Posted on 2004-08-31
8
502 Views
Last Modified: 2013-12-26
Hello,

I want to write a script on Digital UNIX C Shell to read user input but only numeric value. I have created a script to do that but it does not maintain 2 special characters: * and ?.

Do you know how to write it?
------------------------------------
#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
  echo "Right " $ans
else
  echo "wrong"
endif
------------------------------------

Regards,

Paulus

0
Comment
Question by:cclmotors
8 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11948408
It is because the * etc is interpreted by the shell

following will give you an demo

#! /bin/csh -f
echo -n "Enter a number: "
set ans = $<
echo $ans

if you enter a '*'

the output echoed is the list of the files in the directories.

-----
the remedy ? ( i dont know yet :)
0
 
LVL 11

Expert Comment

by:avizit
ID: 11948428
use "set noglob" to disable metacharacters and after that metacharacters represent themselves.
be careful though if you have to use them

#! /bin/csh -f
#
# On C Shell to read a value and ensure it belongs to numeric value.
#
set noglob
echo -n "Enter a number: "
set ans = $<
if (($ans =~ [1-9]) || ($ans =~ [1-9]*[0-9]) && ($ans =~ [0-9])) then
  echo "Right " $ans
else
  echo "wrong"
endif
0
 
LVL 11

Expert Comment

by:avizit
ID: 11948477
and maybe you can change your if condition to

if (($ans =~ [0-9]) || ($ans =~ [1-9][0-9]*) )  then


0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:cclmotors
ID: 11948541
Thank you.

But it cannot maintain an input e.g. 12q

When the 3rd char is not numeric value, it always accept.
0
 
LVL 11

Expert Comment

by:avizit
ID: 11948608
the * is taken to be any charcater any number of times ..


one crude way would be to use something like

if ( ($ans =~ [0-9])  || ($ans =~ [1-9][0-9]) || ($ans =~ [1-9][0-9][0-9] ) ) then    

and so on depending on the acceptable length of the input
hardly a good solution ..
i am still looking into it though ..
0
 
LVL 38

Expert Comment

by:yuzh
ID: 11949681
Why use csh? Csh Programming Considered Harmful, see:
http://www-dbs.inf.ethz.ch/~fessler/csh-whynot.html

It is a lot easier to do it with sh/ksh/bash, eg:

expr $var + 0 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo "$var is not a number"
else
    echo "It is a number"
fi

0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 250 total points
ID: 11950354
I agree, do not use CSH.

If you use KSH for scripting, you can also use "typeset" to restrict the type of a variable.

#!/usr/bin/ksh

read tempvar
# KSH script will exit (stating "bad number") if the value set here is not integer.
typeset -i var=$tempvar
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 64
Unix / Linux grid computing 5 127
string initialization in java 11 103
wordmultiple challenge 12 90
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

20 Experts available now in Live!

Get 1:1 Help Now