LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Data Input Verification Help! (https://www.linuxquestions.org/questions/programming-9/bash-data-input-verification-help-389161/)

Vozx 12-04-2005 11:53 AM

Bash: Data Input Verification Help!
 
Hello, i need some help with several questions guys. Stuck :Pengy:

Input
Valid user input can be in the following formats:
Distance: in miles or kilometers, e.g. 100mi or 100km
Speed: miles per hour or kilometers per hour, e.g. 100m/h or 100km/h
Time: in hours and minutes, e.g. 1hr.20min, 1:20, or any integer which defaults to that number of hours, as in 3 (is 3 hours).
Any combination of the above formats can be entered on the command line and in any order.
Distance and speed can be entered as any decimal number, e.g. 50.8793mi, 78.405km/h

2 arguments will be entered at a command line and i have to calculate the third one based on those 2 arguments. I have no problem working with distance and speed, but

PROBLEM: how can i get the time converted to minutes if any of these formats are entered: e.g. 1hr.20min, 1:20, or any integer which defaults to that number of hours, as in 3 (is 3 hours).


Any help would br appreciated guys, thanks.

schneidz 12-04-2005 12:27 PM

assuming c.
here's the easy condition. this is untested but try playing around with it:
Code:

if(strchr(time-string, h) == '\0' || strchr(time-string, :) == '\0') //chck to see if 'h' or ':' is in string
{
 min = atoi(time-string);                                            //you might have to subtract the ascii value to get each integer (man atoi)
 min = min * 60;
}

_____________________

also since this is homework, try debugging what i posted and post what you tried for the other two conditions and what you are specifically having problems with, then we can try to help.

Vozx 12-04-2005 12:32 PM

Is there an easier way? I am not familiar with the above statement, so far we've been taught sed and awk to dealth with i think. I've used sed to work around with Distance and speed.

like this:
#!/bin/bash
STUFF=`echo "$1" | sed 's/[a-z,A-Z]//g'`
THING=`echo "$2" | sed 's/[a-z,A-Z,/]//g'`

if (echo "$1" | grep -q 'km$') || (echo "$1" | grep -q 'mi$')
then
if (echo "$2" | grep -q 'km/h$') || (echo "$2" | grep -q 'mi/h$')
then
echo $[$STUFF/$THING]
elif
fi
fi


But i can't get a way around with time :(

Again any help appreciated :)

Dave Kelly 12-04-2005 01:33 PM

You will have to seperate the alphanumeric letters (0-9) from the alpha letters (A-z).
Then copy them to a variable to convert.

A study of http://advbash.activeventure.net/index.html will help you understand how to these things.

Vozx 12-04-2005 07:23 PM

I understand that i have to separate them, but i am still looking for some examples how to do it. :confused:


All times are GMT -5. The time now is 05:57 PM.