First argument is numeric or not


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers First argument is numeric or not
# 1  
Old 10-21-2008
First argument is numeric or not

Hi everyone, I want my script to check if the first argument has only numbers or not. Im not sure what im doing wrong.

This is how it looks like:

if [ $1 = ( grep "[^0-9]*") ]
then
echo 'The first arguement should only be in numeric' 1>&2
exit 1
else
exit 0
fi
# 2  
Old 10-21-2008
Hammer & Screwdriver Take a look at the following

Code:
> tst_val="12345"
> if [ $tst_val = `echo $tst_val | tr -d "[:alpha:]"` ]; then echo "all numbers";  else echo "Not just numbers"; fi
all numbers
> tst_val="12345a"
> if [ $tst_val = `echo $tst_val | tr -d "[:alpha:]"` ]; then echo "all numbers";  else echo "Not just numbers"; fi
Not just numbers
>

# 3  
Old 10-21-2008
I don't fully understand what the above sample script you have provided but I have modified mine so it looks like this but it has some errors.

I would appreciate if someone can fix it for me.

Thanks


if [ $1 = "echo $1 | grep "[^0-9]*" ]
then
echo 'The first arguement should only be in numbers' 1>&2
exit 1
else
exit 0
fi

if [ $2 = "echo $2 | grep "[^a-zA-Z]*" ]
then
echo 'The second arguement should only contain letters' 1>&2
exit 1
else
exit 0
fi
# 4  
Old 10-21-2008
You can also use a case statement to test the input i.e.
Code:
#!/usr/bin/bash

case $1 in
    *[!0-9]* )  echo "$1 is not all numeric" && exit 1;;
esac

exit 0

# 5  
Old 10-21-2008
Quote:
Originally Posted by darkhider
Hi everyone, I want my script to check if the first argument has only numbers or not...
if [ $1 = ( grep "[^0-9]*") ]
then
...
It might be easier if you use extended grep - egrep,

Code:
if  echo  "$1"  | egrep "^[0-9]+$"
 then
   echo "The first argument is numeric"
 else
...


Last edited by rubin; 10-21-2008 at 07:44 PM..
# 6  
Old 10-21-2008
Okay, I have another question. This script that i am trying to make is about the find command. The 1st variable is the size and the 2nd variable is the directory.

So for example the user inputs the directory name but that directory name actually doesnt exist, how can I tell the script to tell the user that "the directory that you specified doesnt exist"?
# 7  
Old 10-21-2008
Code:
[ -d "$2" ] && echo "dir found" || echo "dir not found"

man bash or google "bash scripting manual" and the first hit will teach you much more.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expr: non-numeric argument syntax error on line 1, teletype

Hi, I tried to look up the issue i'm experiencing, but i'm confused what's wrong with my script. After executing the script I'm getting the following error expr: non-numeric argument syntax error on line 1, teletype After some research, it seems that the problem relates to bc. I have... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

3. Shell Programming and Scripting

Numeric argument required

Hi, How to return the string "y" to the calling function. Getting the below error when function returns the value. return: y: numeric argument required With Regards (2 Replies)
Discussion started by: milink
2 Replies

4. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

5. Shell Programming and Scripting

expr: non-numeric argument

Hi all, i am facing the error "expr: non-numeric argument" when i use the expr command. Following is the expression which i want to execute HR=$(echo `date +%H`) MIN=$(echo `date +%M`) TOT_MIN=`expr "$HR" \* 60+$MIN` | bc echo $TOT_MIN Here I am being reported with the error expr:... (6 Replies)
Discussion started by: sparks
6 Replies

6. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

7. UNIX for Dummies Questions & Answers

Non Numeric Argument Error

hi there, I was recently introduced to this site by a friend. I hope you guys can help with a code error i can't seem to debug.I can get to add two different data types together. A snippet of the code is below: echo -n "Enter Your MOnthly Investment" read Inv PIP= $(echo "scale=2; 10 / 100"... (4 Replies)
Discussion started by: Allenzo
4 Replies

8. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

9. UNIX for Dummies Questions & Answers

non-numeric argument

quick question, I am trying to run this simple equation expr 2048 / 2.354 but get a "expr: non-numeric argument" error when ever its run. I believe it may be caused by the decimal point but I do not know how to remedy it. (3 Replies)
Discussion started by: TiredOrangeCat
3 Replies

10. Solaris

ps: 65535 is an invalid non-numeric argument for -p option

I want to figure out what is the reason of error message I have in Solaris 10. Why Solaris 10 dosn't recognize 65535? ps: 65535 is an invalid non-numeric argument for -p option usage: ps 'format' is one or more of: Thank you (5 Replies)
Discussion started by: gogogo
5 Replies
Login or Register to Ask a Question