LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 07: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, 07:38 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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, 07:41 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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 07:43 AM.
 
Old 05-17-2008, 12: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, 12:41 AM   #5
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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, 07: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 07:55 AM.
 
  


Reply



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 03:17 AM
Shell: Getting Arguments kalyanofb Programming 4 03-01-2007 11:20 AM
shell scripts wrappers and passing arguments nosbod Programming 2 09-21-2006 08:27 AM
Hiding arguments in shell scripts 3saul Linux - Software 2 01-09-2006 02:44 PM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:57 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
Open Source Consulting | Domain Registration