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. |
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 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. |
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 :) |
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. |
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. |