Sponsored Content
Homework and Emergencies Homework & Coursework Questions expr to translate the date command Post 302501144 by linuxtraining on Wednesday 2nd of March 2011 07:44:19 PM
Old 03-02-2011
expr to translate the date command { solved }

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



4. 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

Last edited by linuxtraining; 03-02-2011 at 09:47 PM.. Reason: left out a $
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

expr command

I am looking for the correct syntax on the expr command in UNIX. I have a script that I am building at the moment. the script is creating file1 that is an actual .sql file that is going inside the oracle database to get some information in there. It take that information, puts it inside another... (2 Replies)
Discussion started by: wolf
2 Replies

2. UNIX for Advanced & Expert Users

Translate date value to normal date and backwards.

Hello, How do i translate datevalues in unix to normal dates. and how do i translate normal dates in to datevalues. I'm using the unix-date. Sample: 1067949360 to 4-11-03 12:36 and 4-11-03 12:36 to 1067949360 I want to built a script with a question to the user: give in date... (4 Replies)
Discussion started by: Frederik
4 Replies

3. Shell Programming and Scripting

date=`/usr/ucb/expr $date1 - 1`

Hi I need to subtract one day from date1=`/bin/date +%d` So I used date=`/usr/ucb/expr $date1 - 1` The only thing is if date1 is a single digit like 08, date will be 8 instead of 08. How can I avoid losing 0? Thanks for all your help!!! (4 Replies)
Discussion started by: whatisthis
4 Replies

4. UNIX for Dummies Questions & Answers

expr command

hi guys.... i hava a command expr... where i m adding a value in a loop like Tc=`expr $Tc\+ $l` where Tc is declred as a variable and every time l contains a new vaue if Tc =0 initially and l =2 Tc should be equal to 0+ 2 and then l = 4 Tc = 2+4 and dispaly as 6 but after... (5 Replies)
Discussion started by: madhu_aqua14
5 Replies

5. UNIX for Dummies Questions & Answers

using the expr command

Hi friends how can i execute expr $va1 * $var2 provided i m not supposed to use '/' also the nglob variable is turned off. (4 Replies)
Discussion started by: ashishj
4 Replies

6. Shell Programming and Scripting

Translate decimal into date

Hello, what can I do to convert a decimal number (001-366) into the day of the year it represent in the format of mm-dd-yyyy ? I know about the date +%j, it gives me the number for the current date, what about a reverse of this, is there such a thing? My number resides in a var, what can I do to... (3 Replies)
Discussion started by: gio001
3 Replies

7. Shell Programming and Scripting

expr command

Hi Can anyone explain me the usage of this command and the arguments used here and what will be the expected output : v_num=`expr nav_d_20100204_1759 : '*\(*\)'` what will be the value returned in v_num. Thanks in Advance!!! Regards Naveen Purbia (3 Replies)
Discussion started by: trying_myluck
3 Replies

8. Shell Programming and Scripting

expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is: $expr "${var}" : . # expr STRING : regrep where the "." is the grep wildcard for any single character. Whats wrong with my code here and is there a... (3 Replies)
Discussion started by: Tewg
3 Replies

9. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

10. Shell Programming and Scripting

Use of tr command to translate after 1st character of each line in a file

Hello, I have an input file contaning following data: < 12345;5454;77;qwert< yuyuy;ruwuriwru> yyyw; > 35353;68686;424242;hrjwhrwrwy< dgdgd; I have first character as '<' or '>'and after that one space is their in each line I just want to replace 1st space encountered after < or >... (3 Replies)
Discussion started by: abhi001cse
3 Replies
GETDATE(3)						     Library Functions Manual							GETDATE(3)

NAME
getdate - convert time and date from ASCII SYNOPSIS
#include <sys/types.h> #include <sys/timeb.h> time_t getdate(buf, now) char *buf; struct timeb *now; DESCRIPTION
Getdate is a routine that converts most common time specifications to standard UNIX format. The first argument is the character string containing the time and date; the second is the assumed current time (used for relative specifications); if NULL is passed, ftime(2) is used to obtain the current time and timezone. The character string consists of 0 or more specifications of the following form: tod A tod is a time of day, which is of the form hh:mm[:ss] (or hhmm) [meridian] [zone]. If no meridian - am or pm - is specified, a 24-hour clock is used. A tod may be specified as just hh followed by a meridian. date A date is a specific month and day, and possibly a year. Acceptable formats are mm/dd[/yy] and monthname dd[, yy] If omitted, the year defaults to the current year; if a year is specified as a number less than 100, 1900 is added. If a number not followed by a day or relative time unit occurs, it will be interpreted as a year if a tod, monthname, and dd have already been specified; other- wise, it will be treated as a tod. This rule allows the output from date(1) or ctime(3) to be passed as input to getdate. day A day of the week may be specified; the current day will be used if appropriate. A day may be preceeded by a number, indicating which instance of that day is desired; the default is 1. Negative numbers indicate times past. Some symbolic numbers are accepted: last, next, and the ordinals first through twelfth (second is ambiguous, and is not accepted as an ordinal number). The symbolic number next is equivalent to 2; thus, next monday refers not to the immediately coming Monday, but to the one a week later. relative time Specifications relative to the current time are also accepted. The format is [number] unit; acceptable units are year, month, fort- night, week, day, hour, minute, and second. The actual date is formed as follows: first, any absolute date and/or time is processed and converted. Using that time as the base, day- of-week specifications are added; last, relative specifications are used. If a date or day is specified, and no absolute or relative time is given, midnight is used. Finally, a correction is applied so that the correct hour of the day is produced after allowing for daylight savings time differences. Getdate accepts most common abbreviations for days, months, etc.; in particular, it will recognize them with upper or lower case first let- ter, and will recognize three-letter abbreviations for any of them, with or without a trailing period. Units, such as weeks, may be speci- fied in the singular or plural. Timezone and meridian values may be in upper or lower case, and with or without periods. FILES
/usr/lib/libu.a SEE ALSO
ctime(3), time(2) AUTHOR
Steven M. Bellovin (unc!smb) Dept. of Computer Science University of North Carolina at Chapel Hill BUGS
Because yacc(1) is used to parse the date, getdate cannot be used a subroutine to any program that also needs yacc. The grammar and scanner are rather primitive; certain desirable and unambiguous constructions are not accepted. Worse yet, the meaning of some legal phrases is not what is expected; next week is identical to 2 weeks. The daylight savings time correction is not perfect, and can get confused if handed times between midnight and 2:00 am on the days that the reckoning changes. Because localtime(2) accepts an old-style time format without zone information, attempting to pass getdate a current time containing a dif- ferent zone will probably fail. unc GETDATE(3)
All times are GMT -4. The time now is 10:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy