LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2008, 08:24 AM   #1
neelendhar
LQ Newbie
 
Registered: Feb 2007
Posts: 28

Rep: Reputation: 15
regarding shell scripts arguments


hi all,


I want to check given command line argument is integer or not in bash script

how to achieve it

for example:

./script -p abc

now valid option for -p is only integer if not script should exit

how to check that $2 is integer or not
 
Old 05-16-2008, 08:38 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
Here are some shell statements that should get you started. In your example, replace g with $2.
Code:
$ g="L"
$ typeset -i j
$ j=$g
$ echo $j
0
$ g=2347834
$ j=$g
$ echo $j
2347834
$
Firstly, g has the value of "L" which is clearly not an integer. I then create a new variable - j - using typeset and it will only be able to store integer values. When I assign the value of $g to j, it fails and j has a value of zero.

When $g has a integer value, the assignment to j will work and $g will equal $j.

In your example, replacing $g with $2, you can test that $2 = $j after assigning the value of $2 to j. If $j has the same value as $2, then you know $2 is an integer.
 
Old 05-16-2008, 08:41 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Use exit value of
Code:
checkNumeric () { typeset -i chkvar; str="$1"; let "foo=$str" 2>/dev/null; }
?

//sorry, didn't see that already got replied to, and way less terse too ;-p

Last edited by unSpawn; 05-16-2008 at 08:43 AM.
 
Old 05-17-2008, 01:09 AM   #4
neelendhar
LQ Newbie
 
Registered: Feb 2007
Posts: 28

Original Poster
Rep: Reputation: 15
hi blacky_5251

thanks for reply

solution is ok but what to do in the case he enters '0' as the argument ?


and this is the only solution that we have or any thing else.
 
Old 05-17-2008, 01:41 AM   #5
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
There are always other ways to do something in Linux - that is the beauty of it.

Test for zero explicitly before you perform the integer test.
 
Old 05-18-2008, 08:53 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
for (unsigned) integers, i always do this in my codes:
Code:
[[ "${var}" =~ "^[[:digit:]]\+$" ]]
or
Code:
[[ "${var}" =~ ^[[:digit:]]+$ ]]
for your requirement, you can replace ${var} with ${2}

please note that the first one will no longer work in newer versions of bash

example:
Code:
num=1234
if [[ "${num}" =~ "^[[:digit:]]\+$" ]]; then
   echo "unsigned integer"
else
   echo "not an unsigned integer"
fi
[[ "${num}" =~ "^[[:digit:]]\+$" ]] && echo "unsigned integer" || echo "not an unsigned integer"

Last edited by konsolebox; 05-18-2008 at 08:55 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
LXer: Parsing arguments for your shell script LXer Syndicated Linux News 0 07-18-2007 04:17 AM
Shell: Getting Arguments kalyanofb Programming 4 03-01-2007 12:20 PM
shell scripts wrappers and passing arguments nosbod Programming 2 09-21-2006 09:27 AM
Hiding arguments in shell scripts 3saul Linux - Software 2 01-09-2006 03:44 PM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 06:13 AM


All times are GMT -5. The time now is 10:41 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration