shell script in unix

July 20, 2011 at 16:18:24
Specs: Windows 7
hi, i need help in writing a shell script that converts military time to civilian time and vice versa ,,, please help me

See More: shell script in unix

Report •

#1
August 5, 2011 at 00:27:53
Here is one that converts military to civilian time. If this is a home work assignment, I'd suggest that you not use it. Most instructors now use a search engine to validate that the assignment is the work of the student and not Jerry, Nails or one of the other contributors to this forum.

And you can figure out how to modify it to add the logic that will convert civilian to military. You can use the logic in this script, adding instead of subtracting where necessary. All you'll need to do is accept a parameter to indicate if civilian or military time is being converted.


#!/bin/ksh

if [[ $# -eq 0 || ${#1} -ne 4 ]]
then
print "Your input parameter must be a 4 digit number!!!"
exit 1
else
expr $1 + 0 >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echo "Input parameter must be numeric"
exit 2
fi
fi

typeset -L2 Hour=$1
typeset -iR2 Minutes=$1

#!/bin/ksh

if [[ $# -eq 0 || ${#1} -ne 4 ]]
then
print "Your input parameter must be a 4 digit number!!!"
exit 1
else
expr $1 + 0 >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echo "Input parameter must be numeric"
exit 2
fi
fi

typeset -L2 Hour=$1
typeset -iR2 Minutes=$1

if [[ $Hour -gt 24 || $Minutes -gt 60 ]]
then
print "Invalid time entered. Hour cannot be greater than 24 or minutes"
print "cannot be greater than 60"
exit 3
fi

if [[ $Hour -gt 12 && $Hour -ne 24 ]]
then
(( Hour = Hour - 12 ))
Meridian="PM"
elif [[ $Hour -lt 12 && $Hour -ne 0 ]]
then
Meridian="AM"
elif [[ $Hour -eq 0 || $Hour -eq 24 ]]
then
Hour=12
Meridian="AM"
elif [[ $Hour -eq 12 ]]
then
Meridian="PM"
fi

print "The time is $Hour:$Minutes $Meridian"


Report •
Related Solutions


Ask Question