ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I think you have to create a function yourself to convert from numbers to strings... Furthermore you have to take into account that if the current minute number is greater than 20 then you'll have to lower the hour-number by one...
The following code is a very rough outline...
Code:
#!/bin/bash
function num_to_string(){
if [ $1 == 1 ]; then echo -n "one"; fi
if [ $1 == 2 ]; then echo -n "two"; fi
# blah blah...
}
MAXTIM=16
MAXMIN=29
TIM=$(date +%k)
MIN=$(date +%M)
if [ $MIN > $MAXMIN ]
then
TIM=$(($TIM - 1))
# blah blah...
fi
KVARMIN=$(( 20 - $MIN ))
echo "du har `num_to_string 1` timmar och `num_to_string $KVARMIN` minuter på dig stress!"
You can create array variables in bash with the contents being the word equivallent of the subscript value.
HOUR=(zero one two three four five six seven eight nine ten eleven twelve)
Then use the hour and minute value of the date to access the value.
[skalkoto@localhost skalkoto]$ cat a.sh
#! /bin/bash
a=$1
echo "a is $a"
let "b = $a % 10"
echo "b is $b"
let "c = ($a -$b)/10"
echo "c is $c"
[skalkoto@localhost skalkoto]$ ./a.sh 23
a is 23
b is 3
c is 2
[skalkoto@localhost skalkoto]$
Is this what you mean?
Last edited by perfect_circle; 12-14-2004 at 12:05 PM.
Ok, well i had some spare time and I thought to give it a try. Hope you're still interested.
I'm not a good scripter, (haven't script in bash for over 2 years), so this is probably not the best code you've ever seen.
Code:
#!/bin/bash
#
#diplay_time
#
function num2char()
# converts a number to a string: 1 -> one,2 -> two,...
{
case $1 in
0) echo -n "zero";;
1) echo -n "one";;
2) echo -n "two";;
3) echo -n "three";;
4) echo -n "four";;
5) echo -n "five";;
6) echo -n "six";;
7) echo -n "seven";;
8) echo -n "eight";;
9) echo -n "nine";;
10) echo -n "ten";;
11) echo -n "eleven";;
12) echo -n "twelve";;
13) echo -n "thirteen";;
14) echo -n "fourteen";;
15) if [ $2 ]; then echo -n "fifteen"
else echo -n "quarter";fi;;
16) echo -n "sixteen";;
17) echo -n "seventeen";;
18) echo -n "eighteen";;
19) echo -n "nineteen";;
20) echo -n "twenty";;
30) if [ $2 ]; then echo -n "thirty"
else echo -n "half";fi;;
40) echo -n "forty";;
50) echo -n "fifty";;
esac
}
function minutes()
#
#displaying the minutes...
#
{
let "b = $MIN % 10" #if min = 12,
let "a = ($MIN -$b)" #a=10,b=2
if [ "$1" -lt 20 ]; #get the values
then #directly from num2char()
echo -n "`num2char $1 $2`"
else #construct it first:
echo -n "`num2char $a $2`" #23 = 20 + 3 -> twenty-three
if [ $b -ne 0 ]
then
echo -n "-`num2char $b $2`"
fi
fi
}
function strip_leading_zero()
#This function is from "Advanced Bash-Scripting Guide"
#Appendix A contributed Scripts Example A-8. days-between:
#Calculate number of days between two dates.
{
val=${1#0} # this function strips possible leading zero(s)
return $val # This is required because a number starting
} # with a zero is interpreted as octal in Bash.
# So, I get an error with 08, 09
function usage()
#
#Displays the Usage message
#
{
echo "Usage: $1 [-a|-m|-h]"
echo " -a: 9:40 -> <nine>:<forty>"
echo " -m: (default) 9:40 -> It's twenty to ten"
echo " -h: display this message"
}
function error_msg()
#
#Displays the Error message
#
{
echo "*ERROR*: wrong input"
echo "`usage $1`"
}
if [ "$#" -gt 1 ] #error
then
echo "`error_msg ${0##*/}`"
exit 0
fi
TIM=$(date +%l) #[1-12]
strip_leading_zero $(date +%M) #[00-59] stripping zero(s)
MIN=$? #if needed
param=${1:-"-m"} #set the "-m" as default
#param=${1:-"-a"} #set the "-a" as default
case "$param" in
"-a") echo "<`num2char $TIM`>:<`minutes $MIN 1`>";;
"-m") echo -n "It's "
if [ "$MIN" -eq 0 ] #9:00 -> "nine o'clock"
then
echo "`num2char $TIM` o'clock"
elif [ "$MIN" -lt 31 ] #9:23 -> "twenty-three past nine"
then
echo "`minutes $MIN` past `num2char $TIM`"
else #9:37 -> "twenty-three to ten"
let "MIN = 60 - $MIN"
let "TIM = ($TIM + 1)"
if [ $TIM -eq 13 ]; then TIM=1; fi # 12+1=1
echo "`minutes $MIN ` to `num2char $TIM`"
fi;;
"-h") echo "`usage ${0##*/}`";;
* ) echo "`error_msg ${0##*/}`"; exit 0;; #error
esac
exit 1
If you find any bugs or spelling mistakes (my english are poor) let me know.
MERRY X-MAS
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.