bash scripting
|
Original Message
|
Name: mindman04
Date: October 25, 2004 at 22:16:58 Pacific
Subject: bash scriptingOS: Windows XP SR2CPU/Ram: Athlon 900 |
Comment: I need to write a script in the Bash shell that asks for users to input number. The script needs to check to see if a non number was entered. Please help.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: thepubba
Date: October 26, 2004 at 09:12:44 Pacific
Subject: bash scripting |
Reply: (edit)I don't do much bash. However, try this: echo "Enter a number:" read NUM if [ "$(expr $NUM + 0 2> /dev/null)" = "$NUM" ] then echo "It's a Number" else echo "It's not a Number" fi
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: thepubba
Date: October 26, 2004 at 09:35:58 Pacific
Subject: bash scripting |
Reply: (edit)Here is a better way to do it (it avoids external calls to expr and echo), but it doesn't work in bash (perhaps someone can explain why) but works fine in ksh: #!/bin/ksh print "Enter a number:" read NUM case $NUM in +([0-9]) ) print "It's a number" ;; *) print "It's not a number" ;; esac
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: October 27, 2004 at 10:31:37 Pacific
Subject: bash scripting |
Reply: (edit)Jerry: In the bash shell, for a script like yours to work, the extended pattern matching has to be turned on. Place the following line in your script, and it works fine with bash: shopt -s extglob BTW, my take on this handles a sign: shopt -s extglob i=-99 function is_integer { [[ $1 = ?([+-])+([0-9]) ]] } is_integer $i && echo "yes" || echo "no" Regards, Nails
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: