Sponsored Content
Top Forums Shell Programming and Scripting Is there a simpler way to validate user input for float? Post 302487698 by andylbh on Thursday 13th of January 2011 08:58:14 AM
Old 01-13-2011
Is there a simpler way to validate user input for float?

I'm trying to only read price (FLOAT (i.e 1.10, 3.14, etc etc))
If the input is just an integer, I will add a .00 behind. (i.e 3 becomes 3.00 , 20 becomes 20.00)
If the input is without 2 decimal places, I'll add a 0. (i.e 3.1 becomes 3.10)
I tried using the below code, it works but I don't think this is the right way to do it since I'm gonna have never ending elif statements for different conditions.
Am I doing it wrongly?
Thanks in advance.

Code:
if [[ $price =~ [^0-9.] ]] ; then 
echo "Error"
 
else
 
if [[ $price == [.][0-9] ]] ; then
price="0""$price""0"
elif [[ $price == [.][0-9][0-9] ]] ; then
price="0""$price"
elif [[ $price == [0-9] ]] ; then
price="$price.00"
elif [[ $price == [0-9][.] ]] ; then
price="$price""00"
elif [[ $price == [0-9][0-9] ]] ; then
price="$price.00"
elif [[ $price == [0-9][0-9][.] ]] ; then
price="$price""00"
elif [[ $price == [0-9][0-9][.][0-9] ]] ; then
price="$price""0"
fi
 
fi

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

validate input from user for file name

Hello, This may have been addressed already somewhere, however I am looking for the easiest/shortest way to validate a response from a user for a file name. The file name should not have any of the following characters ~`!@#$%^&*()_-+={|\:;"'<,>.?/ Further the response should not have any... (2 Replies)
Discussion started by: jerardfjay
2 Replies

2. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies

3. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

4. UNIX for Dummies Questions & Answers

input a float data and add them in shell in linux

I want to input a float data and add them in shell in linux. I am new user of it can anyone tell me how I can do it? thanx (5 Replies)
Discussion started by: purva
5 Replies

5. Shell Programming and Scripting

float input

how to input float data type in bash shell programming in linux? I am new to it so unaware to use the commands plz help me out. thank you. (6 Replies)
Discussion started by: purva
6 Replies

6. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

7. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

8. Shell Programming and Scripting

Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options). Here's the code: #!/bin/bash ########## # # Project : personal script. # Started : Wed Aug 03, 2011 # Author : Habitual # Description : susepaste c-li script with user... (5 Replies)
Discussion started by: Habitual
5 Replies

9. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

10. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies
FSF-FUNDING(7)								GNU							    FSF-FUNDING(7)

NAME
fsf-funding - Funding Free Software DESCRIPTION
Funding Free Software If you want to have more free software a few years from now, it makes sense for you to help encourage people to contribute funds for its development. The most effective approach known is to encourage commercial redistributors to donate. Users of free software systems can boost the pace of development by encouraging for-a-fee distributors to donate part of their selling price to free software developers---the Free Software Foundation, and others. The way to convince distributors to do this is to demand it and expect it from them. So when you compare distributors, judge them partly by how much they give to free software development. Show distributors they must compete to be the one who gives the most. To make this approach work, you must insist on numbers that you can compare, such as, ``We will donate ten dollars to the Frobnitz project for each disk sold.'' Don't be satisfied with a vague promise, such as ``A portion of the profits are donated,'' since it doesn't give a basis for comparison. Even a precise fraction ``of the profits from this disk'' is not very meaningful, since creative accounting and unrelated business deci- sions can greatly alter what fraction of the sales price counts as profit. If the price you pay is $50, ten percent of the profit is prob- ably less than a dollar; it might be a few cents, or nothing at all. Some redistributors do development work themselves. This is useful too; but to keep everyone honest, you need to inquire how much they do, and what kind. Some kinds of development make much more long-term difference than others. For example, maintaining a separate version of a program contributes very little; maintaining the standard version of a program for the whole community contributes much. Easy new ports contribute little, since someone else would surely do them; difficult ports such as adding a new CPU to the GNU Compiler Collection con- tribute more; major new features or packages contribute the most. By establishing the idea that supporting further development is ``the proper thing to do'' when distributing free software for a fee, we can assure a steady flow of resources into making more free software. SEE ALSO
gpl(7), gfdl(7). COPYRIGHT
Copyright (c) 1994 Free Software Foundation, Inc. Verbatim copying and redistribution of this section is permitted without royalty; alter- ation is not permitted. gcc-3.3 2003-03-01 FSF-FUNDING(7)
All times are GMT -4. The time now is 07:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy