string comparison problem

string comparison problem

Post by Darre » Mon, 19 Jun 2000 04:00:00



I am writing a payroll script that accepts two command line parameters, two
times, starttime and endtime..
ie.  $ payroll 09:30 14:25

The script starts with an error checking if statement designed to make sure
the user inputs the times in proper format, HH:MM

This is the line I have right now, but I keep getting an error message

if [ "$1" = [0-2][0-9][:][0-5][0-9] -a "$2" = [0-2][0-9][:][0-5][0-9] ]

Using a 24 hour clock that's why my range goes from 00:00 to 23:59
The error message I keep getting says
[: too many integers

Does anyone have any idea what I'm doing wrong? Or does anyone have a better
solution to do this format check?

Darren

 
 
 

string comparison problem

Post by Nate Eldredg » Mon, 19 Jun 2000 04:00:00



> I am writing a payroll script that accepts two command line parameters, two
> times, starttime and endtime..
> ie.  $ payroll 09:30 14:25

> The script starts with an error checking if statement designed to make sure
> the user inputs the times in proper format, HH:MM

> This is the line I have right now, but I keep getting an error message

> if [ "$1" = [0-2][0-9][:][0-5][0-9] -a "$2" = [0-2][0-9][:][0-5][0-9] ]

> Using a 24 hour clock that's why my range goes from 00:00 to 23:59
> The error message I keep getting says
> [: too many integers

> Does anyone have any idea what I'm doing wrong? Or does anyone have a better
> solution to do this format check?

AFAIK, the [ command will not match with regexs.  You'll need to
either do something with grep, or use perl.

Another option would be to just pass the times to `date -d', if you
have gnu date.  Then the time can be in any common format, and it will
convert it to whatever format you ask for, so you can use a convenient
one (seconds since epoch seems useful)

Example:

starttime=`date -d "$1" +%s`
endtime=`date -d "$2" +%s`
minutes=`expr  \( $endtime - $starttime \) / 60 `
echo You worked for $minutes minutes.

--

Nate Eldredge


 
 
 

string comparison problem

Post by Darre » Mon, 19 Jun 2000 04:00:00


Quote:> Another option would be to just pass the times to `date -d', if you
> have gnu date.  Then the time can be in any common format, and it will
> convert it to whatever format you ask for, so you can use a convenient
> one (seconds since epoch seems useful)

> Example:

> starttime=`date -d "$1" +%s`
> endtime=`date -d "$2" +%s`
> minutes=`expr  \( $endtime - $starttime \) / 60 `
> echo You worked for $minutes minutes.

I can't use date because there is no guarantee they will enter a time at
all. If they enter some words as parameters instead of times I need my
script to tell them they didn't execute the program correctly.

Using perl is also not an option either unfortunately. Can you suggest how I
would use grep to perform this error check?

Darren

 
 
 

string comparison problem

Post by David Hasset » Mon, 19 Jun 2000 04:00:00



> I am writing a payroll script that accepts two command line parameters, two
> times, starttime and endtime..
> ie.  $ payroll 09:30 14:25

> The script starts with an error checking if statement designed to make sure
> the user inputs the times in proper format, HH:MM

> This is the line I have right now, but I keep getting an error message

> if [ "$1" = [0-2][0-9][:][0-5][0-9] -a "$2" = [0-2][0-9][:][0-5][0-9] ]

The '[' (or test) command doesn't "do" regular expressions. However,
case statements do (at least in modern shells). The following should do
what you want. As a general rule, case statements are always better for
string handling than 'test'.

for i in $1 $2; do
  case $i in
    [0-1][0-9]:[0-5][0-9]|[0-2][0-4]:[0-5][0-9]) echo "Valid time - $i"
;;
    *) echo "Invalid time - $i."
       exit 1 ;;
  esac
done

The above has the added advantage of not allowing times such as 29:00 to
slip through. :-)

Cheers,

Dave. :-)

 
 
 

string comparison problem

Post by Ed L Cashi » Mon, 19 Jun 2000 04:00:00



> > Another option would be to just pass the times to `date -d', if you
> > have gnu date.  Then the time can be in any common format, and it will
> > convert it to whatever format you ask for, so you can use a convenient
> > one (seconds since epoch seems useful)

> > Example:

> > starttime=`date -d "$1" +%s`
> > endtime=`date -d "$2" +%s`
> > minutes=`expr  \( $endtime - $starttime \) / 60 `
> > echo You worked for $minutes minutes.

> I can't use date because there is no guarantee they will enter a time at
> all. If they enter some words as parameters instead of times I need my
> script to tell them they didn't execute the program correctly.

> Using perl is also not an option either unfortunately. Can you suggest how I
> would use grep to perform this error check?

You can echo the string to grep and redirect the output to /dev/null,
checking the exit value of grep, but a nicer way is to use the widely
available "expr" command's colon operator like so:

  if [ `expr "$1" : [0-2][0-9][:][0-5][0-9]` = 0 ]; then
      echo bad start time 1>&2
      exit 1
  fi
  if [ `expr "$2" : [0-2][0-9][:][0-5][0-9]` = 0 ]; then
      echo bad end time 1>&2
      exit 1
  fi

--
--Ed Cashin                     PGP public key:

    Note: If you want me to send you email, don't munge your address.

 
 
 

string comparison problem

Post by Nate Eldredg » Mon, 19 Jun 2000 04:00:00



> > Another option would be to just pass the times to `date -d', if you
> > have gnu date.  Then the time can be in any common format, and it will
> > convert it to whatever format you ask for, so you can use a convenient
> > one (seconds since epoch seems useful)

> > Example:

> > starttime=`date -d "$1" +%s`
> > endtime=`date -d "$2" +%s`
> > minutes=`expr  \( $endtime - $starttime \) / 60 `
> > echo You worked for $minutes minutes.

> I can't use date because there is no guarantee they will enter a time at
> all. If they enter some words as parameters instead of times I need my
> script to tell them they didn't execute the program correctly.

date will do that for you.

$ date -d "This is not a date"
date: invalid date `This is not a date'
$ echo $?
1

Then if the value of the variable is null, you know date failed and
you can barf appropriately.

Quote:> Using perl is also not an option either unfortunately. Can you suggest how I
> would use grep to perform this error check?

echo $user_input | grep "time_regex" || echo Sorry, bad date ; exit 1

where time_regex is your carefully crafted regex that matches a time.

--

Nate Eldredge

 
 
 

string comparison problem

Post by cLIeNUX us » Wed, 21 Jun 2000 04:00:00




>> I am writing a payroll script that accepts two command line parameters, two
>> times, starttime and endtime..
>> ie.  $ payroll 09:30 14:25

>> The script starts with an error checking if statement designed to make sure
>> the user inputs the times in proper format, HH:MM

>> This is the line I have right now, but I keep getting an error message

>> if [ "$1" = [0-2][0-9][:][0-5][0-9] -a "$2" = [0-2][0-9][:][0-5][0-9] ]

>> Using a 24 hour clock that's why my range goes from 00:00 to 23:59
>> The error message I keep getting says
>> [: too many integers

>> Does anyone have any idea what I'm doing wrong? Or does anyone have a better
>> solution to do this format check?

>AFAIK, the [ command will not match with regexs.  You'll need to
>either do something with grep, or use perl.

Right.  [0-2][0-9][:][0-5][0-9] and [0-2][0-9][:][0-5][0-9] are probably
numerous test constructs as far as the shell (Bash, and POSIX I think) are
concerned. You didn't even say what kind of script, so I assume a sh. sh
does filename globbing itself, but not regexes of arbitrary strings. Like
Perl, Awk and the Plan9 shell `rc' have "compare regex against string",
but "compare regex to regex" sounds like something that might be provably
impossible or something anyway.

"[: too many integers"   is probably failing at the first [ in "[0-2]".

The shell is very powerful, and thus confusing. You have to write it one
character at a time as pertains to the test command, in my experience.
I'd never imagine a big test construct like yours working on the first go.
You need to proceed in smaller steps. K.I.S.S.

Rick Hohensee

"So far, Linux doesn't have a lot of traction on the client
[Microsoft-ese for desktop computers], except in some university
environments."
Steve Ballmer of Microsoft, as quoted and remarked by John Schwartz
in the Washington Post, June 11 2000

 
 
 

1. string comparison problem in bash 3.00

My script takes user input and compare it with some values. It works
fine when it compares against 0.00 or 10 or 100. But if the user input
is something like "-0.009" it gives error - "integer expression
expected in line 7 and line 9" where it ought to print the number like
-

your number is -0.009.

Here's the code -

Code:

if [ "$num" = 0.00 ] || [ "$num" -le 10 ]; then        # this is line 7
   echo "your number is $num"
elif [ "$num" -ge "100" ]; then                        # this is line 9
   echo "your number is greater than 100"
else
    echo "your number is $num"

2. So true it hurts! We're getting royally screwed with this!

3. Bash string comparison not working

4. Newbie Question

5. String Comparison

6. Mandrake LILO boot loader

7. Multiple String Comparison in Ksh.

8. using rsh and vi

9. string comparison not working

10. string comparisons

11. Sting Comparisons How do i find if one string contains another

12. string comparison

13. AWK string comparisons