expr to translate the date command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. Write a script called "tod" that will display the time of day in am or pm notation rather then the 24 hour clock time. Use expr to convert from 24-hour clock time.
    Use either (if) or (case) statement. Use the standard, unmodified output from the date command for this exercise.

  2. Relevant commands, code, scripts, algorithms:
    date, cut and expr

  3. The attempts at a solution (include all code and scripts):#!/bin/sh -xv
    # this is the date script
    #this will change the 24hr to 12hr
    #
    #
    let i=$"`date +%H`"
    let MIN=$"`date | cut -c15-19`"
    if [ $i -gt 12 ]
    then
    `expr $i -12`
    echo " $i:$MIN"
    fi
    esle
    echo "$i:$MIN"
    fi
    ########################################
    I've also used
    ########################################
    HR=$"`date | cut -c1-2`"
    MT=$"`date | cut -c15-19
    NEWHR=$"`expr $HR - 12`"
    if [ $HR -le 12 ]
    then
    echo "$HR:$MT"
    esle
    echo " $NEWHR:$MIN"
    fi
    ########################################
    and
    ########################################
    HR=$"`date | cut -c1-2`"
    MT=$"`date | cut -c15-19
    NEWHR="`expr $HR - 12`

case $NEWHR
in
24) `expr $NEWHR -12`;;
echo "$NEWHR:$MT"
23) `expr $HR -12`;;
and so on till
*) to eat up the rest
esac

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Washtenaw community college, Washtenaw MI, Phil Geyer, CIS 221.

Any help would be great this has been driving me nuts all spring break. Note : I'm using bash shell, the expr is the thing going wrong it keeps giving me a non-numeric argument error.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

---------- Post updated at 07:44 PM ---------- Previous update was at 07:39 PM ----------

#!/bin/sh -xv
# this is the date script
#this will change the 24hr to 12hr
#
#
let i=$"`date +%H`"
+ date +%H
+ let i=$19
./datt: let: not found
let MIN=$"`date | cut -c15-19`"
+ date
+ cut -c15-19
+ let MIN=$43:47
./datt: let: not found
if (\ [ "$i" -gt 12 \) -a \( "$i" -eq 24 \) ]
then
./datt: syntax error at line 10: `then' unexpected
I figured it out thank you for helping me
code:
this will change the 24hr to 12hr
#
#
HR=$"`date +%H`"
MIN=$"`date +%M`"
SEC=$"`date +%S`"
NEWHR=$"`expr $HR - 12`"
#
#
#
if [ $HR -gt 12 ]
then
echo "$NEWHR:$MIN:$SEC pm"
else
echo "$NEWHR:$MIN:$SEC am"
fi

There needs to be spaces between everything in expr. 3 -12 is invalid, 3 - 12 is okay.

That aside your script can be made a lot better in other ways. You don't need to be doing cut all the time to do simple string processing, that's really slow and difficult.

#!/bin/bash

T="am"
# One way to split strings
read H M S <<<$(date +'%H %M %S')
# Another:
# STR=`date '+%H %M %S'`
# H="${STR:0:2}"
# M="${STR:3:2}"
# S="${STR:6:2}"

H="${H##0}"     # Strip leading zero

if [[ "${H}" -gt 12 ]]
then
        T="pm"
        H=`expr $H - 12`
fi

# printf makes it easy to put them all back together.
# We print the first one as digits, so it can add a leading zero,
# but the rest we just print as strings.
printf "%02d:%s.%s %s\n" $H $M $S $T

thank you for your reply Corona I get a error
[[: H##0: expression recursion level exceeded (error token is "H##0")
But thank you for taking the time to help. When I run this in debugging mode it shows the H=`expr` to be null value this is a problem that I have been fighting for the last week. I can't get the expr command to realize that it has a value for some reason.

---------- Post updated at 08:17 PM ---------- Previous update was at 08:15 PM ----------

./datt3: line 5: syntax error near unexpected token `<<<$'
./datt3: line 5: `read H M S <<<$(date +'%H %M %S')'
also those two errors are new.

Be sure you entered the code I gave you word for word, letter for letter, keystroke for keystroke.

Are you sure you're using bash? bash is not just any shell.

Yes. I even made sure by doing echo $SHELL and it printed bash.
The school server is a little weird I think they don't have somethings active on it until we get in the 226 class so that maybe the reasoning for the errors we encountered. Thank you very much for all you help though. I'm doing the same problem now using the case statement. ( just because I love writing scripts and Linux)

What does bash --version do.

---------- Post updated at 08:17 PM ---------- Previous update was at 08:16 PM ----------

If you can't use <<< because you have a ridiculously old version of bash, you can do

date +'%H %M %S' > /tmp/$$
read H M S </tmp/$$
rm -f /tmp/$$

GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)
Copyright 2000 Free Software Foundation, Inc.

That's your login shell, not what you're using in your shell script.

I see #!/bin/sh at the top of your script. sh might be bash on your system -- or it might not. It might even be dash on some systems, which has almost no features... Try #!/bin/bash

Yea I can do redirection doing simple things like that. but yea it's from 2000 I think.

Okay, you're on Solaris. That means #!/bin/sh isn't bash. It's not even proper POSIX. You're getting Sun's weird old "backwards compatible" shell, barely younger than Disco. If you're allowed to use BASH, even the ancient version they have(you're using 2.0, we now have 4.0) is about 20 years newer.

The read + temporary file trick should work in either shell I think, but "<<<" definitely won't.

You should ask your prof which shell you're supposed to be using, since solaris #!/bin/sh is especially painful.

You should also be very grateful that you have GNU date. :slight_smile:

Yes it really is. I run Ubuntu and have the stuff I do I have to rewrite to wok on the Solaris shell to work.

If I had to guess, I'd think that maybe expr is throwing up on the leading 0 in hour, but I have no way to test that, I don't have access to a solaris system anymore.

I got it. It was pretty easy once I took a minuet to look over the code but now I'm trying to at a argument and assign it to a perimeter where it has to be a number. like 1-24 but i get errors.

Please show the current version of your script and the exact error message which you received.

Slightly off-topic. On this vintage of Solaris surely Korn Shell would be more reliable ... and easier for others to present code which will run.

#!/usr/bin/ksh