LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2004, 06:09 AM   #1
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Rep: Reputation: 15
i need help with some newbie bash commands


How do i create a script that tells me what the clock is in letters?

for i in `date`; do
etc etc

but how do i go from there?


thanks!
 
Old 12-14-2004, 06:49 AM   #2
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
Please come on guys im stuck
 
Old 12-14-2004, 06:57 AM   #3
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
Is 'date' and 'cal' not good enough?
Code:
mikkel@bitsplitter:~ $ cal
   December 2004    
sø ma ti on to fr lø
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

mikkel@bitsplitter:~ $ date
tir dec 14 13:57:43 CET 2004
mikkel@bitsplitter:~ $
 
Old 12-14-2004, 07:05 AM   #4
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
If you are interested in only the time, here's a (very) ugly hack...

Code:
list=`date`
for i in $list
do
echo "$i" | grep ":"
done
 
Old 12-14-2004, 07:09 AM   #5
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
Code:
date +%H:%M
Is probably easier Try 'date --help' to see more options regarding this.

Cheers
 
Old 12-14-2004, 07:26 AM   #6
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
okay thanks guys i made this:
#!/bin/bash
TIM=$(date +%k)
MIN=$(date +%M)
KVARTIM=$(( 16 - $TIM ))
KVARMIN=$(( ? - $MIN ))
echo du har $KVARTIM timmar och $KVARMIN minuter på dig stress!

How do i make it complete?

and how do i make a script that tells me what the time is in letters?

thanks !
 
Old 12-14-2004, 07:46 AM   #7
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
another thing i want it count the time that is left until 16:20 and seems to be a prob for me if i dont re-arrange the whole thing


thanks!
 
Old 12-14-2004, 08:16 AM   #8
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
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 might want to check out this tutorial
 
Old 12-14-2004, 08:47 AM   #9
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
i cant get it to work but thanks anyway
 
Old 12-14-2004, 08:55 AM   #10
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally posted by Alexander.s
i cant get it to work but thanks anyway
whY?
 
Old 12-14-2004, 08:57 AM   #11
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
i dont know how to make the minutes pop up
 
Old 12-14-2004, 09:14 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 12-14-2004, 09:48 AM   #13
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
i dont know how to make the minutes pop up
Code:
[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.
 
Old 12-24-2004, 02:31 PM   #14
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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
 
Old 12-24-2004, 02:33 PM   #15
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Code:
[skalkoto@nikos src]$ ./display_time -h
Usage: display_time [-a|-m|-h]
        -a: 9:40 -> <nine>:<forty>
        -m: (default) 9:40 -> It's twenty to ten
        -h: display this message
[skalkoto@nikos src]$ ./display_time -a
<ten>:<thirty-five>
[skalkoto@nikos src]$ ./display_time -m
It's twenty-five to eleven
[skalkoto@nikos src]$ ./display_time
It's twenty-five to eleven
[skalkoto@nikos src]$ date
Fri Dec 24 22:35:46 EET 2004
[skalkoto@nikos src]$

Last edited by perfect_circle; 12-24-2004 at 02:36 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash commands davatar Slackware 2 04-06-2005 03:05 AM
bash commands Obie Linux - Security 4 08-15-2004 02:42 PM
Help with BASH Commands rridler Linux - Newbie 3 04-16-2004 04:43 PM
Bash commands PhuckFonix Linux - Newbie 2 03-30-2004 07:13 PM
Bash commands wprescott Linux - Distributions 6 04-27-2002 02:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration