LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2005, 11:53 AM   #1
Vozx
LQ Newbie
 
Registered: Dec 2005
Posts: 3

Rep: Reputation: 0
Bash: Data Input Verification Help!


Hello, i need some help with several questions guys. Stuck

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.
 
Old 12-04-2005, 12:27 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

Last edited by schneidz; 12-04-2005 at 12:31 PM.
 
Old 12-04-2005, 12:32 PM   #3
Vozx
LQ Newbie
 
Registered: Dec 2005
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 12-04-2005, 01:33 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
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.
 
Old 12-04-2005, 07:23 PM   #5
Vozx
LQ Newbie
 
Registered: Dec 2005
Posts: 3

Original Poster
Rep: Reputation: 0
I understand that i have to separate them, but i am still looking for some examples how to do it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - how to take input as a variable? trees Linux - General 1 02-19-2004 04:09 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
ran out of input data? Sn1PeR Red Hat 2 09-16-2003 04:06 PM
Linux on VMWare: ran out of input data samos Linux - Newbie 0 07-27-2003 10:26 PM
Text input in bash? pk21 Programming 12 06-05-2003 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration