LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2009, 09:29 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 143

Rep: Reputation: 15
how can i check that $1 is number...


HI ...

how can i check at cshell that $1 is number and not contain letters

Thanks
 
Old 03-08-2009, 09:36 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
If the c-shell is like BASH, then numbers are stored as text strings.

echo $1 | grep -v [0-9]
 
Old 03-08-2009, 09:48 AM   #3
DoME69
Member
 
Registered: Jan 2008
Posts: 143

Original Poster
Rep: Reputation: 15
Thanks...

but this grep can' find

$1 = 98y88
 
Old 03-08-2009, 10:00 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
OOPS!!

grep -v [0-9] finds all lines that do not contain any numbers.

grep -v [^0-9] finds all lines that do not contain any characters that are not numbers
 
Old 03-08-2009, 10:07 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by DoME69 View Post
how can i check at cshell that $1 is number and not contain letters
...
Quote:
Originally Posted by DoME69 View Post
but this grep can' find

$1 = 98y88
True, but "98y88" isn't a number, as it does contain letters...
 
Old 03-08-2009, 01:24 PM   #6
DoME69
Member
 
Registered: Jan 2008
Posts: 143

Original Poster
Rep: Reputation: 15
how can i set, if there is letters at $1
set b = 1

else

set b = 0

?

Thanks again.
 
Old 03-08-2009, 02:15 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
I don't know anything about cshell (sorry).
But here are 3 different ways to do it in bash. Hope this helps you to translate it to cshell.
Code:
if echo $0 | grep -q '^[0-9]*$'; then
    b=1
else
    b=0
fi
Code:
echo $0 | grep '^[0-9]*$' && b=1 || b=0
Code:
echo $0 | grep -qv '^[0-9]*$'
b=$?
 
Old 03-08-2009, 02:37 PM   #8
DoME69
Member
 
Registered: Jan 2008
Posts: 143

Original Poster
Rep: Reputation: 15
Thanks for your effort
but i need it at cshell
 
Old 03-08-2009, 02:42 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by DoME69 View Post
Thanks for your effort
but i need it at cshell
Shouldn't be too difficult to translate from these bash examples. The grep command can stay the same at least.
 
Old 03-09-2009, 05:55 AM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 630

Rep: Reputation: 138Reputation: 138
if expr $1/1 == $1 then number else not a number

Should work at any shell. BTW, at least lookup the exact format for expr.

ATB

End
 
Old 03-09-2009, 02:08 PM   #11
sureshsujatha
Member
 
Registered: Mar 2009
Posts: 40

Rep: Reputation: 15
@AnantaP,
Wont the expression fail if the field inside $1 is not a number?

Last edited by sureshsujatha; 03-14-2009 at 12:19 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check the number of physical disks under RHEL ? markraem Linux - Hardware 4 09-07-2007 03:12 AM
how to check number of opened handles ruj.sabya Linux - General 2 08-06-2007 08:37 AM
Fechmail check not retuning number of emails lmcilwain Linux - Software 3 10-30-2006 08:00 PM
Check to see that a file is bigger than a certain number of MB linAM Programming 3 02-17-2005 12:42 PM
How to check the network port number? sceadu Linux - Networking 2 11-15-2004 10:05 PM


All times are GMT -5. The time now is 07:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration